Jump to content
  • Advertisement
Sign in to follow this  
snowfell

soda machine help

This topic is 3782 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey I am currently making a soda machine example using C++ and I have a issue with the code. To me everything looks fine, but something is wrong with the inserting of the coins. What I am trying to do is set up a system that has a price and a current amount of money. All you do is put in a number that represents a coin and then that amount of money is added to ur total. After that total of money added is equal to or greater than the price, it says you get you drink and gives you change if any. So this is what I have so far, if you notice anything please let me know: #include <iostream> using namespace std; int main() { int input=0; float price=1.25; float total=0.0; int close; cout << "Welcome to the soda machine, please insert coins\n"; while(total<price) { cout << "Money needed: $" << (price-=total) << "\n"; cout << "1 inputs a quarted, 2 inputs a dime, 3 inputs a nickel, 4 inputs a penny\n"; cin >> input; switch(input) { case 1: total+=.25; break; case 2: total+=.10; break; case 3: total+=.05; break; case 4: total+=.01; break; default: cout << "Not money, please insert corrent coins.\n"; break; } input=0; } cout << "You get $" << (total-price) << " change.\n"; cout << "Thank you, please enjoy your drink.\n"; //terminate the program cin >> close; cin.get(); }

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by snowfell

cout << "Welcome to the soda machine, please insert coins\n";

while(total<price)
{
cout << "Money needed: $" << (price-=total) << "\n";


That should probably be (price - total) instead.

What you have now is subtracting total from price, and assigning it back to price.

Share this post


Link to post
Share on other sites
Right off hand, cout << ... << (price-=total) looks suspicious. You want to say cout << price - total;. What you essentially have written is price = price - total which is probably decrementing the price.

Share this post


Link to post
Share on other sites
To clarify even further, the reason you don't want to change the price is because you're using it to check if your while() loop should continue running. The logic is slightly wrong.

Also, a tip for future posts in this forum, include your code in {source} and {/source} tags (except then with [ and ]), to get nice formatting and to keep the indentation.

Share this post


Link to post
Share on other sites
C++ has a keyword 'const' which marks certain values as constants. The price is one such number. By marking it const, the compile would have issued an error message.

Example:
const float price = 1.25;

Secondly, get out of the habit of declaring all your variables at the top. Declare them as close as possible to their use. In this code, you have the line "input = 0" at the end of your loop. This is redundant if you scope the variable inside the loop:

#include <iostream>
using namespace std;

int main()
{
const float price = 1.25;

cout << "Welcome to the soda machine, please insert coins\n";

float total = 0.0;

while(total < price)
{
cout << "Money needed: $" << (price - total) << "\n";
cout << "1 inputs a quarted, 2 inputs a dime, 3 inputs a nickel, 4 inputs a penny\n";

int input; // no need to give it a value, we are about to read into it
cin >> input;

switch(input)
{
case 1:
total += .25;
break;

case 2:
total += .10;
break;

case 3:
total += .05;
break;

case 4:
total += .01;
break;

default:
cout << "Not money, please insert corrent coins.\n";
break;
}
}

cout << "You get $" << (total - price) << " change.\n";
cout << "Thank you, please enjoy your drink.\n";

//terminate the program
int close;
cin >> close;
cin.get();
}



Don't be afraid to use more whitespace, especially around operators. It makes the code easier to read.

Share this post


Link to post
Share on other sites
Quote:
Original post by c4c0d3m0n
To clarify even further, the reason you don't want to change the price is because you're using it to check if your while() loop should continue running. The logic is slightly wrong.

Also, a tip for future posts in this forum, include your code in {source} and {/source} tags (except then with [ and ]), to get nice formatting and to keep the indentation.


Exactly, that was my issue right their. Thanks a lot guys, that was simple enough.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!