...

View Full Version : Cash register program



Devmen
06-29-2007, 06:54 AM
Hi im new to c++ (taking a class) and im trying to make a cash register program that takes the cost of items, sums it up, adds tax, and gives a result. I want to use a loop, either for or while, but im having difficulty.
this is what i have


int main()
{
float item, cost, tax, subtotal, grandtotal;
//Declared Variable
item = 0;
cout.precision(4);
for (int x = 0; x < 1; item++)
{
cout << "Enter the cost of the item in $: "; // prompt for cost of the item
cin >> cost; // input cost
subtotal = cost + subtotal; // adds item cost the the subtotal
if(cost == 0) // ends loop if 0 is entered
x = 1;
}

tax = subtotal * .07; // computes tax
subtotal = tax + subtotal; //adds tax to the cost
cout << "Your Total is: " << subtotal << " with " << item << " item(s)\n";

return 0;

}





as of now im stuck, when i try to stop the loop so that it sums up and adds tax the program terminates. thanks in advance for help!

fbelzile
06-29-2007, 08:12 AM
I think you might have just forgot the curly brackets around the:

x = 1;

During the if statement... You might also need to put in:

system ("pause");

Instead of the "return 0;" This will pause the program to see your output. I'm also new to C++ so there might be another problem that I overlooked.

Rubby
07-06-2007, 10:46 PM
This is certainly not the place to be using a for loop. Those are usually reserved for times when you know exactly how many times the loop will be iterating, that's why you have an index increment in the decoration (you should not be using item++ here!) In this sort of situation I would use a infinite loop if I were you.
I wrote this little chunk of code, but I have no idea if it works. My compiler is on the fritz and I'm too lazy to fix it right now.


#include <iostream>

int main()
{
float cost, subtotal, grandtotal;
float tax = 0.07;
int item = 0;
std::cout.precision(4);
while(1)
{
std::cout << "Enter the cost of the item in $: ";// prompt for cost of the item
std::cin >> cost; // input cost
subtotal += cost; // adds item cost the the subtotal
if(cost == 0) // ends loop if 0 is entered
break;
else
item++;
}
subtotal += (subtotal * tax); // computes tax
std::cout << "Your Total is: " << subtotal << " with " << item << " item(s)\n";

return 0;
}

oracleguy
07-07-2007, 12:58 AM
system ("pause");

Instead of the "return 0;" This will pause the program to see your output. I'm also new to C++ so there might be another problem that I overlooked.

Put the pause before the return 0, not in replace of it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum