473,386 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. // Final Project
  2.  
  3.  
  4. #include <iostream.h>
  5. #include <iomanip.h>        
  6.  
  7. // Function Prototypes
  8. void checkout();
  9. void start_or_startover();
  10. void receipt();
  11. void main_function();
  12.  
  13. float subtotal, tax, total;
  14.  
  15. // Global Variables, makes everything easier
  16.  
  17. // Prices
  18. float laptopprice = 799.99;
  19. float mouseprice = 12.99;
  20. float keyboardprice = 29.99;
  21. float moniterprice = 399.99;
  22. float usbprice = 16.99;
  23. float hdmiprice = 24.99;
  24. float lazerprice = 6.99;
  25. float powerprice = 9.99;
  26.  
  27. // Quantity
  28. int laptopnumber;
  29. int mousenumber;
  30. int keyboardnumber;
  31. int moniternumber;
  32. int usbnumber;
  33. int hdminumber;
  34. int lazernumber;
  35. int powernumber;
  36. int zero;
  37. int choice_amount;
  38. int choice;
  39.  
  40. // Item chosen, and amount of the item are stored in these variables.
  41.  
  42. // Main Function
  43. int main()
  44. {
  45.     // do loop shows merchandise
  46.     do
  47.     {
  48.     cout << "(1) HP Pavilion Intel Core 2 Duo Laptop            $799.99" << endl;
  49.     cout << "(2) Razer Black Wired Lazer Mouse            $12.99" << endl;
  50.     cout << "(3) Ra zer Keyboard Wired                $29.99" << endl;
  51.     cout << "(4) HP LCD Flatscreen Moniter                $399.99" << endl;
  52.     cout << "(5) SATA USB 8GB Flash Drive                $16.99" << endl;
  53.     cout << "(6) HDMI Full 1080p 6ft Cable                $24.99" << endl;
  54.     cout << "(7) Lazer Pointer                    $6.99" << endl;
  55.     cout << "(8) Power Cord                        $9.99" << endl;
  56.     cout << endl;
  57.     cout << "(9) Total" << endl;
  58.     cout << "(10) Cancel Order" << endl;
  59.     cout << " Please enter the choice number of the electronics that you would like to buy." << endl << endl;
  60.     cin >> choice;
  61.     cout << endl;
  62.  
  63.     // if the user is done with his/her choices, the sale is totaled.
  64.     if (choice == 9)
  65.     {
  66.         checkout();
  67.     }
  68.  
  69.     cout << " How many would you like? ";
  70.     cin >> choice_amount;
  71.     cout << endl;
  72.  
  73.     // The number of each item is asked for here.
  74.     switch (choice)
  75.     {
  76.         case 1:
  77.         total = laptopprice + zero;
  78.         break;
  79.  
  80.         case 2:
  81.         total = mouseprice + zero;
  82.         break;
  83.  
  84.         case 3:
  85.         total = keyboardprice + zero;
  86.         break;
  87.  
  88.         case 4:
  89.         total = moniterprice + zero;
  90.         break;
  91.  
  92.         case 5:
  93.         total = usbprice + zero;
  94.         break;
  95.  
  96.         case 6:
  97.         total = hdmiprice + zero;
  98.         break;
  99.  
  100.         case 7:
  101.         total = lazerprice + zero;
  102.         break;
  103.  
  104.         case 8:
  105.         total = powerprice + zero;
  106.         break;
  107.  
  108.         case 9:
  109.         laptopnumber = 0;
  110.         mousenumber = 0;
  111.         keyboardnumber = 0;
  112.         moniternumber = 0;
  113.         usbnumber = 0;
  114.         hdminumber = 0;
  115.         lazernumber = 0;
  116.         powernumber = 0;
  117.     }
  118.  
  119.     ;}
  120.  
  121.     while (choice != 9);
  122.     if (choice == 10);
  123.  
  124.     { start_or_startover(); }
  125.  
  126.  
  127.     receipt();
  128.  
  129.     return 0;
  130.     }
  131. void start_or_startover()
  132. {
  133.     cout << "Ferger's Technology Store" << endl;
  134.  
  135. }
  136. void checkout()
  137.     {
  138.     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)));
  139.  
  140.     tax = subtotal * .0875;
  141.  
  142.     total = subtotal + tax;
  143.     }
  144.  
  145. void receipt()
  146.  
  147.     {
  148.  
  149.         cout << "Thank you for your buisness. Here is your receipt." << endl << endl;
  150.         cout << "Items\t\t  " << "Number of items purchased \t Price of each item \t Total price" << endl;
  151.         cout.setf(ios::fixed);
  152.         cout << "HP Pavilion Intel Core 2 Duo Laptop \t "<< laptopnumber << "\t\t\t" << "$" << setprecision(2) << laptopprice << "\t\t   " << "$" << (laptopnumber * laptopprice) << endl;
  153.         cout << "Razer Black Wired Lazer Mouse \t "<< mousenumber << "\t\t\t" << "$" <<  setprecision(2) << mouseprice << "\t\t   " << "$" << (mousenumber * mouseprice) << endl;
  154.         cout << "Razer Keyboard Wired \t "<< keyboardnumber << "\t\t\t" << "$" << setprecision(2) << keyboardprice << "\t\t   " << "$" << (keyboardnumber * keyboardprice) << endl;
  155.         cout << "SATA USB 8GB Flash Drive \t "<< usbnumber << "\t\t\t" << "$" << setprecision(2) << usbprice << "\t\t   " << "$" << (usbnumber * usbprice) << endl;
  156.         cout << "HDMI Full 1080p 6ft Cable \t "<< hdminumber << "\t\t\t" << "$" << setprecision(2) << hdmiprice << "\t\t   " << "$" << (hdminumber * hdmiprice) << endl;
  157.         cout << "Lazer Pointer \t "<< lazernumber << "\t\t\t" << "$" << setprecision(2) << lazerprice << "\t\t   " << "$" << (lazernumber * lazerprice) << endl;
  158.         cout << "Power Cord \t "<< powernumber << "\t\t\t" << "$" << setprecision(2) << powerprice << "\t\t   " << "$" << (powernumber * powerprice) << endl;
  159.  
  160.         cout << "\t\t Subtotal \t $" << subtotal << endl;
  161.         cout << "\t\t Tax \t\t $" << tax << endl;
  162.         cout << "\t\t Total \t\t $" << total << endl;
  163.  
  164. }
  165.  
Jan 20 '10 #1
3 1893
Markus
6,050 Expert 4TB
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.
Jan 20 '10 #2
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!
Jan 21 '10 #3
johny10151981
1,059 1GB
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
Jan 22 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Chris Geerdink | last post by:
combo with PHP. what is wrong with the Javascript? else { include("mysql.php"); $query1 = mysql_query("INSERT INTO gbook (naam, email, text) VALUES ('".$_POST."', '".$_POST."',...
0
by: Dirk Zimmermann | last post by:
Hi! I have a problem in a program. And I don't understand what is going on. I can code something, that the "error" doesn't occur anymore. But I still don't know the reason and this is...
1
by: Matthew Wilson | last post by:
I need to write a function crc(msg, len) that gets a char array of length len and then calculates the crc32 for the code. I don't understand what's going wrong in the code I have. It goes...
6
by: Simon Mansfield | last post by:
Im trying to make a C program that takes in a date (birthday) and tells the user how many days it has been since that date. So far I have got this... It compiles ok but then crashes, with no idea...
7
by: Mike Barnard | last post by:
It's a simple test... VERY SIMPLE. But... In an external stlyesheet some attributes don't show. With the same styles cut and pasted to the test internally it works as expected. Anyone tell...
4
by: Wally | last post by:
int main(int argc, char *argv) { int c; int port=0; char *srvname = NULL; extern char *optarg; /* **Determine whether we are converting a port **number to a server name or vice versa. */
3
by: nahiyan13 | last post by:
hey there I have been given a problem where i have to write a function integerPower(base,exponent)that returns the value base^exponent(in words meaning if base is 2 and exponent is 3 then the...
5
by: islayer | last post by:
can someone tell me what is wrong with the bold code? i am just learning perl. the program should create a perl file with a random name (5 letters, followed by a number), but the name is always just...
1
by: x40 | last post by:
I try to learn python thru solving some interisting problem, found google trasure hunt, write first program ( but cant find whats wrong). # Unzip the archive, then process the resulting files to...
26
by: Muzammil | last post by:
whats the beauty of "malloc" over "new" why its helpful for programmer.for its own memory area.??
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.