Thread: Vending Machine
View Single Post
Join Date: Jun 2004
Posts: 34
Reputation: Fili is an unknown quantity at this point 
Solved Threads: 0
Fili's Avatar
Fili Fili is offline Offline
Light Poster

Re: Vending Machine

 
0
  #4
Jun 19th, 2004
ok... it's ready but it doesn't cover the part where the option is invalid

  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:
Reply With Quote