Questions on Switch Case

This is a discussion on Questions on Switch Case within the C++ Programming forums, part of the General Programming Boards category; Hey everyone. I'm a new programmer and I have a question about switch case or maybe it has more to ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    11

    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:

    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(); 
    }
    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
    Location
    The edge of the known universe
    Posts
    32,855
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    11
    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, 02:24 PM
  2. Replies: 11
    Last Post: 08-25-2008, 01:01 PM
  3. switch case
    By lilhawk2892 in forum C++ Programming
    Replies: 18
    Last Post: 09-21-2005, 09:23 PM
  4. Some help with switch case bug...
    By hykyit in forum C Programming
    Replies: 5
    Last Post: 05-18-2005, 04:04 AM
  5. hii..switch and case
    By cBegginer in forum C Programming
    Replies: 1
    Last Post: 05-16-2005, 02:45 PM

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