Function issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Learner21
    New Member
    • Mar 2008
    • 9

    Function issue

    I think there is something wrong with my program I got this fom my TA and I think he kind of messed up somewhere I just don't know what to change:
    [code=cpp]
    #include <iostream>
    using namespace std;

    int String(char[]);
    char Menu();
    void Second(char, char, char[],int);

    int main()

    {

    char Twinkle[80] = {' '};
    char select = ' ';
    char Sub_char = 'A';
    char count = 'B';
    char cont = 'Z';
    cout <<"Please enter a string: ";
    cin.getline(Twi nkle, 80);
    cout <<"\n";

    do
    {
    select = Menu();

    if(select != 'A')
    {
    cout << "Invalid selection ! Please re-enter !\n";
    cin >> select;
    }



    char String_1[80] = {' '};
    char character = ' ';
    char sub = ' ';
    int i = 0;
    switch(select)
    {
    case 'A':
    case 'a':
    {
    cout << "Please enter the character to be scanned: ";
    cin >> character;
    cout << "Please enter the character to be substituted: ";
    cin >> sub;
    Second(characte r, sub, String_1, 80);

    while(String_1[i] != '\0')
    {
    cout << String_1[i];
    ++i;
    }
    }

    }
    while(select != 'Z');

    system("pause") ;
    return 0;
    }
    }

    char Menu()
    {
    cout << "Welcome to the string program ! \n";
    cout << "============== =============== == \n";
    cout << "A - Substitute Character \n";
    cout << "B - Count Character \n";
    cin >>select;
    return select;
    }
    void Second(char Ch, char Su, char Str_2[],int num_1)
    {

    int i = 0;
    cout << Ch;
    cout << Su;
    cout << "The updated String is: ";



    while(Str_2[i] != '\0')
    {
    if(Str_2[i] == Ch)
    {
    Str_2[i] == Su;
    }
    i++;
    }
    }[/code]
    Last edited by sicarie; Apr 8 '08, 03:50 PM. Reason: Code tags are [code=cpp] and after your code [/code]. Please use them. They want to be used. They like to be used.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Have you tried stepping though the code using your debugger?

    That will show you the program flow and reveal the contents of all your variables as you go along.

    If you don't know how to use your debugger, then this program presents an excellent opportunity.

    Comment

    • Learner21
      New Member
      • Mar 2008
      • 9

      #3
      Yea and I got four errors well I had to start all over so here is what I have:
      [code=cpp]
      char Menu();
      void insrt_ch(char[], char, char);
      int main()

      {

      char Twinkle[80] = {' '};
      char select = ' ';
      char Sub_char = 'A';
      char count = 'B';
      char cont = 'Z';
      cout <<"Please enter a string: ";
      cin.getline(Twi nkle, 80);
      cout <<"\n";



      cout << "Welcome to the string program ! \n";
      cout << "============== =============== == \n";
      cout << "A - Substitute Character \n";
      cout << "B - Count Character \n";
      cin >>select;
      if(select != 'A')
      {
      cout << "Invalid selection! Please re-enter!";
      cin >> select;
      }


      return select;

      char String_1[80] = {' '};
      char character = ' ';
      char sub = ' ';
      int i = 0;
      switch(select)
      {
      case 'A':
      case 'a':
      {
      cout << "Please enter the character to be scanned: ";
      cin >> character;
      cout << "Please enter the character to be substituted: ";
      cin >> sub;
      insrt_ch(charac ter, sub, String_1);
      [/code]
      Now what I need to do since I have the second function as well and this is what I have so far I am not going to debug because I want to make sure that I have what I need. Instruction is:
      Function 2: (Accept three arguments: an array and two characters, and return no value)
      This function will accept an array containing the string and two characters. It will scan for the first character and when it finds the character, it will substitute the first character with the second character. Prompt for the character to be scanned and the character to be substituted in main() and pass those into the function. Its alot I know and I apologize but really I just want tknow if I am doing it right so far thanks again
      Last edited by sicarie; Apr 9 '08, 01:26 PM. Reason: Code tags are [code=cpp] and after your code [/code]. Please use them. They want to be used. They like to be used.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        [quote-Learner21]
        insrt_ch(charac ter, sub, String_1);
        [/quote]

        When you make this call, String_1 does not have the contents of Twinkle.

        Also, this code:
        Originally posted by Learner21
        etc....
        cin >>select;
        if(select != 'A')
        {
        cout << "Invalid selection! Please re-enter!";
        cin >> select;
        }


        return select; <<<<<<<<<<<<<<< <<<<<<<<

        char String_1[80] = {' '};
        etc....
        is going to have a hard time getting past this return. That is going to terminate main().

        Comment

        Working...