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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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, 03:02 PM
  2. CIN Input count question?
    By kamran in forum C++ Programming
    Replies: 5
    Last Post: 10-24-2006, 04:06 PM
  3. Noob question (redeclaring a array)
    By Demon.killer in forum C Programming
    Replies: 8
    Last Post: 10-21-2006, 12: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, 07:25 PM