Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that helps the user -- prospectively a manager at the company --

ID: 3538196 • Letter: W

Question

Write a program that helps the user -- prospectively a manager at the company -- determine the budget for the next year. You should gather from them both the budgetted amount for last year and the amount spent for last year. For each, the user mayenter the currency units either in front orin back of the value. If they enter neither, you are to assume that currency is measured in dollars ($). If they enter something in both places, you'll have to ask them -- or perhaps be terribly clever -- to decide which thing is the currency unit.

Then calculate for them the percent of their budget that was used -- this may be over 100%. This percent should be roundedto the nearestwhole percent. Finally, force their spending upto the nearest$100and tell them that this figure will be their budget for the next year.

Make sure you print their results in a nice way. Use the currency unit they entered at the beginning (or $). Be sure to place this marker either in front or in back of the new budget just like they entered it. (If they didn't enter the units, place the $in the front by default.) And always print two decimal places (even though we shouldn't have any change to report). Also print the percent of their budget that was used with a percent sign (%) after it.

Explanation / Answer

#include<iostream>

#include<string>

#include<ctype.h>

using namespace std;


//Returns float value from the string and also the currency symbol along with

//the position(beginning or end) are reflected because they are passed by reference

float getFloatValue(string s,int length,char& currency,bool& currencyPosition)

{

string subStr;

float floatValue;

bool flag = 0;

if(!isdigit(s[0])){

currency = s[0];

if(!isdigit(s[length-1])){

subStr = s.substr(1,length-2);

}

else subStr = s.substr(1,length-1);

currencyPosition = 0;

}

else if(!isdigit(s[length-1])){

currency = s[length-1];

subStr = s.substr(0,length-1);

currencyPosition = 1;

}

else {

subStr = s;

currency = '$';

currencyPosition = 0;

}

int i,len = subStr.length();

for(i=0;i<len;i++){

if(!isdigit(subStr[i])){

if(flag == 0 && subStr[i] == '.'){

flag = 1;

continue;

}

else{

cout<<"Your currency format is not correct. Please enter in a proper manner and try again ";

exit(0);

}

}

}

floatValue = atof(subStr.c_str());

return floatValue;

}


int main()

{

//Random initializations for testing;

string budget = "$12000.70$";

string spending = "$10085.56";

string subBudget;

string subSpending;

float Budget;

float Spending;

float percent;

char currency1,currency2;

bool currencyPosition1,currencyPosition2;

cout<<"Please enter the budgetted amount for last year : ";

getline(cin,budget);

cout<<"Please enter the total amount spent last year : ";

getline(cin,spending);

int l1 = budget.length();

int l2 = spending.length();

Budget = getFloatValue(budget,l1,currency1,currencyPosition1);

Spending = getFloatValue(spending,l2,currency2,currencyPosition2);


percent = (Spending/Budget)*100;

int intPercent =(int) (percent + 0.5);

float nearestPercent = intPercent;

int newIntBudget =(int) ((Spending + 50)/100 )*100;

float newBudget = (float)newIntBudget;

  

//The if else takes care whether the currency symbol has to be printed

//in the beginning or in the end

if(currencyPosition1 == 0){

printf("Your Budget for this year was %c%.02f ",currency1,Budget);

}

else{

printf("Your Budget for this year was %.02f%c ",Budget,currency1);

}


if(currencyPosition2 == 0){

printf("Total Amount spent by you this year was %c%.02f ",currency2,Spending);

}

else{

printf("Total Amount spent by you this year was %.02f%c ",Spending,currency2);

}


printf("You spent %.02f%% of your Budget this year ",nearestPercent);


if(currencyPosition1 == 0){

printf("Your Budget for the next year would be %c%.02f ",currency1,newBudget);

}

else{

printf("Your Budget for the next year would be %.02f%c ",newBudget,currency1);

}


return 0;

}


  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote