Answers

Question and Answer:

  Home  Basic C++ Syntax

⟩ How one would use switch in a program?

#include

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();

}

 217 views

More Questions for you: