First Time C++ Coder: Need Help With One Little Part (loop)
Sep 7, 2015 at 12:55am UTC
For my project, I need to make a program that would ask for a dollar amount for bottles that cost $2 and for every 5 bottles, the user would receive a free bottle until they had less than 5. I got most of it working but for some bottles, it wouldn't output correctly. For example, $68 should return 42 bottles total with 2 left over but it just returns 40 total and 0 left over. I know I need a loop somewhere but i'm not sure where. Any help would be appreciated. My code is below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
//Energy Drinks
#include<iostream>
#include <fstream>
using namespace std;
int main()
{
int dollars;
do
{
int dollars=0;
int bottle=0;
int total=0;
int newcap=0;
int oldcap=0;
int leftover=0;
int change=0;
cout << "\n" ;
cout << "How much money would you like to insert? $" ;
cin >> dollars;
cout << "\n" ;
if (dollars==0)
{
return 0;
}
bottle=dollars/2;
newcap=bottle/5;
oldcap=bottle%5;
if ((newcap+oldcap)%5!=0)
{
bottle=bottle+(newcap+oldcap)/5;
}
if (newcap%5==0)
{
newcap=newcap+newcap/5;
}
total=bottle+newcap;
if (dollars%2!=0)
{
change=dollars%2;
}
cout << "Your total bottle amount is: " ;
cout << total;
cout << ".\n" ;
cout << "Your leftover cap amount is: " ;
cout << total%5;
cout << ".\n" ;
cout << "You have $" ;
cout << change;
cout << " left in change." ;
cout << "\n" ;
}while (dollars!=0);
return 0;
}
Last edited on Sep 7, 2015 at 12:56am UTC
Sep 7, 2015 at 10:18am UTC
Hi!
I am not sure if I understand exactly what you want to do. I have no idea on what you're trying to do after line 34. So I cannot tell you what to fix and how to fix.
However, I can point out some behind the scene work which might help you see why you're getting that unexpected output.
--> Read my comments below, I am showing you the math that is happening behind the scenes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
//Energy Drinks
#include<iostream>
#include <fstream>
using namespace std;
int main()
{
int dollars;
do
{
int dollars=0;
int bottle=0;
int total=0;
int newcap=0;
int oldcap=0;
int leftover=0;
int change=0;
cout << "\n" ;
cout << "How much money would you like to insert? $" ;
cin >> dollars; ///- Input 68, dollars = 68
cout << "\n" ;
if (dollars==0)
{
return 0;
}
bottle=dollars/2; ///- bottle = 68/2 = 34
newcap=bottle/5; ///- newcap = 34/5 = 6
oldcap=bottle%5; ///- oldcap = remOf(34/5) = 4 *** remOf = remainder of = modulus ***
if ((newcap+oldcap)%5!=0) ///- remOf((6+4)/5) == 0 -- condition false (so don't do anything)
{
bottle=bottle+(newcap+oldcap)/5;
}
if (newcap%5==0) ///- remOf(6/5) != 0 -- condition false (so don't do anything)
{
newcap=newcap+newcap/5;
}
total=bottle+newcap; ///- total = 34+6 = 40
if (dollars%2!=0) ///- remOf(68/2) == 0 -- condition false (so don't do anything)
{
change=dollars%2;
}
cout << "Your total bottle amount is: " ;
cout << total; ///- total = 40 and hence it couts 40
cout << ".\n" ;
cout << "Your leftover cap amount is: " ;
cout << total%5; ///- remOf(40/5) = 0 and hence it couts 0
cout << ".\n" ;
cout << "You have $" ;
cout << change; ///- value of change remains 0 since you initialized it.
cout << " left in change." ;
cout << "\n" ;
}while (dollars!=0);
return 0;
}
Last edited on Sep 7, 2015 at 10:20am UTC
Sep 7, 2015 at 2:19pm UTC
Thank you but I solved it!
Topic archived. No new replies allowed.