C++ Syntax Question:
Download Job Interview Questions and Answers PDF
How one would use switch in a program?
Answer:
#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 gamen";
cout<<"2. Load gamen";
cout<<"3. Play multiplayern";
cout<<"4. Exitn";
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, quittingn";
break;
}
cin.get();
}</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 gamen";
cout<<"2. Load gamen";
cout<<"3. Play multiplayern";
cout<<"4. Exitn";
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, quittingn";
break;
}
cin.get();
}</iostream>
Download Basic C++ Syntax Interview Questions And Answers
PDF
Previous Question | Next Question |
What is switch case in C++ Syntax? | What is prototype for that C string function? |