### How to Store the Last Data ?

Case

An automotive company needs a program that checks gasoline usages. This program can be used to check the gasoline usages based on how fast the car move. As a programmer, you are asked to create the program. Here are some rules about the program:

• At the beginning, the program will set the gasoline to 100 liters.

• Show the main menus, such as:
1. Start Driving
2. Rest
3. Exit

• If user chooses “Start Driving”, then the program will:
o Show the speed (km/hour) with random 1 – 100.
o If speed is less than 50 km/hour, gasoline will be reduced by 20 liters.
o If speed is equals or larger than 50 km/hour, gasoline will be reduced by 40 liters.
o If the gasoline is empty, then the program will show “Your gasoline is not enough to run the car…”

• If user chooses “Rest”, then the program will:
o If the gasoline is full, then the program will show “Your gasoline is full…”, else the gasoline will be increased by 20 liters.

• If user chooses “Exit”, then the program will end.

My attempt:
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465`` ``````#include #include using namespace std; int main() { int gasoline = 100; int menu; char returnMainmenu; do { do { cout << "1. Start driving" << endl << "2. Rest" << endl << "3. Exit" << endl << endl; cout << "What do you want to do: "; cin >> menu; cout << endl; } while(menu<1 || menu>3); if(menu == 1) { int speed = rand() % 100 + 1; cout << "Your speed: " << speed << endl; if(speed >= 50) { cout << "Current fuel: " << gasoline-40 << endl; cout << "Keep moving [Y/N]: "; cin >> returnMainmenu; cout << endl; } else { cout << "Current fuel: " << gasoline-20 << endl; cout << "Keep moving [Y/N]: "; cin >> returnMainmenu; cout << endl; } if(menu == 2) { if(gasoline < 100) { gasoline+20; cout << "Fuel added, current fuel: " << gasoline << endl; cout << "Keep moving [Y/N]: "; cin >> returnMainmenu; cout << endl; } if(gasoline==100) { cout << "Your fuel is full." << endl; cout << "Keep moving [Y/N]: "; cin >> returnMainmenu; cout << endl; } } if(menu==3) { cout << "Goodbye, have a nice day!" << endl; } } } while(returnMainmenu=='y'); return 0; } ``````

My problem is:

Example, i choose menu 1, lets say gasoline reduced into 60. Then back to main menu, and choose 2. BUT THE GASOLINE STILL 100 not 60 :(

How to store the last gasoline so we can use it for another menu (like rest)? Thanks
Last edited on
Shouldn't line 20 be && not ||?
Also you are not assigning values to gasoline on line 29 and 35. You are just outputting a number subtracting a number if you want to assign a value you will use -= that will subtract then asign the new value. `gasoline -= 20;` Same thing goes with line 43 you are not assigning the new value you need += instead of +.

I might suggest you read up on operators a bit more. Especially assignment( = )
http://www.cplusplus.com/doc/tutorial/operators/
Oh my god, I LOVE YOU SO MUCH giblit :* Thanks :)
Topic archived. No new replies allowed.