Whats wrong with this program?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • qwert7465
    New Member
    • Jan 2010
    • 6

    Whats wrong with this program?

    I wrote this program and took care of most of the errors and when i execute it, it works but something is wrong. It is a store and i put some products in this store and after you made your selection, it gives you the receipt. unfortunatly it does not keep track of how many items you selected so it is always zero. any help would be appreciated on this problem. here is the code:

    Code:
    // Final Project
    
    
    #include <iostream.h>
    #include <iomanip.h>		
    
    // Function Prototypes
    void checkout();
    void start_or_startover();
    void receipt();
    void main_function();
    
    float subtotal, tax, total;
    
    // Global Variables, makes everything easier
    
    // Prices
    float laptopprice = 799.99;
    float mouseprice = 12.99;
    float keyboardprice = 29.99;
    float moniterprice = 399.99;
    float usbprice = 16.99;
    float hdmiprice = 24.99;
    float lazerprice = 6.99;
    float powerprice = 9.99;
    
    // Quantity
    int laptopnumber;
    int mousenumber;
    int keyboardnumber;
    int moniternumber;
    int usbnumber;
    int hdminumber;
    int lazernumber;
    int powernumber;
    int zero;
    int choice_amount;
    int choice;
    
    // Item chosen, and amount of the item are stored in these variables.
    
    // Main Function
    int main()
    {
    	// do loop shows merchandise
    	do
    	{
    	cout << "(1) HP Pavilion Intel Core 2 Duo Laptop			$799.99" << endl;
    	cout << "(2) Razer Black Wired Lazer Mouse			$12.99" << endl;
    	cout << "(3) Ra zer Keyboard Wired				$29.99" << endl;
    	cout << "(4) HP LCD Flatscreen Moniter				$399.99" << endl;
    	cout << "(5) SATA USB 8GB Flash Drive				$16.99" << endl;
    	cout << "(6) HDMI Full 1080p 6ft Cable				$24.99" << endl;
    	cout << "(7) Lazer Pointer					$6.99" << endl;
    	cout << "(8) Power Cord						$9.99" << endl;
        cout << endl;
    	cout << "(9) Total" << endl;
    	cout << "(10) Cancel Order" << endl;
    	cout << " Please enter the choice number of the electronics that you would like to buy." << endl << endl;
    	cin >> choice;
    	cout << endl;
    	
    	// if the user is done with his/her choices, the sale is totaled.
    	if (choice == 9)
    	{
    		checkout();
    	}
    	
    	cout << " How many would you like? ";
    	cin >> choice_amount;
    	cout << endl;
    	
    	// The number of each item is asked for here.
    	switch (choice)
    	{
    		case 1:
    		total = laptopprice + zero;
    		break;
    
    		case 2:
    		total = mouseprice + zero;
    		break;
    
    		case 3:
    		total = keyboardprice + zero;
    		break;
    
    		case 4:
    		total = moniterprice + zero;
    		break;
    
    		case 5:
    		total = usbprice + zero;
    		break;
    
    		case 6:
    		total = hdmiprice + zero;
    		break;
    
    		case 7:
    		total = lazerprice + zero;
    		break;
    
    		case 8:
    		total = powerprice + zero;
    		break;
    		
    		case 9:
    		laptopnumber = 0;
    		mousenumber = 0;
    		keyboardnumber = 0;
    		moniternumber = 0;
    		usbnumber = 0;
    		hdminumber = 0;
    		lazernumber = 0;
    		powernumber = 0;
    	}
    	
    	;}
    	
    	while (choice != 9);
    	if (choice == 10);
    	
    	{ start_or_startover(); }
    	
    
    	receipt();
    
    	return 0;
    	}
    void start_or_startover()
    {
    	cout << "Ferger's Technology Store" << endl;
    
    }
    void checkout()
    	{
    	subtotal = (laptopnumber * laptopprice) + (mousenumber * mouseprice) + (keyboardnumber * keyboardprice) + (	subtotal = (laptopnumber * laptopprice) + (mousenumber * mouseprice) + (keyboardnumber * keyboardprice) + (	subtotal = (laptopnumber * laptopprice) + (mousenumber * mouseprice) + (keyboardnumber * keyboardprice) + (moniternumber * moniterprice) + (usbnumber * usbprice) + (hdminumber * hdmiprice) + (lazernumber * lazerprice) + (powernumber * powerprice)));
    
    	tax = subtotal * .0875;
    
    	total = subtotal + tax;
    	}
    
    void receipt()
    
    	{
    
    		cout << "Thank you for your buisness. Here is your receipt." << endl << endl;
    		cout << "Items\t\t  " << "Number of items purchased \t Price of each item \t Total price" << endl;
    		cout.setf(ios::fixed);
    		cout << "HP Pavilion Intel Core 2 Duo Laptop \t "<< laptopnumber << "\t\t\t" << "$" << setprecision(2) << laptopprice << "\t\t   " << "$" << (laptopnumber * laptopprice) << endl;
    		cout << "Razer Black Wired Lazer Mouse \t "<< mousenumber << "\t\t\t" << "$" <<  setprecision(2) << mouseprice << "\t\t   " << "$" << (mousenumber * mouseprice) << endl;
    		cout << "Razer Keyboard Wired \t "<< keyboardnumber << "\t\t\t" << "$" << setprecision(2) << keyboardprice << "\t\t   " << "$" << (keyboardnumber * keyboardprice) << endl;
    		cout << "SATA USB 8GB Flash Drive \t "<< usbnumber << "\t\t\t" << "$" << setprecision(2) << usbprice << "\t\t   " << "$" << (usbnumber * usbprice) << endl;
    		cout << "HDMI Full 1080p 6ft Cable \t "<< hdminumber << "\t\t\t" << "$" << setprecision(2) << hdmiprice << "\t\t   " << "$" << (hdminumber * hdmiprice) << endl;
    		cout << "Lazer Pointer \t "<< lazernumber << "\t\t\t" << "$" << setprecision(2) << lazerprice << "\t\t   " << "$" << (lazernumber * lazerprice) << endl;
    		cout << "Power Cord \t "<< powernumber << "\t\t\t" << "$" << setprecision(2) << powerprice << "\t\t   " << "$" << (powernumber * powerprice) << endl;
    	
    		cout << "\t\t Subtotal \t $" << subtotal << endl;
    		cout << "\t\t Tax \t\t $" << tax << endl;
    		cout << "\t\t Total \t\t $" << total << endl;
    
    }
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    A couple of observations:
    1. You do not increment the 'amount' variables anywhere. Presumably this would be done in the select statement. If you do not increment them, how else would you know how many have been selected?
    2. In your select statement, case: 9, you set all the amount variables to 0. Considering that the 9 case is titled "Total", you should not be reseting any purchases. Also, in that case, you should stop asking 'How many do you want' and go straight to your checkout/receipt.


    Mark.

    Comment

    • qwert7465
      New Member
      • Jan 2010
      • 6

      #3
      thanks! i finished the program. i was wondering how to prevent a infinite loop. can someone post some code that i could paste into my program to prevent a character from looping it infinitely. thanks!

      Comment

      • johny10151981
        Top Contributor
        • Jan 2010
        • 1059

        #4
        you better try another option like
        Enter 11: to exit shoping

        out of the switch use condition to stop while loop.. as Example:

        while(1)
        {
        if(var==desired _data)
        break;
        }

        Best Regards
        johny

        Comment

        Working...