cin Help...Sort of a noob Question?

This is a discussion on cin Help...Sort of a noob Question? within the C++ Programming forums, part of the General Programming Boards category; I have this problem with cin, and I'm not sure what to do: Here's some of my code: Code: void ...

  1. #1
    People Love Me
    Join Date
    Jan 2003
    Posts
    412

    Question cin Help...Sort of a noob Question:

    I have this problem with cin, and I'm not sure what to do: Here's some of my code:

    Code:
    void Menu1()
    {
    	cout << "\nSelect a command:";
    	for ( ; ; )
    	{
    		bool exit = false;
    
    		cout << "\n1 - Go Exploring";
    		cout << "\n2 - Exit";
    		cout << "\nCommand: ";
    
    	int input;
    	cin >> input;
    
    	switch (input)
    	{
    	case 1: cout << "\nOne!";
    		break;
    	case 2: exit = true;
    		break;
    	default: cout << "\nThat wont work."
    		break;
    	}
    
    		if (exit)
    		       break;
    		else
    		       continue;
    	}
    }
    My problem is thus: when you type in any other intenger besides one or two, the default message appears. But, whenever you type in a non-intenger input, the program crashes horribly. I know that since the cin variable is an intenger, that it can only accept intengers, but how do I keep the program from crashing?
    I just want the default message to be printed once if you type in anything other than an intenger.

    BTW: I know that my for ( ; ; ) could be written as while(!exit), but that's just the way I like to do my menu loops.
    Last edited by Krak; 01-25-2003 at 07:33 AM.

  2. #2
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Use a character array

    Code:
    char choice[100];
    
    cout << "1. sleep\n2. eat\nChoose:";
    gets(choice);
    
    switch(choice[0])
    {
    case '1':
         cout << "sleep";
         break;
    case '2':
       cout << "eat";
       break;
    }
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  3. #3
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>but how do I keep the program from crashing?
    Clean up if cin has an error by first clearing the stream of errors and then discarding the junk input and starting over with a clean slate, then jump back to the beginning of the loop and try again
    Code:
    void Menu1()
    {
      cout << "\nSelect a command:";
      for ( ; ; )
      {
        bool exit = false;
    
        cout << "\n1 - Go Exploring";
        cout << "\n2 - Exit";
        cout << "\nCommand: ";
    
        int input;
        cin >> input;
    
        if (!cin)
        {
          cin.clear();
    
          while (cin.get() != '\n')
          {}
    
          continue;
        }
    
        switch (input)
        {
          case 1: cout << "\nOne!";
            break;
          case 2: exit = true;
            break;
          default: cout << "\nThat wont work.";
            break;
        }
    
        if (exit)
          break;
      }
    }
    >>Use a character array
    That's a waste, using a 100 character array just so that you can use the first one and ignore the others.

    >>gets(choice);
    You risk a buffer overflow this way, since this is C++ you might as well use getline and be safe. C++ is hard enough without forcing more difficulties into things. :-)
    Code:
    cin.getline(choice, 100);
    *Cela*

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    you could also input it in as a char. then use the cctype functions isdigit() or isalpha(), and if it is a digit, cast it as an int or store it in an int then switch that int.

  5. #5
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >> then use the cctype functions isdigit() or isalpha(), and if it is a digit, cast it as an int or store it in an int then switch that int.
    Why all that? Just treat it as a character and save yourself a lot of effort. :-)
    Code:
    void Menu1()
    {
      cout << "\nSelect a command:";
    
      for ( ; ; )
      {
        cout << "\n1 - Go Exploring";
        cout << "\n2 - Exit";
        cout << "\nCommand: ";
    
        char input;
        cin >> input;
    
        switch (input)
        {
          case '1':
            cout << "\nOne!";
            break;
          case '2': 
            return;
          default:
            cout << "\nThat wont work.";
        }
      }
    }
    *Cela*

  6. #6
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    I think an array would be more ideal because it would work even if the user enters more than one digit
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  7. #7
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Code:
    	    if (!cin)
        {
          cin.clear();
    
          while (cin.get() != '\n')
          {}
    
          continue;
        }
    Newbie Question: What's this?

  8. #8
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Sort of a noob Question => Qbeinnooostu

  9. #9
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    On a more serious note I would write something like

    Code:
    #include <cctype>
    using std::isspace;
    
    ...
    ..
    .
    
    int c, n;
    
    // skip ws
    while(isspace(c = cin.get()))
          continue;
    
    if (isdigit(c)) {
          cin.putback(c);
          cin >> n;
    } else {
          cout << "Error not a number" << endl;
    }
    It won't stop someone from entering something like
    4a but it's good enough for most applications.
    Also isdigit, isalpha, isspace take ints. This is because EOF == -1 on alot of machines.

  10. #10
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>I think an array would be more ideal because it would work even if the user enters more than one digit
    So do both of my examples. But mine are easier to follow since a reader wouldn't have to wonder what you intend to use the rest of the array for and it doesn't use up nearly as much memory just for a single character. :-)

    >>Newbie Question: What's this?
    If cin fails it sets some bits so that you can't use the stream anymore, if (!cin) tells you if any of them are set and cin.clear() resets those bits. The while loop then reads the bad input and throws it away so that next time you start clean.
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple cin question
    By verbity in forum C++ Programming
    Replies: 25
    Last Post: 05-09-2007, 04:02 PM
  2. CIN Input count question?
    By kamran in forum C++ Programming
    Replies: 5
    Last Post: 10-24-2006, 05:06 PM
  3. Noob question (redeclaring a array)
    By Demon.killer in forum C Programming
    Replies: 8
    Last Post: 10-21-2006, 01:06 PM
  4. Replies: 5
    Last Post: 11-01-2002, 06:09 PM
  5. Linked list & radix sort question, please help!
    By Lior in forum C Programming
    Replies: 5
    Last Post: 09-02-2002, 08:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21