CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2009
    Posts
    1

    beginner help with switch and path

    Code:
    #include <iostream>
    
    void encode()
    {
        using namespace std;
        char szInput[255] = { 0 };
        cout << "What do you want to encode?" << endl;
        cin.getline(szInput, 255);
        for(int iii = 0; iii < 255; iii++)
        {
            if((int)szInput[iii] > 64 && (int)szInput[iii] < 123)
            {
            szInput[iii] = (int)szInput[iii] + 13;
            cout << szInput[iii];
            }
            else
            cout << szInput[iii];
        }
    
    }
    
    
    int main()
    {
        using namespace std;
        cout << "What do you want to do?" << endl;
        cout << "1. Encode" << endl;
        cout << "2. Decode" << endl;
        int nSel;
        cin >> nSel;
        switch(nSel)
        {
            case 1:
                encode();
                break;
            case 2:
                break;
            default:
                main();
        }
    
    }
    not complete yet, but builds with no errors. When i select 1, it executes encode() but it only prints "What do you want to encode?" and then the program doesn't go any further and finishes not giving you a chance to input anything. Could someone help?

  2. #2
    Join Date
    Dec 2008
    Posts
    56

    Re: beginner help with switch and path

    Initially when you prompt the user for a choice between encoding and decoding, only the numeric input is processed and stored in nSel. The newline character is still sitting in the cin stream, and unfortunately for you it is picked up by the next read, which is the getline() in the encode() function.

    To correct the issue, instruct cin to ignore all characters in its stream up till and including the newline character; this is how you do this:
    Code:
    ...
        int nSel;
        cin >> nSel;
        cin.ignore(100, '\n');   // skip the next 100 characters until a newline is found; 100 was arbitrarily chosen
    ...
    Btw, the main() function is a special function that is called by the system startup function. You should never call it yourself. Thus remove the call to main() from the default case of your select block, and instead output a message to the user that they made an invalid choice.

    If you want to re-prompt the user to enter a valid choice, then place your prompt, the read, and the select inside a loop (e.g. a while- or do-loop).

    Code:
    int main()
    {
      bool done = false;
    
      while (!done)
      {
        cout << "Menu:"
             << "\n\t1. encode"
             << "\n\t2. decode"
             << "\n\t3. quit"
             << "\n\nChoice: ";
        int nSel;
        cin >> nSel;
        cin.ignore(100, '\n');
    
        switch(nSel)
        {
          ...
    
          case 3:
              done = true;
              break;
    
          default:
              std::cout << "Invalid input; try again..." << std::endl;
              break;
        }
      }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured