Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- === VERZE 1 ===
- #include <iostream>
- using std::cout;
- using std::cin;
- using std::endl;
- int main(int argc, char*argv []){
- //declare section
- //start money
- int money = 5000000;
- //item counters
- int audi = 0;
- int bmw = 0;
- int subaru = 0;
- //menu choice (dont neednt to initialize)
- char choice = '0';
- //--declare section end--
- //Do, while not ended by choice = 0
- do{
- //how much car to buy
- int count =0;
- cout << "Welcome to car store!" << endl;
- cout << "You have " << endl;
- cout << "Audi's: " << audi << endl;
- cout << "BMW's: " << bmw << endl;
- cout << "Subaru's: " << subaru << endl;
- cout << "Money: " << money << endl;
- cout << "Which type do you want?" << endl;
- cout << "(S)ubaru (25459 per car) - (B)MW (31596 per car) - (A)udi (36498 per car)?" <<endl;
- cout << "Type '0'to leave the game! " << endl;
- cin >> choice;
- //dont write the same code twice or more times ;)
- if(choice != '0'){
- cout << "How much? ";
- cin >> count;
- }
- switch(choice){
- case 's':
- //without break continues to case bellow !! not as correct as can be
- case 'S':
- money -= 25459 * count;
- cout << "You bought subaru! Your current money is: " << money << endl;
- subaru +=1 * count;
- break;
- case 'b':
- //without break continues to case bellow
- case 'B':
- money -= 31596 * count;
- cout << "You bought BMW! Your current money is: " << money << endl;
- bmw+=1 * count;
- break;
- case 'a':
- //without break continues to case bellow
- case 'A':
- money -= 36498 * count;
- cout << "You bought Audi! Your current money is: " << money << endl;
- audi+=1 * count;
- break;
- default:
- cout << "Wrong choice, repeat please" << endl << endl;
- }
- }while(choice != '0');
- return 0;
- }
- === VERZE 2 ===
- #include <iostream>
- #include <string>
- using std::cout;
- using std::cin;
- using std::endl;
- using std::string;
- using namespace std;
- int main(int argc, char*argv []){
- //declare section
- //start money
- int money = 5000000;
- //item counters
- int audi = 0;
- int bmw = 0;
- int subaru = 0;
- //menu choice (dont neednt to initialize)
- string choice;
- //--declare section end--
- //Do, while not ended by choice "END" or "end"
- do{
- //how much car to buy
- int count =0;
- cout << "Welcome to car store!" << endl;
- cout << "You have " << endl;
- cout << "Audi's: " << audi << endl;
- cout << "BMW's: " << bmw << endl;
- cout << "Subaru's: " << subaru << endl;
- cout << "Money: " << money << endl;
- cout << "Which type do you want?" << endl;
- cout << "(S)ubaru (25459 per car) - (B)MW (31596 per car) - (A)udi (36498 per car)?" <<endl;
- cout << "Type 'END' to leave the game! " << endl;
- //btw better std::getline (http://www.cplusplus.com/reference/string/string/getline/)
- cin >> choice;
- //dont write the same code twice or more times ;)
- //returns length of string var 1 -> only 1 char
- if(choice.length() == 1){
- cout << "How much? ";
- cin >> count;
- }
- switch(choice[0]){
- case 's':
- //without break continues to case bellow !! not as correct as can be
- case 'S':
- money -= 25459 * count;
- cout << "You bought subaru! Your current money is: " << money << endl;
- subaru +=1 * count;
- break;
- case 'b':
- //without break continues to case bellow
- case 'B':
- money -= 31596 * count;
- cout << "You bought BMW! Your current money is: " << money << endl;
- bmw+=1 * count;
- break;
- case 'a':
- //without break continues to case bellow
- case 'A':
- money -= 36498 * count;
- cout << "You bought Audi! Your current money is: " << money << endl;
- audi+=1 * count;
- break;
- default:
- cout << "Wrong choice, repeat please" << endl << endl;
- }
- }while(!choice.compare("END") && !choice.compare("end") );
- //same as !choice.compare("END") == 0
- // string.compare return 0 if equals
- // http://www.cplusplus.com/reference/string/string/compare/
- return 0;
- }
Add Comment
Please, Sign In to add comment