Ninjakreborn Posted January 31, 2010 Share Posted January 31, 2010 I am totally lost here... 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; break; } Very straightforward. Prevent choice..based off their choices perform actions...now what happens if they perform an invalid choice. It reloads the choices..but then exits out of the system. From a logic standpoint..how would I present the list of choices again, as well as let the switch statement take affect again when a choice was selected the second time..In PHP for example your able to reshow a form if it's been entered incorrectly and still submit...do I have to put that switch statement into a function as well...but there are going to be tons of switch statements throughout this entire application..I am wondering if I have to have a separate one for each set of choices? Any advice on this issue? Link to comment https://forums.phpfreaks.com/topic/190431-c-how-to-manage-the-logic/ Share on other sites More sharing options...
Mchl Posted January 31, 2010 Share Posted January 31, 2010 Usually you do this with while() 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; default: std::cout<<"Invalid Choice"; std::cout<<choices; break; } } Link to comment https://forums.phpfreaks.com/topic/190431-c-how-to-manage-the-logic/#findComment-1004554 Share on other sites More sharing options...
Ninjakreborn Posted January 31, 2010 Author Share Posted January 31, 2010 Wait..I am very, very confused now. I thought a C++ application was like a PHP application...Meaning it went line by line and ran the code as it went. Now based off this code...it works differently? I got a little lost here. However I implemented your code, and it runs it forever..like the while loop keeps going eternally and reshowing the choices over and over again very rapidly. Not to mention when I do it like this: 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(); } It does work to an extent...however it doesn't reflect the choices..so the first time if they select 6, which is an invalid choice.. then they select 1 it still goes as an invalid choices...it doesn't replay that switch statement. If that makes any sense. I was afraid of that, I know the while look will allow me to reshow the same choices but won't let me rerun the same switch statement. Link to comment https://forums.phpfreaks.com/topic/190431-c-how-to-manage-the-logic/#findComment-1004608 Share on other sites More sharing options...
Mchl Posted January 31, 2010 Share Posted January 31, 2010 Reading from cin should also be within while loop bool validChoice = false; while(!validChoice) { std::cin>>response; 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(); } Link to comment https://forums.phpfreaks.com/topic/190431-c-how-to-manage-the-logic/#findComment-1004609 Share on other sites More sharing options...
Ninjakreborn Posted January 31, 2010 Author Share Posted January 31, 2010 Perfect, thanks for the feedback. Link to comment https://forums.phpfreaks.com/topic/190431-c-how-to-manage-the-logic/#findComment-1004771 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.