Simplifiy Code in C++

Windwaker222

In Runtime
Messages
379
It's time to play....Simplify my crappy code

The winner gets..Well nothing

Anyway I just learned C++ today since I want to be a programmer when I am older. The program is basically letting you buy stuff. There isnt a limit since I haven't figured out how yet. I just wanted to know is there an easier, but still simple way to write the code

Code:
#include <iostream>
using namespace std;
int main()
{
int cash=5000;
int input;
int a=700;
int b=300;
int c=350;
int d=1400;
int e=1100;
int f=260;
int g=79;
int h=400;
int i=599;
int j=59;


      
     cout << "Welcome!  You are now using the most automated purchasing program in the world. This program has been created to ease you through the process of spending money.\n\n";
     cout << "You have " << cash << "$ to spend\n\n";
     do{
     cout << "1. nVidia 6800 Ultra : 700$\n";
     cout << "2. nVidia 6600 GT : 300$\n";
     cout << "3. AMD 64 3500+ : 350$\n";
     cout << "4. Intel Pentium 4 3.4GHz EE : 1400$\n";
     cout << "5. AMD 64 FX-55 : 1100$\n";
     cout << "6. Western Raptor 10K 74GB SATA 16MB : 260$\n";
     cout << "7. Liteon 16xDVD-RW : 79$\n";
     cout << "8. Apple Ipod 20GB : 400$\n";
     cout << "9. Apple Mac Mini : 599$\n";
     cout << "10. Doom 3 : 59$\n\n";
     cout << "Please enter your selection : ";
     cin >> input;
     cin.ignore();
     
     switch ( input ) {
     case 1:
          cout << "\n\nYou now have " << (cash=cash-a) <<"$ left.\n\n";
          break;
     case 2:
          cout << "\n\nYou now have " << (cash=cash-b) <<"$ left.\n\n";
          break;
     case 3:
          cout << "\n\nYou now have " << (cash=cash-c) <<"$ left.\n\n";
          break;
     case 4:
          cout << "\n\nYou now have " << (cash=cash-d) <<"$ left.\n\n";
          break;
     case 5:
          cout << "\n\nYou now have " << (cash=cash-e) <<"$ left.\n\n";
          break;
     case 6:
          cout << "\n\nYou now have " << (cash=cash-f) <<"$ left.\n\n";
          break;
     case 7:
          cout << "\n\nYou now have " << (cash=cash-g) <<"$ left.\n\n";
          break;
     case 8:
          cout << "\n\nYou now have " << (cash=cash-h) <<"$ left.\n\n";
          break;
     case 9:
          cout << "\n\nYou now have " << (cash=cash-i) <<"$ left.\n\n";
          break;
     case 10:
          cout << "\n\nYou now have " << (cash=cash-j) <<"$ left.\n\n";
          break;
     case q:
          
     default :
          cout << "\n\nThat is not a valid item!  You still have " << cash << "$ to spend!\n\n";
          break;
     }
} while( input != 0);
     cin.get();     
}
 
short answer yes, i'll shorten it later on if you like. busy atm. but with the switch function, no i dont know of a way. but im at a pretty low level in comparison.
 
I know of something little that can be changed to shorten it. In all of your cases instead of putting cash=cash-a, you could put cash-=a .
 
and

int cash=5000;
int input;
int a=700;
int b=300;
int c=350;
int d=1400;
int e=1100;
int f=260;
int g=79;
int h=400;
int i=599;
int j=59;

can be written as

int cash=5000, input, a=700, b=300, c=350, d=1400, e=1100, f=260, g=79, h=400, i=599, j=59;

all on one line.
 
tip: add an if statement right after do

if (cash=<0) {cout << "you've run out of cash"; exit (1); }

this way if you run out of money you don't go negative. (which you can't do in real life)
 
or change the while to

while(( input != 0)&&(cash>=0));
 
also instead of cin.get(); you could use system ("pause");
pauses, informs you to press anykey then after a key is pressed it resumes. cin.get(); only goes away when enter is pressed. system ("pause"); isn't very portable though, but if you're in *nix you wouldn't need to keep the terminal from flicking away :p

#include <cstdlib> // to use system ("pause");
 
I use system("pause"); with the standard #include <iostream.h> but eithers fine.
Root's while suggestion is a good one.
My learning always taught me "cin << " as opposed to "cin.get()" but whichever takes your fancy really.
 
also, just noticed, with your line:
cout << "You have " << cash << "$ to spend\n\n";
you really want the $ sign before the number, so change it to:
cout << "You have $" << cash << " to spend\n\n";
pretty trivial I know but every little helps ;)
 
Back
Top Bottom