-
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?
-
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.
-
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.