I'm writing a simple program for my class to calculate and display certain numbers, and my code all works with no issue, however on the display some of my numbers are getting rounded where I don't want them to be. I have the precision set to two decimal places because a few of the input numbers are decimals, but for example, if the answer is 3000, the number 2999 is being displayed.
Here is my code, if this helps
int main()
{
double expense = 0.0;
double expenseTotal = 0.0;
double income = 0.0;
double incomeTotal = 0.0;
double netProfit = 0.0;
double netIncome = 0.0;
double netExpense = 0.0;
int incNo = -1;
int expNo = -1;
cout << "ENTER A NEGATIVE NUMBER TO STOP INCOME AMOUNT DATA ENTRY." << endl << endl;
do
{
cout << "Enter Income Amount: ";
cin >> income;
netIncome = income;
incomeTotal += netIncome;
incNo += 1;
} while (income > 0);
cout << endl << "ENTER A NEGATIVE NUMBER TO STOP EXPENSE AMOUNT DATA ENTRY." << endl << endl;
do
{
cout << "Enter Expense Amount: ";
cin >> expense;
netExpense = expense;
expenseTotal += netExpense;
expNo += 1;
} while (expense > 0);
cout << endl << "Total of the " << incNo << " income amounts entered ----> $" << fixed << setprecision(2) << incomeTotal << endl;
cout <<"Total of the " << expNo << " expense amounts entered ----> $" << fixed << setprecision(2) << expenseTotal << endl;
netProfit = incomeTotal - expenseTotal;
if (netProfit > 0)
{
cout << "Net Profit earned ----> $" << fixed << setprecision(2) << netProfit;
cout << endl;
}
else
{
cout << "Net Loss incurred ----> $" << fixed << setprecision(2) << netProfit;
cout << endl;
cout << "Note . . . Loss figire most be reported to company CEO." << endl << endl;
}
return 0;
}