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

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




switch

2 Pages V  1 2 >  
Reply to this topicStart new topic

switch, check if the user enters a letter instead of a number?

Manny
6 May, 2007 - 12:47 PM
Post #1

New D.I.C Head
*

Joined: 30 May, 2006
Posts: 44


My Contributions
Hi All:

blink.gif

I need some help. How can I check if the user enters a letter instead of a number?
With in the switch statement

CODE


void showMainMenu()
{

    int choice;

    if ( fullName == "" )
        fullName = Getname();

    do
    {    
        cout << endl << endl;
        cout << "\tWelcome " << fullName << "," << " to Samsung Main menu.\n";
        cout << "\tPlease Enter a menu number: 1, 2, 3, 4, or 5 to exit.\n";
        cout << endl;
        cout << "\t1) Movie Menu        2) Music Menu\n";
        cout << "\t3) alibi        4) comfort\n";
        cout << "\t5) quit\n";
        cout << endl << endl;

        cout << "\tPlease enter Main Menu option: ";
        cin >> choice;

        switch (choice)
        {

            case 1 : showMovieMenu();
                 break;
            case 2 : music();
                 break;
            case 3 : cout << "\tThe boss was in all day.";
                 break;
            case 4 : comfort();
                 break;
            case 5 : break;

            default : printf("\n\t\a%c[%d;%dmThat's not a good choice ", 0x1B, BRIGHT, RED, BG_BLACK);
                  printf("%c[%dm", 0x1B, 0);
                  cout << fullName;
                  printf(", %c[%d;%dmtry agin.\n", 0x1B, BRIGHT, RED, BG_BLACK);
                  printf("%c[%dm", 0x1B, 0);

        }


    }

    while (choice != 5);

    cout << endl << endl;    
    cout << "\tThank you " << fullName << ", " << "for using Samsungs,\n";
    cout << "\tmove and music tool.\n\n\n";


}



smile.gif

User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Switch
6 May, 2007 - 12:50 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 3,984



Thanked: 16 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
you can add a default case which will handle any other input, and have it call back to start the menu over. Something like:
default: showMainMenu();
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Switch
6 May, 2007 - 01:02 PM
Post #3

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
There are a variety of things you can do...the default case suggested by William_Wilson is a great option...or you could also use the isdigit() function to validate that an digit has been entered instead of an alpha character.
User is offlineProfile CardPM
+Quote Post

Manny
RE: Switch
6 May, 2007 - 01:07 PM
Post #4

New D.I.C Head
*

Joined: 30 May, 2006
Posts: 44


My Contributions
QUOTE(Amadeus @ 6 May, 2007 - 02:02 PM) *

There are a variety of things you can do...the default case suggested by William_Wilson is a great option...or you could also use the isdigit() function to validate that an digit has been entered instead of an alpha character.


QUOTE

I have tried both options with no luck

Help

blink.gif


User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Switch
6 May, 2007 - 01:13 PM
Post #5

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
The problem is that you need to identify the error input before the switch statement. Try the .good() method of the cin operator. It will check to see if an error flags are set..if not, no problem.

Just remember to reset the flags on failure with a cin.clear().


User is offlineProfile CardPM
+Quote Post

Manny
RE: Switch
6 May, 2007 - 03:22 PM
Post #6

New D.I.C Head
*

Joined: 30 May, 2006
Posts: 44


My Contributions
QUOTE(Manny @ 6 May, 2007 - 02:07 PM) *

QUOTE(Amadeus @ 6 May, 2007 - 02:02 PM) *

There are a variety of things you can do...the default case suggested by William_Wilson is a great option...or you could also use the isdigit() function to validate that an digit has been entered instead of an alpha character.


QUOTE

I have tried both options with no luck

Help

blink.gif




Amadeus

This worked just fine thank you

now to tern this in to a class

biggrin.gif

icon_up.gif

QUOTE


The problem is that you need to identify the error input before the switch statement. Try the .good() method of the cin operator. It will check to see if an error flags are set..if not, no problem.

Just remember to reset the flags on failure with a cin.clear().



CODE


