Thread: Questions on Switch Case

  1. #1
    Registered User
    Join Date
    Feb 2013

    Questions on Switch Case

    Hey everyone. I'm a new programmer and I have a question about switch case or maybe it has more to do with functions in general. I was reading lesson 5 of the tutorial and i was presented with this block of 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     
    case 2:            // Note the colon, not a semicolon     
    case 3:            // Note the colon, not a semicolon     playmultiplayer();     
    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";     
    The tutorial stated that the code would compile but not run because the functions were not declared but as I went through the code, I could see any reason why it would not run so I tried to run it and it failed. My question is why would the program not run? It seems the functions that are called upon in the case statement were defined in the beginning of the code. The void playgame() function prints out "Play game called:" on the screen and etc. Is there something I'm just not getting?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    The tutorial would appear to be wrong.

    On the plus side, you didn't accept what was written at face value, and tried it for yourself - this is a good thing! - keep it up.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Thats strange. The first few times i tried to run the code. It didn't work. After reading your post i tried it again and now it suddenly works. I've made no changes. I was having problems like that with other code as well. Maybe there is something wrong with my compiler. I'm running CodeLite. I have a large process running in the background. Maybe thats causing this strange problem. Anyway, thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch/Case questions
    By kbzombie in forum C++ Programming
    Replies: 11
    Last Post: 04-07-2010, 01:24 PM
  2. Replies: 11
    Last Post: 08-25-2008, 12:01 PM
  3. switch case
    By lilhawk2892 in forum C++ Programming
    Replies: 18
    Last Post: 09-21-2005, 08:23 PM
  4. Some help with switch case bug...
    By hykyit in forum C Programming
    Replies: 5
    Last Post: 05-18-2005, 03:04 AM
  5. hii..switch and case
    By cBegginer in forum C Programming
    Replies: 1
    Last Post: 05-16-2005, 01:45 PM