Thread: Switch function problem

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    12

    Switch function problem

    Hi all

    I've only just started using the C++ language recently and I'm stuck on something which should be pretty easy but I can't solve.

    option is defined as char option[80]. My main problem is that if I enter an option of 9, "PLEASE TRY AGAIN" comes up. I want this to happen if say 22 is entered as well but currently Case 2 is selected, not the default. Why is it doing this and how would I sort it?

    Code:
    do {
              cout << "\n     Products\n";
              cout << "       1.  New\n";
              cout << "       2.  Old\n";
              cout << "       3.  Discontinued\n";
              cout << "       q.  End menu\n";
              cout << "\n\n";
              cout << "\n     Press appropriate key to select > ";
         
         gets(option);	
         
          switch (option[0]) {
                   case '1':
                             new(items);
                             key();
                             break;
                   case '2':
                             old(items);
                             key();
                             break;
                   case '3': 
                             discontinued(items);
                             key();
                             break;
                   case 'q':
                             cout <<("\n\t\t Returning to main.\n");
                             key();
                             break;
                   default:
                             cout <<("\t\t PLEASE TRY AGAIN\n");
                             key();
                             break;
                                      }    //end switch
                      }//end do
    
                             while (option[0] != 'q'  );
                             break;
    Last edited by Jack-Bauer; 05-14-2006 at 10:45 AM. Reason: spelling

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Very simply it's because the switch statement is only looking
    at the first element of your string. The second digit in "22" is
    strored at the element option [1]. It would be better to use an
    integer for this task, and change your 'q' case to 0.

    Also, see my sig - use cin.getline to read strings in C++. Also,
    new is a reserved word in C++, so your function called new won't
    be allowed.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    12
    I've read through some of the pages in your sig but I still don't get it.

    The 'q' part works fine, it does it when its supposed to. Problem is how would I get around the fact the switch statement only looks in the first element and not the entire string?

  4. #4
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    >>I've read through some of the pages in your sig but I still don't
    get it.

    I didn't put a link for cin.getline in my sig, but do look at the link
    about gets - its an old function that can easily crash your program
    if a person enters too much data. The reason I have an fgets link
    is because people don't tend to use gets in C++, its a C function
    and fgets is the more modern version.

    >>The 'q' part works fine

    I'm sure it does, i wasn't contesting that it wouldn't. The reason
    i suggested using integers is to get around my next point (in
    using integers, entering q would obviously be invalid).

    >>how would I get around the fact the switch statement only
    looks in the first element and not the entire string?

    You cant - at least not with a switch statement. You could make
    this program work using an if/else block and using strcmp.

    Code:
    if (!strcmp(option, "1"))
    {
        //do stuff
    }
    
    else if (!strcmp(option, "2"))
    {
        //do stuff
    }
    
    else if (!strcmp(option, "3"))
    {
        //do stuff
    }
    
    else if (!strcmp(option, "q"))
    {
        //do stuff
    }
    
    else
    {
        //do default stuff
    }
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    12
    Hey thanks, that bit of code worked a charm.

    Another question though, because that bit of code I posted at the start is a part of a nested function, I need the while (option[0] != 'q' ); after your bit of code. I can get that to work, but I was wondering is there any way possible to say option[0] != 'q' and 'Q'?

    I'm understand now why you said it was best to use intergers
    Last edited by Jack-Bauer; 05-14-2006 at 12:47 PM.

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Yes, There is a toupper() and tolower function ...

    You can say:

    Code:
    while (  tolower( variable ) != 'q' )
    {
        // insert things here
    }
    of you could always do:

    Code:
    while (  variable != 'q' || variable != 'Q')
    {
        // insert things here
    }
    You can look here ... of the tolower thing there, or is it toupper? I can't remember. Also, the || thing in the while statement means or, so it psedo code reads:

    while (variable doesn't equal small q) OR (variable doesn't equal capital Q) etc.

    >> Also, see my sig - use cin.getline to read strings in C++.
    haha
    Last edited by twomers; 05-14-2006 at 01:54 PM.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    12
    its working as I want it now, thanks for all your help, that last bit solved ma problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM