I am having a little bit of an issue here. I worked with C++ for 6 months..stepped away for awhile and came back. Now I am back to where I left off...but recently ran into a difficult issue related to how the Logic flow works.
OK...let me make the example simple because it's hard to explain. Say I have a list of choices presented to the user...they have 4 choices...1, 2, 3, and 4. I have a switch statement below those choices which decide what happens based off of their actions. If they choose 1-4 something happens..if they choose anything else it defaults to a default action..and in theory I would like to reshow the choices and perform the same switch statement when they make a new choice.
That all is really simple in theory...but it seems not simple when trying to implement it.
I have two implementations here:
This is my first implementation..logically it's impossible to work..because I can't replay the switch statement again..or make it rerun.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
// Start the story
std::cout<<"This year is 1983, the place is Atlanta, Georgia. Your name is "<<firstname<<" "<<lastname<<". "
<<"Your recently turned 18 years of age, and have gotten very bored with your life. You have spent the past 5 years "
<<"trying to become a member of either the police force, a military force, or some type of governmental agency. "
<<"Lately it seems as if you might have just gotten lucky. You recently received a letter in the mail from the police force "
<<"about an application you filled out with them months ago. This could be the break you have been waiting for "
<<"With this opprotunity that has been presented to you, what are you going to do?"<<std::endl<<std::endl;
std::string choices = "You can:\n1: Go to the police station.\n2: Call the police station.\n3: Throw away the mail you got from the police station.\n\n";
std::cout<<choices;
int response;
std::cin>>response;
std::cin.ignore();
switch(response) {
case 1:
std::cout<<"go to police station";
break;
case 2:
std::cout<<"call the police station";
break;
case 3:
std::cout<<"With a heavy heart you realize you just are not cut out to be a police officer. You crumble the letter up, roll it up into a"
<<" ball and throw it into the garbage.";
exit(1);
break;
default:
std::cout<<"Invalid Choice";
std::cout<<choices;
}
|
The second implementation is below..this was suggested by someone else who was trying to give me advice..however without that get at the bottom it just replays the choices over and over again rapidly and never stops...with the get at the bottom it actually only shows the choices once...but for some reason it does not take the action..I don't really see how the bottom code can work from a logic standpoint anyway as I don't see how C++ can go back up and re-run the switch statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
// Start the story
std::cout<<"This year is 1983, the place is Atlanta, Georgia. Your name is "<<firstname<<" "<<lastname<<". "
<<"Your recently turned 18 years of age, and have gotten very bored with your life. You have spent the past 5 years "
<<"trying to become a member of either the police force, a military force, or some type of governmental agency. "
<<"Lately it seems as if you might have just gotten lucky. You recently received a letter in the mail from the police force "
<<"about an application you filled out with them months ago. This could be the break you have been waiting for "
<<"With this opprotunity that has been presented to you, what are you going to do?"<<std::endl<<std::endl;
std::string choices = "You can:\n1: Go to the police station.\n2: Call the police station.\n3: Throw away the mail you got from the police station.\n\n";
std::cout<<choices;
int response;
std::cin>>response;
std::cin.ignore();
bool validChoice = false;
while(!validChoice) {
switch(response) {
case 1:
std::cout<<"go to police station";
validChoice = true;
break;
case 2:
std::cout<<"call the police station";
validChoice = true;
break;
case 3:
std::cout<<"With a heavy heart you realize you just are not cut out to be a police officer. You crumble the letter up, roll it up into a"
<<" ball and throw it into the garbage.";
exit(1);
break;
default:
std::cout<<"Invalid Choice";
std::cout<<choices;
}
std::cin.get();
}
|
Now there is my problem, as well as both implementations I have attempted. That leaves me with the problem. Basically...how can I make this happen the way I am wanting..because in theory I would like to do this exact same thing about 200 more times or so roughly in this same application..because it's going to be a game (I am building just for learning purposes for now). So..I thought even about putting another switch inside the switch but that's useless..it'll require me to do that infinitely..so I thought about putting that entire choice select and switch into a separate function..that's useless to because I am still left with the same problem.
Any advice on how to sort this out from a logical standpoint would be GREATLY appreciated..all other issues I ran across I was able to put together by reading stuff online, but this one really has me totally stumped..this would serve as a gateway for me to be able to figure out other logic control structures, as well as help me when I get deeper into the application.