Thread: invalid user entry

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    4

    Question invalid user entry

    I am using VC++ and am trying to figure out how to do a loop such that when the user enters a char of str the program will display error and ask the user to retry. I have it working for numbers but dont know how to solve the other invalid entry of letters. Any ideas?

    Code:
    void menu (int &selection)
    {
    	cout << "     Available Currencies Menu" << endl << endl;	//Display Menu
    	cout << "1. British Pounds" << endl;
    	cout << "2. Canadian Dollars" << endl;
    	cout << "3. Indian Rupees" << endl;
    	cout << "4. Japanese Yens" << endl;
    	cout << "5. Mexican Pesos" << endl << endl;
    	
    	cout << "Please enter your selection number from the list above: # ";	//user enters menu selection
    	cin >> selection;
    	
    	//validate selection
    	while (selection < 1 || selection > 5)				//user entry validated
    	{
    		cout << "Invalid Entry! Please Enter Number Between 1-5" << endl;	//if invalid, error displayed
    		cout << "Please try again: # ";		//user enters menu selection
    		cin >> selection;
    	}//end while
    	
    }//end menu selection function

  2. #2
    Registered User Sekti's Avatar
    Join Date
    Feb 2002
    Posts
    163

    maybe try

    do
    if(selection < 1 && selection > 5)
    {
    work
    }
    else
    {
    dont work
    }

    just a geuss see if it works
    +++
    ++
    + Sekti
    ++
    +++

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Make the selection variable a char, then just use similar valiadation code:
    Code:
    while ( selection >= '1' && '5' >= selection ) {
      // Process
    }
    The way this works is that cin will always enter a char value, so you test that char value with the range of '1' - '5' instead of the integer values 1 - 5. If anything but '1', '2', '3', '4', or '5' is entered, the input is invalid.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    4
    Didn't work I think because the selection points to an array value that is const ????

    I thought that if i specified what the valid selections that anything other than those would give error irregardless of what the entry is. I guess it all part of the learning process.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Database entry - avoiding duplicate records
    By ChadJohnson in forum Tech Board
    Replies: 2
    Last Post: 02-04-2006, 12:43 AM
  2. Newbie Help: Currency Converter
    By Ashfury in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 01:21 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Invalid conversion from 'const void*' to 'void*' error
    By prawntoast in forum C Programming
    Replies: 3
    Last Post: 05-01-2005, 10:30 AM
  5. Replies: 6
    Last Post: 04-12-2002, 08:33 AM