void showMainMenu()
{
    
    char ch;
    int choice;

    if ( fullName == "" )
        fullName = Getname();

    do
    {    
        cout << endl << endl;
        cout << "\tWelcome " << fullName << "," << " to Samsung Main menu.\n";
        cout << "\tPlease Enter a menu number: 1, 2, 3, 4, or 5 to exit.\n";
        cout << endl;
        cout << "\t1) Movie Menu        2) Music Menu\n";
        cout << "\t3) alibi        4) comfort\n";
        cout << "\t5) quit\n";
        cout << endl << endl;

        cout << "\tPlease enter Main Menu option: ";
        cin >> choice;

        while (!cin.good())
        {
            cin.clear();
            
            printf("\n\t\a%c[%d;%dmYou entered a letter ", 0x1B, BRIGHT, RED, BG_BLACK);
            printf("%c[%dm", 0x1B, 0);
            cout << fullName;
            printf(", %c[%d;%dmEnter a number from (1 - 4) try agin.\n\n", 0x1B, BRIGHT, RED, BG_BLACK);
            printf("%c[%dm", 0x1B, 0);
            cin.get();

            cout << "\tPlease enter your Main Menu option: ";
            cin >> choice;
        }
        
        switch (choice)
        {

            case 1 : showMovieMenu();
                 break;
            case 2 : music();
                 break;
            case 3 : cout << "\tThe boss was in all day.";
                 break;
            case 4 : comfort();
                 break;
            case 5 : break;

            default :
                  printf("\n\t\a%c[%d;%dmThat's not a good choice ", 0x1B, BRIGHT, RED, BG_BLACK);
                  printf("%c[%dm", 0x1B, 0);
                  cout << fullName;
                  printf(", %c[%d;%dmtry agin.\n", 0x1B, BRIGHT, RED, BG_BLACK);
                  printf("%c[%dm", 0x1B, 0);

        }


    }

    while (choice != 5);

    cout << endl << endl;    
    cout << "\tThank you " << fullName << ", " << "for using Samsungs,\n";
    cout << "\tmove and music tool.\n\n\n";
}




User is offlineProfile CardPM
+Quote Post

Flash0429
RE: Switch
8 May, 2007 - 04:52 PM
Post #7

New D.I.C Head
*

Joined: 8 May, 2007
Posts: 15


My Contributions
is there a way to check a string with a switch statement
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Switch
8 May, 2007 - 06:15 PM
Post #8

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
Not in any native c++ method. You could program a class to do it, however.
User is offlineProfile CardPM
+Quote Post

Flash0429
RE: Switch
9 May, 2007 - 08:33 AM
Post #9

New D.I.C Head
*

Joined: 8 May, 2007
Posts: 15


My Contributions
QUOTE(Amadeus @ 8 May, 2007 - 07:15 PM) *

Not in any native c++ method. You could program a class to do it, however.


how do you do that im a bit new at this and i have a program that im makin but im stuck at one part i sotof figured out how to do it but i need the swith to use string the problem is in the other poast that i have call need help with functions plz
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Switch
9 May, 2007 - 08:52 AM
Post #10

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
If you are new to C++, I might suggest that writing a class and methods to do this may not be the best option. Is there a reason you need to use the switch statement, as opposed to an an if else if construct?
User is offlineProfile CardPM
+Quote Post

Flash0429
RE: Switch
10 May, 2007 - 08:11 AM
Post #11

New D.I.C Head
*

Joined: 8 May, 2007
Posts: 15


My Contributions
QUOTE(Amadeus @ 9 May, 2007 - 09:52 AM) *

If you are new to C++, I might suggest that writing a class and methods to do this may not be the best option. Is there a reason you need to use the switch statement, as opposed to an an if else if construct?


well if you look at the other post that i posted i felt that a switch would be the easiest way to do this having them input the month then using a switch statement to check it and return the stone youll see if u take a look at that post
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Switch
10 May, 2007 - 09:07 AM
Post #12

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
Link me to the post, and I'll have a look.Long story short, you cannot use the switch statement with a string, only integers and chars. You could write a class to handle it, but inside the class, it would still be a version of the if else if construct.
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 12/1/08 09:42PM

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