Guest User

xdega

a guest
Oct 3rd, 2011
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.40 KB | None | 0 0
  1. #include <iostream> //standard input/output functions
  2. #include <cstdlib> //used for the rand() function
  3. #include <ctime> //used for the time() function
  4.  
  5. //declare standard namespace
  6. using namespace std;
  7.  
  8. int main(){
  9. //Define Local Variables
  10. string driver_name= "NULL";
  11. string run_program= "NULL";
  12. int light_color= 0;
  13. char green_option= 0;
  14. int red_option= 0;
  15. int amber_option= 0;
  16. int lives= 5;
  17.  
  18. //begin data collection
  19. cout<< "Hi, could you please tell me your full name? :";
  20. cin>> ws;
  21. getline(cin,driver_name);
  22. cout<< "Okay, hello " <<driver_name <<"," <<endl;
  23. cout<< "this program will tell you how good (or bad) of a driver you are." <<endl;
  24.  
  25. do{
  26.     //Noted Anomaly. If the string "NULL" is entered, the INVALID INPUT message will not be shown
  27.     //This is not too much of a problem as everything else works as intended.
  28.  
  29.     if(run_program!="NULL" && run_program!="Y"&& run_program!="N" && run_program!="y" && run_program!="n"){
  30.     cout<< "INVALID INPUT. PLEASE TRY AGAIN!" <<endl;
  31.     }
  32. cout<<"Would you like to run this program and learn" <<endl;
  33. cout<< "about your driving habbits? (Y or N): ";
  34. cin >> run_program;
  35. }
  36. while (run_program!="NULL" && run_program!="Y"&& run_program!="N" && run_program!="y" && run_program!="n");
  37.  
  38.     if (run_program == "Y" || run_program== "y"){ //
  39.     //main program here...
  40.     cout<<"You said yes, great. Lets begin!" <<endl;
  41.  
  42.     do
  43.     {
  44.             //initialize a random number for the traffic light color
  45.             srand ( time(NULL) ); //init rand() w/ time() as the seed
  46.             light_color = rand() % 3+ 1;
  47.             //begin the conditions for the traffic light color (nested if)
  48.  
  49.             //Green Light section.
  50.             if (light_color==1){
  51.             cout<< "The light is green, what do you do?" <<endl;
  52.             cout<< "(A). Speed Up" <<endl;
  53.             cout<< "(B). Proceed With Caution" <<endl;
  54.             cout<< "(C). Stop" <<endl;
  55.             cout<< "Answer: ";
  56.             cin>> green_option;
  57.                 while(isdigit(green_option)){
  58.                 cout<< "\n\n\t *Invalid Input!";
  59.                 cout<< "\t *Please enter A,B or C" <<endl;
  60.                  cout<< "\n The light is green, what do you do?" <<endl;
  61.                 cout<< "(A). Speed Up" <<endl;
  62.                 cout<< "(B). Proceed With Caution" <<endl;
  63.                 cout<< "(C). Stop" <<endl;
  64.                 cout<< "Answer: ";
  65.                 cin>> green_option;
  66.                 }
  67.                 //Give the user a response based on the Answer.
  68.                 switch(green_option){
  69.  
  70.                 case 'a': //FALL-THROUGH INTENDED
  71.                 case 'A':
  72.                 cout<<"There is no need to speed up, just proceed through the light." <<endl;
  73.                 lives--;
  74.                 cout<<"\n\t** You have: "; cout<<lives;
  75.                 cout<< " lives remaining**" <<endl;
  76.                 break;
  77.  
  78.                 case 'b': //FALL-THROUGH INTENDED
  79.                 case 'B':
  80.                 cout<<"Good Choice!" <<endl;
  81.                 cout<<"\n\t** You have: "; cout<<lives;
  82.                 cout<< " lives remaining**" <<endl;
  83.                 break;
  84.  
  85.                 case 'c': //FALL-THROUGH INTENDED
  86.                 case 'C':
  87.                 cout<<"Are you a dummy?, green means GO!" <<endl;
  88.                 lives--;
  89.                 cout<<"\n\t** You have: "; cout<<lives;
  90.                 cout<< " lives remaining**" <<endl;
  91.                 break;
  92.  
  93.                 default:
  94.                 cout<< "\n\n\t *Invalid input!," <<endl;
  95.                 cout<< "\t use the following" <<endl;
  96.                 cout<< "\t options: A,B or C*" <<endl;
  97.                 break;
  98.                 }
  99.             }
  100.             // Amber Light section.
  101.             else if (light_color==2){
  102.             cout<< "The light is amber, what do you do?" <<endl;
  103.             cout<< "(A). Speed Up" <<endl;
  104.             cout<< "(B). Proceed With Caution" <<endl;
  105.             cout<< "(C). Stop" <<endl;
  106.             cout<< "Answer: ";
  107.             cin>> amber_option;
  108.                 while(isdigit(amber_option)){
  109.                 cout<< "\n\n\t *Invalid Input!";
  110.                 cout<< "\t *Please enter A,B or C" <<endl;
  111.                  cout<< "\n The light is amber, what do you do?" <<endl;
  112.                 cout<< "(A). Speed Up" <<endl;
  113.                 cout<< "(B). Proceed With Caution" <<endl;
  114.                 cout<< "(C). Stop" <<endl;
  115.                 cout<< "Answer: ";
  116.                 cin>> amber_option;
  117.                 }
  118.  
  119.                 //Give the user a response based on the Answer.
  120.                 switch(amber_option){
  121.  
  122.                 case 'a': //FALL-THROUGH INTENDED
  123.                 case 'A':
  124.                 cout<<"Speeding up to beat red lights, causes a LOT of accidents." <<endl;
  125.                 lives--;
  126.                 cout<<"\n\t** You have: "; cout<<lives;
  127.                 cout<< " lives remaining**" <<endl;
  128.                 break;
  129.  
  130.                 case 'b': //FALL-THROUGH INTENDED
  131.                 case 'B':
  132.                 cout<<"The light will most likely turn red as you proceed, don't do it." <<endl;
  133.                 lives--;
  134.                 cout<<"\n\t** You have: "; cout<<lives;
  135.                 cout<< " lives remaining**" <<endl;
  136.                 break;
  137.  
  138.                 case 'c': //FALL-THROUGH INTENDED
  139.                 case 'C':
  140.                 cout<<"CORRECT! You should be slowing down and stopping." <<endl;
  141.                 cout<<"\n\t** You have: "; cout<<lives;
  142.                 cout<< " lives remaining**" <<endl;
  143.                 break;
  144.  
  145.                 default:
  146.                 cout<< "\n\n\t Invalid input!," <<endl;
  147.                 cout<< "\t use the following" <<endl;
  148.                 cout<< "\t options: A,B or C" <<endl;
  149.                 break;
  150.                 }
  151.             }
  152.  
  153.             //Red Light section.
  154.             else if (light_color==3){
  155.              cout<< "The light is red, what do you do?" <<endl;
  156.             cout<< "(A). Speed Up" <<endl;
  157.             cout<< "(B). Proceed With Caution" <<endl;
  158.             cout<< "(C). Stop" <<endl;
  159.             cout<< "Answer: ";
  160.             cin>> red_option;
  161.                 while(isdigit(red_option)){
  162.                 cout<< "\n\n\t *Invalid Input!";
  163.                 cout<< "\t *Please enter A,B or C" <<endl;
  164.                  cout<< "\n The light is amber, what do you do?" <<endl;
  165.                 cout<< "(A). Speed Up" <<endl;
  166.                 cout<< "(B). Proceed With Caution" <<endl;
  167.                 cout<< "(C). Stop" <<endl;
  168.                 cout<< "Answer: ";
  169.                 cin>> red_option;
  170.                 }
  171.                 //Give the user a response based on the Answer.
  172.                 switch(red_option){
  173.  
  174.                 case 'a': //FALL-THROUGH INTENDED
  175.                 case 'A':
  176.                 cout<<"What?, Speed up in to oncoming traffic? Are you nuts?" <<endl;
  177.                 lives--;
  178.                 cout<<"\n\t** You have: "; cout<<lives;
  179.                 cout<< " lives remaining**" <<endl;
  180.                 break;
  181.  
  182.                 case 'b': //FALL-THROUGH INTENDED
  183.                 case 'B':
  184.                 cout<<"hmm, Not a fantastic idea no matter how cautious you are." <<endl;
  185.                 lives--;
  186.                 cout<<"\n\t** You have: "; cout<<lives;
  187.                 cout<< " lives remaining**" <<endl;
  188.                 break;
  189.  
  190.                 case 'c': //FALL-THROUGH INTENDED
  191.                 case 'C':
  192.                 cout<<"That would be correct!, Good Job!" <<endl;
  193.                 cout<<"\n\t** You have: "; cout<<lives;
  194.                 cout<< " lives remaining**" <<endl;
  195.                 break;
  196.  
  197.                 default:
  198.                 cout<< "\n\n\t Invalid input!," <<endl;
  199.                 cout<< "\t use the following" <<endl;
  200.                 cout<< "\t options: A,B or C" <<endl;
  201.                 break;
  202.                 }
  203.             }
  204.         } //end WHILE loop
  205.         while (lives>0);
  206.     }//end main IF statement
  207.  
  208.     /*
  209.     //Terminating conditions (lives have been depleted) or users says no.
  210.     //to end the program.
  211.     */
  212.     else if (run_program == "N" || run_program== "n"){
  213.     //I guess they don't want to run the program?
  214.     cout<< "You said NO?!" <<endl;
  215.     cout<< "Fair enough, maybe another day." <<endl;
  216.     }
  217. cout<<"\t****************************" <<endl;
  218. cout<<"\tYOU ARE DEAD! (learn to drive)" <<endl;
  219. cout<<"\t****************************" <<endl;
  220. return 0;
  221. }
  222. //end of program.
  223.  
Advertisement
Add Comment
Please, Sign In to add comment