Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cash register program

    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

    Code:
    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!

  • #2
    New Coder
    Join Date
    Jun 2005
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.
    Last edited by fbelzile; 06-29-2007 at 07:17 AM.

  • #3
    New Coder
    Join Date
    May 2007
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Code:
    #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;
    }
    Last edited by Rubby; 07-06-2007 at 09:49 PM. Reason: oops

  • #4
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by fbelzile View Post
    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.
    OracleGuy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •