Output error
May 21, 2014 at 5:10pm UTC
I am trying to get the output income to be only to 2 decimal places, I attempted the cin.precision(2) but the number outputted is something way off. Let me know what I need to change to make it display correctly. I don't know what I am missing.
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
# include <iostream>
# include <string>
# include <iomanip>
using namespace std;
int main()
{
double taxableIncome;
double incomeTax = 0.00;
double error;
string returnType;
char tryAgain;
bool isAgain = false ;
char answer;
cout << "Kirk's Tax Calculator" ;
cout << "\nCS 1410 Project #8" << endl;
while (true )
{
do
{
error = 0;
cout << "\nPlease enter in your taxable income." ;
cout << "\nThis must be a positive value: " ;
cin >> taxableIncome;
if (taxableIncome < 0)
{
cout << "Please enter a valid integer" << endl;
cin >> taxableIncome;
cin.clear();
}
} while (error);
do
{
isAgain = false ;
cout << "\nPlease enter an \"m\" if married and filing joint return," ;
cout << "\nor \"s\" if filing a single return or married filing separate return: " ;
cin >> returnType;
if (returnType == "m" || returnType == "M" )
{
if (taxableIncome > 0 && taxableIncome < 1726)
{
incomeTax = (((taxableIncome - 0)*.023) + 0);
}
if (taxableIncome > 1726 && taxableIncome < 5176)
{
incomeTax = (((taxableIncome - 1726)*.033) + 40);
}
if (taxableIncome > 5176 && taxableIncome < 8626)
{
incomeTax = (((taxableIncome - 5176)*.052) + 175);
}
if (taxableIncome > 8626)
{
incomeTax = (((taxableIncome - 8626)*.075) + 390);
}
cout << "\nYour taxable income is $" << taxableIncome;
cout << "\nand you are filing a joint return." << endl;
cout.precision(2);
cout << "Your income tax will be $" << incomeTax << endl;
}
else
if (returnType == "s" || returnType == "S" )
{
if (taxableIncome > 0 && taxableIncome < 863)
{
incomeTax = (((taxableIncome - 0)*.023) + 0);
}
if (taxableIncome > 863 && taxableIncome < 2588)
{
incomeTax = (((taxableIncome - 863)*.033) + 25);
}
if (taxableIncome > 2588 && taxableIncome < 4313)
{
incomeTax = (((taxableIncome - 2588)*.052) + 85);
}
if (taxableIncome > 4313)
{
incomeTax = (((taxableIncome - 4313)*.075) + 181);
}
cout << "\nYour taxable income is $" << taxableIncome;
cout << "\nand you are filing a single return." << endl;
cout.precision(2);
cout << "Your income tax will be $" << incomeTax << endl;
}
else
{
isAgain = true ;
cout << "that is an invalid character" << endl;
}
} while (isAgain);
// Loop to ask user is he/she would like to play again.
while (true )
{
// Get user response.
cout << "Would you like to do another calculation (y or n)? " ;
cin >> answer;
cin.ignore();
// Check if proper response.
if (answer == 'n' || answer == 'N' || answer == 'y' || answer == 'Y' )
{
break ;
}
else
{
cout << "Please enter \'y\' or \'n\'...\n" ;
}
}
// Check user's input and run again or exit;
if (answer == 'n' || answer == 'N' )
{
std::cout << "Thank you for playing!" ;
break ;
}
else
{
cout << "\n\n\n" ;
}
}
return 0;
}
May 21, 2014 at 5:20pm UTC
I figured it out!
cout << fixed << setprecision(2);
Topic archived. No new replies allowed.