Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 01-17-2005, 09:43 PM   #1
In Runtime
 
Windwaker222's Avatar
 
Join Date: Nov 2004
Posts: 351
Default Simplifiy Code in C++

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();     
}
__________________

__________________
AMD Phenom 9600 / 4GB OCZ Gold / nVidia 8800GT / ASUS Crosshair 4xPCI-E
Windwaker222 is offline   Reply With Quote
Old 01-24-2005, 12:20 PM   #2
Mal
In Runtime
 
Mal's Avatar
 
Join Date: Jan 2005
Posts: 221
Default Re: Simplifiy Code in C++

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.
__________________

Mal is offline   Reply With Quote
Old 01-26-2005, 06:54 AM   #3
Baseband Member
 
John000's Avatar
 
Join Date: Jan 2005
Posts: 21
Send a message via AIM to John000
Default

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 .
John000 is offline   Reply With Quote
Old 01-26-2005, 09:31 AM   #4
Mal
In Runtime
 
Mal's Avatar
 
Join Date: Jan 2005
Posts: 221
Default Re: Simplifiy Code in C++

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.
Mal is offline   Reply With Quote
Old 02-07-2005, 10:47 PM   #5
Daemon Poster
 
RewtGuy's Avatar
 
Join Date: Dec 2004
Posts: 595
Send a message via AIM to RewtGuy
Default Re: Simplifiy Code in C++

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)
__________________
Windows: A thirty-two bit extension and GUI shell to a sixteen bit patch to an eight bit operating system originally coded for a four bit microprocessor and sold by a two-bit company that can't stand one bit of competition.
RewtGuy is offline   Reply With Quote
Old 02-08-2005, 07:01 AM   #6
Site Team
 
root's Avatar
 
Join Date: Mar 2004
Posts: 8,006
Default Re: Simplifiy Code in C++

or change the while to

while(( input != 0)&&(cash>=0));
root is offline   Reply With Quote
Old 02-09-2005, 03:48 PM   #7
Daemon Poster
 
RewtGuy's Avatar
 
Join Date: Dec 2004
Posts: 595
Send a message via AIM to RewtGuy
Default Re: Simplifiy Code in C++

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");
__________________
Windows: A thirty-two bit extension and GUI shell to a sixteen bit patch to an eight bit operating system originally coded for a four bit microprocessor and sold by a two-bit company that can't stand one bit of competition.
RewtGuy is offline   Reply With Quote
Old 02-17-2005, 03:51 PM   #8
Mal
In Runtime
 
Mal's Avatar
 
Join Date: Jan 2005
Posts: 221
Default Re: Simplifiy Code in C++

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.
Mal is offline   Reply With Quote
Old 02-17-2005, 03:53 PM   #9
Mal
In Runtime
 
Mal's Avatar
 
Join Date: Jan 2005
Posts: 221
Default Re: Simplifiy Code in C++

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
__________________

Mal is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 11:22 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0