Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<stdlib.h>
- #include<time.h>
- using namespace std;
- void writeMenu(){
- cout<<"1. Start driving"<<endl;
- cout<<"2. Rest"<<endl;
- cout<<"3. Exit"<<endl;
- }
- void hold(){
- cin.clear();
- cin.ignore();
- getchar();
- }
- int gasoline = 100;
- int input, speed, doneRandom = 0;
- int main(){
- system("cls");
- cout<<"Your gasoline's car: "<<gasoline<<" liters"<<endl;
- cout<<"========================"<<endl;
- writeMenu();
- cin>>input;
- if(!cin){
- cin.clear();
- cin.ignore();
- main();
- } else {
- switch(input){
- case 1:
- /* initialize random seed: */
- srand (time(NULL));
- /* generate secret number between 1 and 10: */
- if(doneRandom == 0){
- speed = rand() % 100 + 1;
- doneRandom = 1;
- }
- cout<<"Your speed : "<<speed<<" km/hour"<<endl;
- if(speed >= 50){
- if(gasoline >= 40){
- gasoline -= 40;
- cout<<"You have spent 40 liters gasoline"<<endl;
- } else {
- cout<<"Your gasoline is not enough to run the car..."<<endl;
- }
- } else {
- if(gasoline >= 20){
- gasoline -= 20;
- cout<<"You have spent 20 liters gasoline"<<endl;
- } else {
- cout<<"Your gasoline is not enough to run the car..."<<endl;
- }
- }
- hold();
- main();
- break;
- case 2:
- if(gasoline == 100){
- cout<<"Your gasoline is full"<<endl;
- } else {
- if(gasoline > 80){
- gasoline = 100;
- cout << "Your gasoline is now fully recharged" << endl;
- } else {
- gasoline += 20;
- cout << "Successfully recharged 20 liters gasoline" << endl;
- }
- }
- hold();
- main();
- break;
- case 3:
- cout<<"Thank you..."<<endl;
- exit(0);
- break;
- default:
- cout<<"inputan salah"<<endl;
- cin.clear();
- cin.ignore();
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement