Newbie question (user error checking)

This is a discussion on Newbie question (user error checking) within the C++ Programming forums, part of the General Programming Boards category; I'm just starting to learn C++.. and this is really my first programming language, but I'm not sure how to ...

  1. #1
    eno
    eno is offline
    Registered User eno's Avatar
    Join Date
    Nov 2002
    Posts
    8

    Newbie question (user error checking)

    I'm just starting to learn C++.. and this is really my first programming language, but I'm not sure how to prevent users from messing up my program when they start entering garbage. Here's an example I wrote:

    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
    	int choice;
    	while (choice != 3) {
    		cout << "Choice: ";
    		cout << "\n(1) First\n(2) Second\n(3) Quit\n\n";
    		cin >> choice;
    		if (choice == 1) {
    			cout << "First\n";
    		}
    		if (choice == 2) {
    			cout << "Second\n";
    		}
    	}
    	cout << "Exit\n";
    	return 0;
    }
    Now lets say someone enters 'g'... it would just go into an infinite while loop.. and I'd have to close it. I've actually had this problem for quite a while. Any help is really appreciated

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You can test cin for failure and act accordingly:
    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
    	int choice;
    	while (choice != 3) {
    		cout << "Choice: ";
    		cout << "\n(1) First\n(2) Second\n(3) Quit\n\n";
    		
        cin >> choice;
        if ( !cin )
          break;
    
    		if (choice == 1) {
    			cout << "First\n";
    		}
    		if (choice == 2) {
    			cout << "Second\n";
    		}
    	}
    	cout << "Exit\n";
    	return 0;
    }
    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    try this..

    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
    	char choice;
    	while (choice != '3') {
    		cout << "Choice: ";
    		cout << "\n(1) First\n(2) Second\n(3) Quit\n\n";
    		cin >> choice;
    		if (choice == '1') {
    			cout << "First\n";
    		}
    		if (choice == '2') {
    			cout << "Second\n";
    		}
    	}
    	cout << "Exit\n";
    	return 0;
    }
    The problem was that it only accepted numbers. Now it should work...
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  5. #5
    eno
    eno is offline
    Registered User eno's Avatar
    Join Date
    Nov 2002
    Posts
    8
    Wow what a quick response! Thanks so much guys

    I didn't realize you could do that with cin

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    The problem was that it only accepted numbers. Now it should work...
    Wouldn't it still screw up if the user typed in "asdfgjskdfjsf"?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Hunter2
    Wouldn't it still screw up if the user typed in "asdfgjskdfjsf"?
    Or if choice was initially '3', which you have no control over seeing as it's uninitilized.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 11:47 PM
  2. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 09:23 AM
  3. Testing user input question?
    By Hoser83 in forum C Programming
    Replies: 18
    Last Post: 02-15-2006, 01:18 PM
  4. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  5. easy newbie question
    By ioaz in forum C++ Programming
    Replies: 7
    Last Post: 09-24-2003, 04:58 PM

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