#include <iostream>
using namespace std;
void playgame();
void loadgame();
void playmultiplayer();
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();
}
playgame() {
cout << "You are playing the game\n";
}
loadgame() {
cout << "You have loaded the game\n";
}
playmultiplayer() {
cout << "You are playing multiplayer\n";
}
Switch cases exampleJust trying to get a quick basic understanding
Page 1 of 1
3 Replies - 20960 Views - Last Post: 14 October 2008 - 10:31 PM
#1
Switch cases example
Posted 14 October 2008 - 09:14 PM
I know this code is wrong. No need to tell me. I'm just trying to get a basic idea on how it would work for this. I want the program to call another part of the code depending on which option they choose. For example, if they were to choose 1, then it would display "You are now playing the game", so it would go down to the section of code playgame() and start reading from there. Let me know if you don't quite understand what I mean and I will try and reword it. I'm making it as clear as I can.
Replies To: Switch cases example
#2
Re: Switch cases example
Posted 14 October 2008 - 09:18 PM
The code looks right to me...
#3
Re: Switch cases example
Posted 14 October 2008 - 09:34 PM
It looks pretty good to me, too. It would not compile until I added a type to your functions, but then it worked fine.
void playgame() {
cout << "You are playing the game\n";
}
void loadgame() {
cout << "You have loaded the game\n";
}
void playmultiplayer() {
cout << "You are playing multiplayer\n";
}
#4
Re: Switch cases example
Posted 14 October 2008 - 10:31 PM
Sorry for the complete change ups, this is my first project that I actually somewhat understand how to do, and keep trying different things with it. I'm super stoked. Anyways, I did get it to work. I changed it to use 'y' or 'n' to determine where to go from there. The file attached is the description on what I am trying to do(I have M$ Office Ultimate 2007, I have it as a doc compatable with 97-2003, so hopefully you guys can view it so you know what I'm doing). If anyone could give me input on what parts of code to use, that would be great. I don't want you to tell me how to write my code to finish it, just maybe hints on stuff to use(example, maybe I should look up 'for' and 'while', figure out how to use them and put them in the code?) I have no idea, I'm just showing what I mean by hints.
#include <iostream>
using namespace std;
void noequip();
void equip();
int main()
{
char input;
cout << "Do you need equipment?";
cout << "Y/N:";
int nobreak=1;
while (nobreak) {
cin>> input;
switch (input) {
case 'n':
case 'N': {
cout << "You have chosen no"<< endl;
noequip();
break;
}
case 'y':
case 'Y': {
cout << "You have chosen yes"<< endl;
nobreak=0;
equip();
break;
}
}
}
return 0;
}
void noequip(void) {
int numStudents;
cout << "How many students do you have?\n";
cin >> numStudents;
if (numStudents <= 10) {
cout << "class assigned to tiny classroom";
}
else if (numStudents > 10 && numStudents <=40)
{
cout << "class assignmed to small classroom";
}
else {
cout << "no suitable classroom available";
}
}
void equip(void) {
int numStudents;
cout << "How many students do you have?\n";
cin >> numStudents;
if (numStudents <= 100 && numStudents > 40) {
cout << "class assigned to medium classroom";
}
else if (numStudents > 100 && numStudents <= 1000)
{
cout << "class assignmed to large classroom";
}
else {
cout << "no suitable classroom available";
}
}
This post has been edited by bbarker3: 14 October 2008 - 10:36 PM
Page 1 of 1

New Topic/Question
Reply



MultiQuote




|