Our professor wants us to display 2 decimal points even if they're both 0. For example if my answer is 1435, it needs to be 1435.00. How Do I go about doing this? Here is my code so far. It is a budget projection program.
#include <iostream>
#include <climits>
#include <cmath>
usingnamespace std;
void final_print (double new_budget,bool location)
{
if (location)
{cout<<new_budget<<" $ \n";}
else
{cout<<"$"<<new_budget<<endl;}
}
bool isValid(char peeked)
{
if(peeked == '1'||peeked== '2'||peeked =='3'||peeked =='4'||peeked =='5'
||peeked =='6'||peeked =='7'||peeked =='8'||peeked =='9'||peeked =='0')
{returnfalse;}
else
{returntrue;}
}
int main ()
{
cout << "\t\tWelcome to the Budget Program!";
bool again;
char response;
do
{
char dollar_sign2 = '\0';
char dollar_sign = '\0';
double budget;
double spent;
cout << "\n\nWhat is your budget for the year? ";
if (isValid(cin.peek()))
{
cin >> dollar_sign;
}
cin >> budget;
bool at_end = false;
if(cin.peek() == '$')
{at_end=true;}
cout << "\nHow much have you spent? ";
if (isValid(cin.peek()))
{
cin >> dollar_sign2;
}
cin >> spent;
cin.ignore (INT_MAX,'\n');
double used = floor((spent/budget)*100+.5);
//these if else statements will display the percentage used
if (used > 100)
{
cout << "\n\tYou have used " << used << "% of your budget!"
<<"\n\tYour overspending won't last long in this world!";
}
elseif (used < 100)
{
cout <<"\n\tYou have used "<<used<< "% of your budget!"
<< "\n\tUse the extra money for a vacation, Smith!";
}
else // (used == 100)
{
cout << "\n\tYou have used "<<used << "% of your budget!"
<< "\n\tHopefully the money was put in the right places!";
}
//the next if/else statements will display the new budget
double new_budget;
if (spent > budget)
{
new_budget = ceil(spent/100+.1)*100;
cout << "\n\nYour new budget for next year will be ";
final_print(new_budget,at_end);
}
elseif (spent < budget)
{
new_budget = budget;
cout << "\n\nYou spent less than the budget! Nice! Your budget will remain at ";
final_print(new_budget,at_end);
}
cout <<"\n\n\n\t\tWould you like to run this program again? ";
cin >> response;
response = tolower(response);
cin.ignore(INT_MAX,'\n');
if (response == 'y')
{
again = true;
}
else
{
again = false;
cout << "\n\n\t\tThanks For Using My Program! Bye!";
}
} while (again);
return 0;