Advertisement
fjraito

Code 2 - Driving

Apr 21st, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | None | 0 0
  1. #include<iostream>
  2. #include<stdlib.h>
  3. #include<time.h>
  4. using namespace std;
  5.  
  6. void writeMenu(){
  7.     cout<<"1. Start driving"<<endl;
  8.     cout<<"2. Rest"<<endl;
  9.     cout<<"3. Exit"<<endl;
  10. }
  11.  
  12. void hold(){
  13.     cin.clear();
  14.     cin.ignore();
  15.     getchar();
  16. }
  17.  
  18. int gasoline = 100;
  19. int input, speed, doneRandom = 0;
  20.    
  21. int main(){
  22.    
  23.     system("cls");
  24.    
  25.     cout<<"Your gasoline's car: "<<gasoline<<" liters"<<endl;
  26.     cout<<"========================"<<endl;
  27.     writeMenu();
  28.     cin>>input;
  29.     if(!cin){
  30.         cin.clear();
  31.         cin.ignore();
  32.         main();
  33.     } else {
  34.         switch(input){
  35.             case 1:
  36.  
  37.                 /* initialize random seed: */
  38.                 srand (time(NULL));
  39.            
  40.                 /* generate secret number between 1 and 10: */
  41.                 if(doneRandom == 0){
  42.                     speed = rand() % 100 + 1;
  43.                     doneRandom = 1;
  44.                 }                  
  45.                 cout<<"Your speed : "<<speed<<" km/hour"<<endl;
  46.                 if(speed >= 50){
  47.                     if(gasoline >= 40){
  48.                         gasoline -= 40;
  49.                         cout<<"You have spent 40 liters gasoline"<<endl;   
  50.                     } else {
  51.                         cout<<"Your gasoline is not enough to run the car..."<<endl;
  52.                     }
  53.                 } else {
  54.                     if(gasoline >= 20){
  55.                         gasoline -= 20;
  56.                         cout<<"You have spent 20 liters gasoline"<<endl;   
  57.                     } else {
  58.                         cout<<"Your gasoline is not enough to run the car..."<<endl;
  59.                     }
  60.                 }
  61.                 hold();
  62.                 main();
  63.                 break;
  64.             case 2:
  65.                
  66.                 if(gasoline == 100){
  67.                     cout<<"Your gasoline is full"<<endl;   
  68.                 } else {
  69.                     if(gasoline > 80){
  70.                         gasoline = 100;
  71.                         cout << "Your gasoline is now fully recharged" << endl;
  72.                     } else {
  73.                         gasoline += 20;
  74.                         cout << "Successfully recharged 20 liters gasoline" << endl;
  75.                     }
  76.                 }
  77.                 hold();
  78.                 main();
  79.                 break;
  80.             case 3:
  81.                
  82.                 cout<<"Thank you..."<<endl;
  83.                 exit(0);
  84.                 break;
  85.                
  86.             default:
  87.                 cout<<"inputan salah"<<endl;
  88.                 cin.clear();
  89.                 cin.ignore();
  90.                 break;
  91.         }
  92.     }
  93.    
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement