Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,120 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,765 people online right now. Registration is fast and FREE... Join Now!




Validation

 
Reply to this topicStart new topic

Validation, Preventing the user from entering a char when a number is needed

Reedo321
17 Apr, 2007 - 10:51 AM
Post #1

New D.I.C Head
*

Joined: 27 Mar, 2007
Posts: 5


My Contributions
Hi guys i wonder if u can help me im just tryin to create a simple menu using a switch function where the user can select a value from the list and go to that case. Ive got that working fine but i want it to validate that the number input by the user is within the values of the menu and that its not a char, so far ive got:

CODE
#include <iostream>
using namespace std;

const char BEEP = '\a';

void main ()
{

        int selection;

        system("cls");
        cout << endl;
        cout << "\t\t\t\t\Menu\n\n";
        cout << "Welcome to menu...\n\n";
        cout << "\t1 = Scenario 1\n";
        cout << "\t2 = Scenario 2\n";
        cout << "\t3 = Scenario 3\n";
        cout << "\t0 = Scenario 0\n\n";
        cout << "Please select an option from the menu: ";
        cin  >> selection;

        while((!cin.good()) || (selection <0) || (selection >3))
        {
                cin.clear();
                cout << BEEP <<"\nError! Please select a value from the menu (0-3): ";
                cin.get();
                cin  >> selection;
                
        }
        switch (selection)
                {
                case 0:
                        system("cls");
                        cout << "\nScenario 0\n\n";
                        system("pause");
                        break;
                case 1:
                        system("cls");
                        cout << "\nScenario 1\n\n";
                        system("pause");
                        break;
                case 2:
                        system("cls");
                        cout << "\nScenario 2\n\n";
                        system("pause");
                        break;

                case 3:
                        system("cls");
                        cout << "\nScenario 3\n\n";
                        system("pause");
                        break;
                }
}


This works but if u enter more than one character then it loops around the error more than once, does anyone no how to prevent it doin this?

Cheers guys

This post has been edited by Reedo321: 17 Apr, 2007 - 10:52 AM
User is offlineProfile CardPM
+Quote Post

GWatt
RE: Validation
17 Apr, 2007 - 11:11 AM
Post #2

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,178



Thanked: 18 times
Dream Kudos: 450
My Contributions
You can read the entire line in as a string getline(cin, string) and take the first character in the string. You can also provide a default case in your switch statement, and print out some mean error message.
User is online!Profile CardPM
+Quote Post

Reedo321
RE: Validation
17 Apr, 2007 - 11:38 AM
Post #3

New D.I.C Head
*

Joined: 27 Mar, 2007
Posts: 5


My Contributions
Cheers GWatt i'll have a try at somethin along those lines. Have u any idea y its skippin my original cin the first time and only stoppin for input on the nth iteration where n is the number of chars entered? surely tho as soon as it goes into the while loop then it should clear the input stream thus stoppin for input at the cin?!?

Cheers
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:02PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month