Switch Case question (from C++ Tutorial Example)

This is a discussion on Switch Case question (from C++ Tutorial Example) within the C++ Programming forums, part of the General Programming Boards category; Hi, In working through t the Switch Case example in the C++ tutorial I wanted to try and have the ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    2

    Switch Case question (from C++ Tutorial Example)

    Hi,
    In working through t the Switch Case example in the C++ tutorial I wanted to try and have the list use characters (a,b,c,d) rather than numbers (1,2,3,4) as the the case identifiers.

    Here is the code (in case you forgot:-)
    Code:
    #include <iostream>
    
    using namespace std;
    
    void playgame()
    {
        cout << "Play game called";
    }
    void loadgame()
    {
        cout << "Load game called";
    }
    void playmultiplayer()
    {
        cout << "Play multiplayer game called";
    }
    	
    int main()
    {
      int input;
      
      cout<<"1. Play game\n";
      cout<<"2. Load game\n";
      cout<<"3. Play multiplayer\n";
      cout<<"4. Exit\n";
      cout<<"Selection: ";
      cin>> input;
      switch ( input ) {
      case 1:            // Note the colon, not a semicolon
        playgame();
        break;
      case 2:            // Note the colon, not a semicolon
        loadgame();
        break;
      case 3:            // Note the colon, not a semicolon
        playmultiplayer();
        break;
      case 4:            // Note the colon, not a semicolon
        cout<<"Thank you for playing!\n";
        break;
      default:            // Note the colon, not a semicolon
        cout<<"Error, bad input, quitting\n";
        break;
      }
      cin.get();
    }
    I tried changing "input" from int to char with no joy. I also tried just changing the numbers to letters in case with the same predictable results. Is there a library I need to declare or am I on a fools errand?

    TIA

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    5,863
    Don't show the code you started with. That is not the code causing you problems. Show the code you have actually modified so input is of type char, and then compares input with a,b,c ....

    Hint: if you want a switch on the letter 'a', then the relevant case in the switch block looks like
    Code:
    case 'a' :
    Note the single quotes.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    2
    Quote Originally Posted by grumpy View Post
    Don't show the code you started with. That is not the code causing you problems. Show the code you have actually modified so input is of type char, and then compares input with a,b,c ....

    Hint: if you want a switch on the letter 'a', then the relevant case in the switch block looks like
    Code:
    case 'a' :
    Note the single quotes.
    Thanks!
    You answered my question. I'd forgotten/didn't know about the single quotes when using chars. I need to get a book in addition to the tutorial it seems.
    Last edited by dacart; 08-15-2011 at 08:01 AM. Reason: grammar

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C tutorial Switch..case loop
    By dunsta in forum C Programming
    Replies: 8
    Last Post: 04-18-2010, 04:55 AM
  2. another newbie question (switch...case)
    By Terran in forum C++ Programming
    Replies: 11
    Last Post: 05-23-2008, 04:07 PM
  3. switch case question
    By dantegl36 in forum C Programming
    Replies: 2
    Last Post: 12-04-2006, 10:12 AM
  4. Switch Case Question
    By dnysveen in forum C++ Programming
    Replies: 12
    Last Post: 06-06-2006, 05:34 AM
  5. switch() case 1: Question
    By Zero_X in forum C++ Programming
    Replies: 0
    Last Post: 04-18-2006, 12:33 PM

Tags for this Thread


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