Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream> //standard input/output functions
- #include <cstdlib> //used for the rand() function
- #include <ctime> //used for the time() function
- //declare standard namespace
- using namespace std;
- int main(){
- //Define Local Variables
- string driver_name= "NULL";
- string run_program= "NULL";
- int light_color= 0;
- char green_option= 0;
- int red_option= 0;
- int amber_option= 0;
- int lives= 5;
- //begin data collection
- cout<< "Hi, could you please tell me your full name? :";
- cin>> ws;
- getline(cin,driver_name);
- cout<< "Okay, hello " <<driver_name <<"," <<endl;
- cout<< "this program will tell you how good (or bad) of a driver you are." <<endl;
- do{
- //Noted Anomaly. If the string "NULL" is entered, the INVALID INPUT message will not be shown
- //This is not too much of a problem as everything else works as intended.
- if(run_program!="NULL" && run_program!="Y"&& run_program!="N" && run_program!="y" && run_program!="n"){
- cout<< "INVALID INPUT. PLEASE TRY AGAIN!" <<endl;
- }
- cout<<"Would you like to run this program and learn" <<endl;
- cout<< "about your driving habbits? (Y or N): ";
- cin >> run_program;
- }
- while (run_program!="NULL" && run_program!="Y"&& run_program!="N" && run_program!="y" && run_program!="n");
- if (run_program == "Y" || run_program== "y"){ //
- //main program here...
- cout<<"You said yes, great. Lets begin!" <<endl;
- do
- {
- //initialize a random number for the traffic light color
- srand ( time(NULL) ); //init rand() w/ time() as the seed
- light_color = rand() % 3+ 1;
- //begin the conditions for the traffic light color (nested if)
- //Green Light section.
- if (light_color==1){
- cout<< "The light is green, what do you do?" <<endl;
- cout<< "(A). Speed Up" <<endl;
- cout<< "(B). Proceed With Caution" <<endl;
- cout<< "(C). Stop" <<endl;
- cout<< "Answer: ";
- cin>> green_option;
- while(isdigit(green_option)){
- cout<< "\n\n\t *Invalid Input!";
- cout<< "\t *Please enter A,B or C" <<endl;
- cout<< "\n The light is green, what do you do?" <<endl;
- cout<< "(A). Speed Up" <<endl;
- cout<< "(B). Proceed With Caution" <<endl;
- cout<< "(C). Stop" <<endl;
- cout<< "Answer: ";
- cin>> green_option;
- }
- //Give the user a response based on the Answer.
- switch(green_option){
- case 'a': //FALL-THROUGH INTENDED
- case 'A':
- cout<<"There is no need to speed up, just proceed through the light." <<endl;
- lives--;
- cout<<"\n\t** You have: "; cout<<lives;
- cout<< " lives remaining**" <<endl;
- break;
- case 'b': //FALL-THROUGH INTENDED
- case 'B':
- cout<<"Good Choice!" <<endl;
- cout<<"\n\t** You have: "; cout<<lives;
- cout<< " lives remaining**" <<endl;
- break;
- case 'c': //FALL-THROUGH INTENDED
- case 'C':
- cout<<"Are you a dummy?, green means GO!" <<endl;
- lives--;
- cout<<"\n\t** You have: "; cout<<lives;
- cout<< " lives remaining**" <<endl;
- break;
- default:
- cout<< "\n\n\t *Invalid input!," <<endl;
- cout<< "\t use the following" <<endl;
- cout<< "\t options: A,B or C*" <<endl;
- break;
- }
- }
- // Amber Light section.
- else if (light_color==2){
- cout<< "The light is amber, what do you do?" <<endl;
- cout<< "(A). Speed Up" <<endl;
- cout<< "(B). Proceed With Caution" <<endl;
- cout<< "(C). Stop" <<endl;
- cout<< "Answer: ";
- cin>> amber_option;
- while(isdigit(amber_option)){
- cout<< "\n\n\t *Invalid Input!";
- cout<< "\t *Please enter A,B or C" <<endl;
- cout<< "\n The light is amber, what do you do?" <<endl;
- cout<< "(A). Speed Up" <<endl;
- cout<< "(B). Proceed With Caution" <<endl;
- cout<< "(C). Stop" <<endl;
- cout<< "Answer: ";
- cin>> amber_option;
- }
- //Give the user a response based on the Answer.
- switch(amber_option){
- case 'a': //FALL-THROUGH INTENDED
- case 'A':
- cout<<"Speeding up to beat red lights, causes a LOT of accidents." <<endl;
- lives--;
- cout<<"\n\t** You have: "; cout<<lives;
- cout<< " lives remaining**" <<endl;
- break;
- case 'b': //FALL-THROUGH INTENDED
- case 'B':
- cout<<"The light will most likely turn red as you proceed, don't do it." <<endl;
- lives--;
- cout<<"\n\t** You have: "; cout<<lives;
- cout<< " lives remaining**" <<endl;
- break;
- case 'c': //FALL-THROUGH INTENDED
- case 'C':
- cout<<"CORRECT! You should be slowing down and stopping." <<endl;
- cout<<"\n\t** You have: "; cout<<lives;
- cout<< " lives remaining**" <<endl;
- break;
- default:
- cout<< "\n\n\t Invalid input!," <<endl;
- cout<< "\t use the following" <<endl;
- cout<< "\t options: A,B or C" <<endl;
- break;
- }
- }
- //Red Light section.
- else if (light_color==3){
- cout<< "The light is red, what do you do?" <<endl;
- cout<< "(A). Speed Up" <<endl;
- cout<< "(B). Proceed With Caution" <<endl;
- cout<< "(C). Stop" <<endl;
- cout<< "Answer: ";
- cin>> red_option;
- while(isdigit(red_option)){
- cout<< "\n\n\t *Invalid Input!";
- cout<< "\t *Please enter A,B or C" <<endl;
- cout<< "\n The light is amber, what do you do?" <<endl;
- cout<< "(A). Speed Up" <<endl;
- cout<< "(B). Proceed With Caution" <<endl;
- cout<< "(C). Stop" <<endl;
- cout<< "Answer: ";
- cin>> red_option;
- }
- //Give the user a response based on the Answer.
- switch(red_option){
- case 'a': //FALL-THROUGH INTENDED
- case 'A':
- cout<<"What?, Speed up in to oncoming traffic? Are you nuts?" <<endl;
- lives--;
- cout<<"\n\t** You have: "; cout<<lives;
- cout<< " lives remaining**" <<endl;
- break;
- case 'b': //FALL-THROUGH INTENDED
- case 'B':
- cout<<"hmm, Not a fantastic idea no matter how cautious you are." <<endl;
- lives--;
- cout<<"\n\t** You have: "; cout<<lives;
- cout<< " lives remaining**" <<endl;
- break;
- case 'c': //FALL-THROUGH INTENDED
- case 'C':
- cout<<"That would be correct!, Good Job!" <<endl;
- cout<<"\n\t** You have: "; cout<<lives;
- cout<< " lives remaining**" <<endl;
- break;
- default:
- cout<< "\n\n\t Invalid input!," <<endl;
- cout<< "\t use the following" <<endl;
- cout<< "\t options: A,B or C" <<endl;
- break;
- }
- }
- } //end WHILE loop
- while (lives>0);
- }//end main IF statement
- /*
- //Terminating conditions (lives have been depleted) or users says no.
- //to end the program.
- */
- else if (run_program == "N" || run_program== "n"){
- //I guess they don't want to run the program?
- cout<< "You said NO?!" <<endl;
- cout<< "Fair enough, maybe another day." <<endl;
- }
- cout<<"\t****************************" <<endl;
- cout<<"\tYOU ARE DEAD! (learn to drive)" <<endl;
- cout<<"\t****************************" <<endl;
- return 0;
- }
- //end of program.
Advertisement
Add Comment
Please, Sign In to add comment