943,490 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 21530
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 17th, 2004
1

Vending Machine

Expand Post »
Yes i know everyone hates homework help but this was a last place i could go I am writing a program that stimulates a vending machine and i have written almost all of the code its just not working correctly I am stressing out because it is due by tomorrow and was wondering if anyone would be able to help me out in any way possible i have attached the *.cpp file and would be willing to do anything if someone would look over it Im not asking for you to completly due it just help me becase i am stuck and do not know were to turn.
In the program it is to give change back which i am having trouble doing so because it breaks it down into half dollars dollars dimes quarters and nickles its really the only problem i have i have written code that i believed to work but it has not if anyone is out there please help
This is what the outcome should look like http://www.cs.ua.edu/114/Summer2004/Programs/prog2.htm
Attached Files
File Type: cpp azar_thomas.cpp (2.4 KB, 555 views)
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
phish1429 is offline Offline
1 posts
since Jun 2004
Jun 18th, 2004
0

Re: Vending Machine

Gee, you sure were in a hurry but the lack of full stops really mangles what you say.

This is not really a place to ask for homework answers.But I did look at your code and you use a lot of variables like a,b,c etc for what resonable puprose I cant guess.Also code is a bit messed up in the sense that it too packed where you calculate the vals so I could not understant it too much either.

But here's something :
Lesson1:Code clearly and use spaces and newlines.They will save your life when it comes to debugging.Noodle code is a death trap.(Your code is not too noodlely except in the if else part).

Lesson 2:Use funtions (I dont know if you know funtions)

Now your prob:One Idea would be to convert the entire amount in to the lowest denomintions availble(nickle?) and calulate using that.In the end convert it back to dollars and show what ever remanins as nickles.Dont bother with quaters unless you have to.
Not much but i could not understand you code nor what was you prob,nor do i have a compiler handy right now. :0
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Jun 19th, 2004
0

Re: Vending Machine

I'm writing your program though it might be too late!
Reputation Points: 16
Solved Threads: 0
Light Poster
Fili is offline Offline
34 posts
since Jun 2004
Jun 19th, 2004
0

Re: Vending Machine

ok... it's ready but it doesn't cover the part where the option is invalid

