+ Reply to Thread
Results 1 to 11 of 11

Thread: C++ question

  1. #1
    Ultimate Member nothing's Avatar
    Join Date
    Oct 2002
    Location
    Brazil
    Posts
    1,413

    C++ question

    Let's say I have a menu like this

    1.Start
    2.Help
    3.Exit

    and the user, instead of typing a number and pressing enter, he/she just presses enter. How do I make my program display an error message when that happens? Thanks.

  2. #2
    Hired Geek fishybawb's Avatar
    Join Date
    Jun 2002
    Location
    York, UK
    Posts
    3,371
    Using cin won't work for this, as you've probably found out - it waits for a value, and ignores "empty" (linefeed) input. Here's one way of doing it:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
      // cin.get needs a char variable - they can store ints too!
      char choice = 0;
    
      // while the input is "bad" ie. less than 1 OR greater than 3
      while(choice < 1 || choice > 3)
      {
        // prompt the user for a number
        cout << endl << "Enter a choice (1-3): ";
        // get the number they typed
        cin.get(choice);
        // clear the stream queue to ignore "enter" presses
        cin.sync();
        // convert the keycode to actual number (1 = 49 in ascii)
        choice = choice - 48;
       }
    
      switch(choice)
      {
        case 1:
          // user chose 1
          break;
    
        // etc...
      }
    
      return 0;
    }
    cin.get() grabs the input as an ASCII code, so to convert it to a number, we have to subtract 48 from it - 1 is 49 in ASCII, 2 is 50 and so on. It's a bit messy and there's probably a better way of doing it, but I can't think of one


  3. #3
    Ultimate Member nothing's Avatar
    Join Date
    Oct 2002
    Location
    Brazil
    Posts
    1,413
    Well, that is very nice fishybawb. You have no idea how long I've been trying to do something like that. Thank you very much!

  4. #4
    Hired Geek fishybawb's Avatar
    Join Date
    Jun 2002
    Location
    York, UK
    Posts
    3,371
    You're welcome

  5. #5
    Ultimate Member nothing's Avatar
    Join Date
    Oct 2002
    Location
    Brazil
    Posts
    1,413
    Hey fishy, what compiler do you use?

  6. #6
    Hired Geek fishybawb's Avatar
    Join Date
    Jun 2002
    Location
    York, UK
    Posts
    3,371
    I used to use Visual Studio .NET 2003, but it's pretty slow and clunky, so I've gone back to 6.0 - I lost interest in .NET fairly quickly anyway. How about you?

  7. #7
    Ultimate Member nothing's Avatar
    Join Date
    Oct 2002
    Location
    Brazil
    Posts
    1,413
    Ok. Could you please compile and run this program using your compile and tell me what happens?
    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    
    #include <string>
    using std::string;
    
    int main()
    {
    	string choice;
    
    	do {
    
    		cout << "Enter a choice: ";
    		std::getline (cin, choice);
    	
    	} while(choice[0] < '1' || choice[0] > '3');
    
    	return 0;
    }

  8. #8
    Hired Geek fishybawb's Avatar
    Join Date
    Jun 2002
    Location
    York, UK
    Posts
    3,371
    It behaves badly. Entering valid numbers (ie. 1-3) works, but you have to press enter twice. Enter an INvalid number and you still have to press enter twice, but repeated bad attempts only need one enter press to get the next prompt. Entering a valid number at this point still needs two enters, but after the first you get and "Enter a choice:" prompt, even though pressing enter alone is enough to end the program. I hope that makes sense I'm fairly sure that's the result you'd get on any compiler...

  9. #9
    Ultimate Member nothing's Avatar
    Join Date
    Oct 2002
    Location
    Brazil
    Posts
    1,413
    I am really sorry to say but you're wrong, my dear friend.

    http://www.cs.drexel.edu/~mcs171/Sp0...tructions.html

    Read that

  10. #10
    Hired Geek fishybawb's Avatar
    Join Date
    Jun 2002
    Location
    York, UK
    Posts
    3,371
    Learn something new every day I'm not sure I've ever used getline, but if I do it's now fixed thanks to you. Looking at the code I thought it was a combination of the do...while and getline leaving newline codes in the keyboard buffer... D'oh!

  11. #11
    Ultimate Member nothing's Avatar
    Join Date
    Oct 2002
    Location
    Brazil
    Posts
    1,413
    I was looking for information on how to use the getline function and that page was among the results. This must be my lucky day

Bookmarks

Posting Permissions

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







New Security Features Planned for Firefox 4
Another Laptop Theft Exposes 21K Patients' Data
Oracle Hits to Road to Pitch Data Center Plans
Microsoft Preps Array of Windows Patches
Microsoft Nears IE9 Beta With Final Preview
Simplified Analytics Improve CRM, BI Tools
Android Passes RIM as Top Mobile OS in 2Q
VMware Updates Hyperic System Management
File Monitoring Key to Enterprise Security
LinkedIn Snaps Up SaaS Player mSpoke