Okay, the problem that i\'m having is that when the program compiles it for 10.0
ID: 3638342 • Letter: O
Question
Okay, the problem that i'm having is that when the program compiles it for 10.00 * 5 (see text file below) there is no .00 at the end of 5 and it skips the character and reads in the 14 as the cents part. If anyone could point out where in my code i'm going wrong or help me fix it, it would be greatly appreciated! i also put a picture at the bottom of where it goes wrong.
---------------------------------------------------------------------------------
#include
#include
#include
using namespace std;
class money{
private:
public:
int dollars;
int cents;
money();
void setDollars(int value);
void setCents(int value);
int add(money& value);
int subtract(money& value);
int multiply(money& value);
int divide(money& value);
bool compare(money& value);
bool isEqual(money& value);
};
money::money(){
dollars = 0;
cents = 0;
}
void money::setDollars(int value){
money::dollars = value;
}
void money::setCents(int value){
money::cents = value;
}
int money::add(money& value){
return ((value.dollars * 100) + value.cents) + ((money::dollars * 100) + money::cents);
}
int money::subtract(money& value){
return ((money::dollars * 100) + money::cents)-((value.dollars * 100) + value.cents) ;
}
int money::multiply(money& value){
return ((value.dollars) + value.cents) * ((money::dollars * 100) + money::cents);
}
int money::divide(money& value){
return ((money::dollars *100) + money::cents) / (value.dollars + value.cents);
}
bool money::compare(money& value){
if (((money::dollars * 100) + money::cents) > ((value.dollars * 100) + value.cents))
return true;
else{
return false;
}
}
bool money::isEqual(money& value){
if (((money::dollars * 100) + money::cents) == ((value.dollars * 100) + value.cents))
return true;
else{
return false;
}
}
int main(){
// Declare Class Values for Both operands (could be done in an array, but this is easier to manage)
money firstValue;
money secondValue;
char operand ='NULL' ;
bool thisResult;
string ans;
ifstream input;
// open file to read
input.open("MoneyOps.txt");
while(input.peek() != EOF) {
// read first integer and set main(dollars) to integer
input >> firstValue.dollars;
input.ignore(1, '.');// ignoring the dot
// read second integer and set main(cents) to integer
input >> firstValue.cents;
// read operand
input>>operand;
// read set 2 integer and set money(dollars) to integer
input >> secondValue.dollars;
input.ignore(1, '.');
// read set 2 integer 2 and set money(cents) to integer
input >> secondValue.cents; // if has . cents then store it to secondvalue.cents
// switch case for operand to determine function type
double result; // for integer operations
bool thisResult;
// checking for operand value
switch (operand){
case '+':
result = firstValue.add(secondValue);
cout << firstValue.dollars << "." << firstValue.cents << " + "
<< secondValue.dollars << "." << secondValue.cents << " = "
<< (result / 100)<break;
case '-':
result = firstValue.subtract(secondValue);
cout << firstValue.dollars << "." << firstValue.cents << " - "
<< secondValue.dollars << "." << secondValue.cents << " = "
<< (result / 100)<break;
case '/':
result = firstValue.divide(secondValue);
cout << firstValue.dollars << "." << firstValue.cents << " / "
<< secondValue.dollars << "." << secondValue.cents << " = "
<< (result/100)<break;
case '*':
result = firstValue.multiply(secondValue);
cout << firstValue.dollars << "." << firstValue.cents << " * "
<< secondValue.dollars << "." << secondValue.cents << " = "
<< (result / 100)<break;
case '>':
case '<':
thisResult = firstValue.compare(secondValue);
// to get exact false or true instead of binary value of 0 or 1
switch(thisResult)
{case 1:
ans = "true";
break;
case 0:
ans = "false";
break;}
cout << firstValue.dollars << "." << firstValue.cents <<" "<< operand <<" "
<< secondValue.dollars << "." << secondValue.cents << " = "
<< ans<
break;
case '=':
thisResult = firstValue.isEqual(secondValue);
// to get exact false or true instead of binary value of 0 or 1
switch(thisResult)
{case 1:
ans = "true";
break;
case 0:
ans = "false";
break;}
cout << firstValue.dollars << "." << firstValue.cents << " "<<< secondValue.dollars << "." << secondValue.cents << " = "
<< ans<break;
};
} // end of while loop
system("pause");
return 0;
}
-------------------------------------------------------------------------------------------------
Text file accompanied:
12.95 + 2.10
3.25 > 32.50
32.50 > 3.25
3.25 < 32.50
10.01 - 9.99
10.00 * 5
14.99 - 14.99
10.00 / 3
5.45 < 5.45
5.45 > 5.45
5.45 = 5.45
2.63 = 2.62
1.55 + 8.45
9.00 / 4
1.25 * 3
Explanation / Answer
this is going to cause trouble. take the input into a float variable. now take it's integral part as dollars float is 100 fractional part Implementation float f; input >> f; int dollars = f; int cents = (f-dollars)*100
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.