C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2. #include <conio.h>
  3. int dol,cen;
  4. unsigned int cb=0,ch;
  5. void convert(int cb)
  6. {
  7. dol=0;cen=0;
  8. while (cb>=100) {
  9. cb-=100;
  10. dol++;
  11. }
  12. cen=cb;
  13. }
  14.  
  15. void read()
  16. {
  17. convert(cb);
  18. cout <<"The current balance is\t" <<dol << " dollars and "<< cen <<" cents "<<endl;
  19. cout <<"Please choose from one of these options"<<endl;
  20. cout <<"\n";
  21.  
  22. cout <<"5 - Deposit a nickle\t";
  23. cout <<"45 - Buy some gum for 45 cents"<<endl;
  24.  
  25. cout <<"10 - Deposit a dime\t"<<"\t";
  26. cout <<"55 - Buy crackers for 55 cents"<<endl;
  27.  
  28. cout <<"25 - Deposit a quarter\t";
  29. cout <<"60 - Buy a soft drink for 60 cents"<<endl;
  30.  
  31. cout <<"50 - Deposit a half dollar\t";
  32. cout <<"70 - Buy a candy bar for 70 cents"<<endl;
  33.  
  34. cout <<"100 - Deposit a dollar bill\t";
  35. cout <<"85 - Buy some chips for 85 cents"<<endl;
  36.  
  37. cout <<"0 - Request coin return and quit\t";
  38.  
  39. cout <<"\n";
  40.  
  41. cout << "\n" <<"Enter a number to choose an option:\t";
  42. cin >> ch;
  43. }
  44. void main()
  45. {
  46. clrscr();
  47. cout <<"This program simulates a vending machine"<<endl;
  48. read();
  49. while (ch!=0)
  50. {
  51. cout<<"\n\n";
  52. if (ch==5) { cb+=5;ch=-1;}
  53. if (ch==10){ cb+=10;ch=-1;}
  54. if (ch==25) {cb+=25;ch=-1;}
  55. if (ch==50) {cb+=50;ch=-1;}
  56. if (ch==100) {cb+=100;ch=-1;}
  57.  
  58. if (ch==0)
  59. {
  60. cout<<"Quiting\n";
  61. while (cb%100==0&&cb>0) {cout<<"Returning a dollar\n";cb-=100;}
  62. while (cb%50==0&&cb>0) {cout<<"Returning a half dollar\n";cb-=50;}
  63. while (cb%25==0&&cb>0) {cout<<"Returning a quarter\n";cb-=25;}
  64. while (cb%10==0&&cb>0) {cout<<"Returning a dinme\n";cb-=10;}
  65. while (cb%5==0&&cb>0) {cout<<"Returning a nickel\n";cb-=5;}
  66. ch=-1;
  67. }
  68.  
  69. if (ch==45)
  70. {
  71. if (cb>45) {
  72. cout<<"Here is your gum\n";
  73. cb-=45;
  74. }
  75. else cout<<"Error:Deposit more money\n";
  76. ch=-1;}
  77. if (ch==55)
  78. {
  79. if (cb>55) {
  80. cout<<"Here are your crackers\n";
  81. cb-=55;
  82. }
  83. else cout<<"Error:Deposit more money\n";
  84. ch=-1;}
  85. if (ch==60)
  86. {
  87. if (cb>60) {
  88. cout<<"Here is your soft drink\n";
  89. cb-=60;
  90. }
  91. else cout<<"Error:Deposit more money\n";
  92. ch=-1;}
  93. if (ch==70)
  94. {
  95. if (cb>70) {
  96. cout<<"Here is your candy bar\n";
  97. cb-=70;
  98. }
  99. else cout<<"Error:Deposit more money\n";
  100. ch=-1;}
  101. if (ch==85)
  102. {
  103. if (cb>85) {
  104. cout<<"Here are your chips\n";
  105. cb-=85;
  106. }
  107. else cout<<"Error:Deposit more money\n";
  108. ch=-1;}
  109. read();
  110. }
  111. cout << "GoodBye\n";
  112. }
:cheesy:
Reputation Points: 16
Solved Threads: 0
Light Poster
Fili is offline Offline
34 posts
since Jun 2004
Jun 19th, 2004
0

Re: Vending Machine

Quite nice,Fili
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Jun 19th, 2004
0

Re: Vending Machine

Thanks Fire Net Do you suppose it's too late for it to be of any use?
Reputation Points: 16
Solved Threads: 0
Light Poster
Fili is offline Offline
34 posts
since Jun 2004
Jun 19th, 2004
0

Re: Vending Machine

I dont think so,it will be of some use to someone,hey teachers usally accept a submission even if it a bit late.
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Jun 19th, 2004
0

Re: Vending Machine

Quote originally posted by FireNet ...
I dont think so,it will be of some use to someone
Thank you :cheesy:
Reputation Points: 12
Solved Threads: 0
Newbie Poster
MaxC is offline Offline
11 posts
since May 2004
Jun 20th, 2004
0

Re: Vending Machine

I'm not expert but I did programming in my 2 year course just finished and understood that vending machines work on the weight of coins.

:rolleyes:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MtBobcat is offline Offline
3 posts
since Jun 2004
Jun 20th, 2004
0

Re: Vending Machine

Quote originally posted by MtBobcat ...
I'm not expert but I did programming in my 2 year course just finished and understood that vending machines work on the weight of coins.

:rolleyes:
Well, that's what his problem said. Anyway i'm not a US or UK citizen so i'm not great at english What is a vending machine exactly?! :o
Reputation Points: 16
Solved Threads: 0
Light Poster
Fili is offline Offline
34 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: how to make colors
Next Thread in C++ Forum Timeline: regarding round robin algorithm





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC