Feb 11, 2017 at 10:01pm UTC
When I enter money=1, it returns a quarter (true), but when I enter 0.85, 0.95, it keeps returning 0.
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
#include <iostream>
#include <string>
using namespace std;
string mReturn(double );
string changeE(double );
int main(){
double change, money, price=0.75;
string str;
cout<<"Enter the money: " ;
cin>>money;
change=money-price;
cout<<"Your change: " <<change;
str=changeE(change);
cout<<"\nChange: " <<str;
}
string changeE(double ch){
string chH;
chH=mReturn(ch);
return chH;
}
string mReturn(double ch){
string currency;
if (ch==0.25) currency="a quarter." ;
else if (ch==0.2) currency="2 dimes" ;
else if (ch==0.15) currency="a dime and a nickel." ;
else if (ch==0.1) currency="a dime." ;
else if (ch==0.05) currency="a nickel." ;
else currency="0" ;
return currency;
}
When I tried to use this code, eliminate money, it returns the true results.
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
#include <iostream>
#include <string>
using namespace std;
string mReturn(double );
string changeE(double );
int main(){
double change;
string str;
cout<<"Enter the change: " ;
cin>>change;
cout<<"Your change: " <<change;
str=changeE(change);
cout<<"\nChange: " <<str;
}
string changeE(double ch){
string chH;
chH=mReturn(ch);
return chH;
}
string mReturn(double ch){
string currency;
if (ch==0.25) currency="a quarter." ;
else if (ch==0.2) currency="2 dimes" ;
else if (ch==0.15) currency="a dime and a nickel." ;
else if (ch==0.1) currency="a dime." ;
else if (ch==0.05) currency="a nickel." ;
else currency="0" ;
return currency;
}
I think the problem is change=money-price;
However, I can't figure what's going wrong.
Last edited on Feb 11, 2017 at 10:05pm UTC
Feb 11, 2017 at 10:08pm UTC
print out money to many digits.
it could just be floating point storage. sometimes you get
0.1 represented as 0.99999999999999999999999999999999999999999999999
or the like and equality fails. That seems unlikely, but something weird is going on here and comparison of equality with doubles is kind of a no-no.
Last edited on Feb 11, 2017 at 10:16pm UTC
Feb 11, 2017 at 10:22pm UTC
1 2 3 4 5 6 7 8 9 10 11
string mReturn(double ch){
string currency;
if (ch==0.25) currency="a quarter." ;
else if (ch==0.2) currency="2 dimes" ;
else if (ch==0.15) currency="a dime and a nickel." ;
else if (ch==0.1) currency="a dime." ;
else if (ch==0.05) currency="a nickel." ;
else currency="0" ;
return currency;
}
0.85 and 0.95 don't match any of the if/else-if statements, thus the else statement will be executed, returning 0.
Also, try to avoid floating point values for currency, as they can't store numbers precisely. Prefer to use integers, one for whole dollars and another for cents.
Last edited on Feb 11, 2017 at 10:24pm UTC
Feb 11, 2017 at 10:30pm UTC
1 2 3
try to avoid floating point values for currency,
as they can't store numbers precisely. Prefer to use integers,
one for whole dollars and another for cents.
Thanks for that! It now works perfectly!
Last edited on Feb 11, 2017 at 10:38pm UTC
Feb 12, 2017 at 12:27am UTC
currency is a specific example of what I was saying about comparison of equality with doubles. For the same reasons.