questions

ok im making a program in dev c++ and i want to make one part of it multiple choice where the user can choose their own path so to speak. i just want to know how to do that
I think you want control structures like if/else.
http://cplusplus.com/doc/tutorial/control/
If you want to make more than 2 choices you'll need the "switch" statement:

switch (cin name that has been wrote before)
{
case 1:
instruction 1 (for example: cout <<"hello"<<endl;)
break;
case 2:
instruction 2
break;
case 3:
instruction 3
break;
};


I've written only 3 choices but you can put many as you want!
you should put a default case in there too, which occurs if none of the other cases are true.

After the final break in the above example do this

1
2
3
default:
// whatever you want to do, eg output an error
break;


EDIT - also this isn't the only way to do it, ordinary if and else statements would be fine as Zhuge said
Last edited on
one thing i forgot to mention is that i just turned 13 and i litteraly just started c++ and i did not understand but about 2 words of what u just said sorry. :-(
in that case learn c++ syntax properly before you do anything else.

http://cplusplus.com/doc/tutorial/
ok thanx for that link and also here is my code if you want to take a look at it (i am of course open to all suggestions)

#include <windows.h>
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;

int main()
{
const int GOLD_PEICES = 900;
int crew, killed, survivors, enemy_army;
string leader, vp, enemy, galaxy, ship_name;
{
cout << "welcome to your space adventure";
cout << "please enter the following so that i can make your adventure \n\n";
cout << "enter a # under 900: " ;
cin >> crew;
cout << "enter a # under 802 and lower than the last # you chose : " ;
cin >> killed;
survivors = crew - killed;
cout << "enter your last name : " ;
cin >> leader;
cout << "enter the last name of an ex boyfriend/girlfriend : " ;
cin >> enemy;
cout << " enter the name of a pet you have or want : " ;
cin >> ship_name;
cout << "enter the funniest word you can think of : " ;
cin >> galaxy;
cout << "enter the last name of your best friend : " ;
cin >> vp;
enemy_army = crew * 3 / 2;
system("PAUSE");
cout << "making story please wait........." ;
_sleep(5000);
system("CLS");
}
// the story

cout << "The year was 2511.\n";
cout << "And the brand new ship, the " << ship_name << "-2 had just set sail. ";
cout << "its mission was to travel the 7 galaxies ";
cout << "and the first one was the most dangerous. ";
cout << "They were going to fly straight through the " << galaxy <<" ";
cout << "galaxy. general " << leader << " was the leader of the ship ";
cout << "and sgt. " << vp << " was 2nd in command ";
cout << "The ship was housing " << crew << " people. ";
cout << "the reason this galaxy was so dangerous is because of " << enemy << " ";
cout << "the giant multicolored space ape. " << enemy << " controles an army " ;
cout << "of " << enemy_army << " space chimpanzees. " ;
cout << "The mission was going good and with succesion. no one had even heard from " << enemy << ". ";
cout << "Just then the ship started shaking. it was a single blast from " << enemy << "'s portable zoo ship";
cout << " its all up to you general. what should we do???";
// first decision. choose to run, defend left, defend right, or defend front?
cout << " ";

system("PAUSE");
}

srry i still dont know how to make a multiple choice for this.
You should walk through the tutorial on this site before attempting a large project. What you need to use is either a switch structure or if..else statements. For example, let's say your player has the following choices promopted:

1. Run
2. Defend Left
3. Defend Right
4. Defend Front

Enter your choice:

You can simply create an int variable, call it int choice;
1
2
3
4
5
6
if ( choice == 1 )
    // do something
else if ( choice == 2 )
    // do something
else if ( choice == 3 )
    // do something 


Make sense? You will also need to prompt until a valid number was entered. So start here http://www.cplusplus.com/doc/tutorial/control/ as zhuge recommended.
Last edited on
Topic archived. No new replies allowed.