MrSnoopy

cpp projekt

Mar 31st, 2014 (edited)
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  === VERZE 1 ===
  2. #include <iostream>
  3.  
  4. using std::cout;
  5. using std::cin;
  6. using std::endl;
  7.  
  8. int  main(int argc, char*argv []){
  9.  
  10.     //declare section
  11.  
  12.     //start money
  13.     int money = 5000000;
  14.  
  15.     //item counters
  16.     int audi = 0;
  17.     int bmw = 0;
  18.     int subaru = 0;
  19.  
  20.     //menu choice (dont neednt to initialize)
  21.     char choice = '0';
  22.  
  23.     //--declare section end--
  24.  
  25.  
  26.     //Do, while not ended by choice = 0
  27.     do{
  28.         //how much car to buy
  29.         int count =0;
  30.  
  31.         cout << "Welcome to car store!" << endl;
  32.         cout << "You have " << endl;
  33.         cout << "Audi's: " << audi << endl;
  34.         cout << "BMW's:  " << bmw << endl;
  35.         cout << "Subaru's: " << subaru << endl;
  36.  
  37.         cout << "Money: " << money << endl;
  38.  
  39.         cout << "Which type do you want?" << endl;
  40.         cout << "(S)ubaru (25459 per car) - (B)MW (31596 per car) - (A)udi (36498 per car)?" <<endl;
  41.         cout << "Type '0'to leave the game! " << endl;
  42.  
  43.         cin >> choice;
  44.  
  45.         //dont write the same code twice or more  times ;)
  46.         if(choice != '0'){
  47.             cout << "How much? ";
  48.             cin >> count;
  49.         }
  50.  
  51.         switch(choice){
  52.         case 's':
  53.             //without break continues to case bellow !! not as correct as can be
  54.         case 'S':
  55.             money -= 25459 * count;
  56.             cout << "You bought subaru! Your current money is: " << money << endl;
  57.             subaru +=1 * count;
  58.             break;
  59.  
  60.         case 'b':
  61.                 //without break continues to case bellow
  62.         case 'B':
  63.             money -= 31596 * count;
  64.             cout << "You bought BMW! Your current money is: " << money << endl;
  65.             bmw+=1 * count;
  66.             break;
  67.         case 'a':
  68.                 //without break continues to case bellow
  69.         case 'A':
  70.             money -= 36498 * count;
  71.             cout << "You bought Audi! Your current money is: " << money << endl;
  72.             audi+=1 * count;
  73.             break;
  74.         default:
  75.             cout << "Wrong choice, repeat please" << endl << endl;
  76.         }
  77.  
  78.  
  79.     }while(choice != '0');
  80.  
  81.  
  82.     return 0;
  83. }
  84.  
  85. === VERZE 2 ===
  86. #include <iostream>
  87. #include <string>
  88.  
  89. using std::cout;
  90. using std::cin;
  91. using std::endl;
  92. using std::string;
  93.  
  94. using namespace std;
  95.  
  96. int  main(int argc, char*argv []){
  97.  
  98.     //declare section
  99.  
  100.     //start money
  101.     int money = 5000000;
  102.  
  103.     //item counters
  104.     int audi = 0;
  105.     int bmw = 0;
  106.     int subaru = 0;
  107.  
  108.     //menu choice (dont neednt to initialize)
  109.     string choice;
  110.     //--declare section end--
  111.  
  112.  
  113.     //Do, while not ended by choice "END" or "end"
  114.     do{
  115.         //how much car to buy
  116.         int count =0;
  117.  
  118.         cout << "Welcome to car store!" << endl;
  119.         cout << "You have " << endl;
  120.         cout << "Audi's: " << audi << endl;
  121.         cout << "BMW's:  " << bmw << endl;
  122.         cout << "Subaru's: " << subaru << endl;
  123.  
  124.         cout << "Money: " << money << endl;
  125.  
  126.         cout << "Which type do you want?" << endl;
  127.         cout << "(S)ubaru (25459 per car) - (B)MW (31596 per car) - (A)udi (36498 per car)?" <<endl;
  128.         cout << "Type 'END' to leave the game! " << endl;
  129.  
  130.         //btw better std::getline (http://www.cplusplus.com/reference/string/string/getline/)
  131.         cin >> choice;
  132.  
  133.         //dont write the same code twice or more  times ;)
  134.             //returns length of string var 1 -> only 1 char
  135.         if(choice.length() == 1){
  136.             cout << "How much? ";
  137.             cin >> count;
  138.         }
  139.  
  140.         switch(choice[0]){
  141.         case 's':
  142.             //without break continues to case bellow !! not as correct as can be
  143.         case 'S':
  144.             money -= 25459 * count;
  145.             cout << "You bought subaru! Your current money is: " << money << endl;
  146.             subaru +=1 * count;
  147.             break;
  148.  
  149.         case 'b':
  150.                 //without break continues to case bellow
  151.         case 'B':
  152.             money -= 31596 * count;
  153.             cout << "You bought BMW! Your current money is: " << money << endl;
  154.             bmw+=1 * count;
  155.             break;
  156.         case 'a':
  157.                 //without break continues to case bellow
  158.         case 'A':
  159.             money -= 36498 * count;
  160.             cout << "You bought Audi! Your current money is: " << money << endl;
  161.             audi+=1 * count;
  162.             break;
  163.         default:
  164.             cout << "Wrong choice, repeat please" << endl << endl;
  165.         }
  166.  
  167.        
  168.     }while(!choice.compare("END") && !choice.compare("end") );
  169.     //same as !choice.compare("END") == 0
  170.     // string.compare return 0 if equals
  171.     // http://www.cplusplus.com/reference/string/string/compare/
  172.  
  173.     return 0;
  174. }
Add Comment
Please, Sign In to add comment