Write this program so that the monthly budget and the indefinite list of expense
ID: 3769750 • Letter: W
Question
Write this program so that the monthly budget and the indefinite list of expenses is input from a data file.
I recommend that you organize your input data file as follows,
1855
Car payment
201.00
Rent
950.00
Insurance
200.00
Supermarkets, food
400.00
Restaurants
100.00
Internet service
56.00
Cell phone
109.00
Gasoline
175.00
1. First test case
Budget for the Month: $1855.00
Expenses Amounts
-------------------- ----------
Car payment 201.00
Rent 950.00
Insurance 200.00
Supermarkets, food 400.00
Restaurants 100.00
Internet service 56.00
Cell phone 109.00
Gasoline 175.00
---------------------------------
Sum of expenses: $2191.00
Careful, you've exceeded your budget by $336.00 dollars.
Largest budgeted item: $950.00 for Rent
Smallest budgeted item: $56.00 for Internet service
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include <cstring>
#define MAX 1000
using namespace std;
int main() {
ifstream infile;
char name[MAX],item[MAX][MAX];
float dollar,budget,sum = 0;
int count = 0;
float price[MAX];
infile.open("test1.txt");
if (!infile.is_open()) {
cerr<<"cannot open this file";
}
infile.getline(name,MAX);
budget = atof(name);
cout<<"Budget for the month: $"<<budget<<endl;
cout<<"Expenses Amounts ";
cout<<"-------- ------- ";
while(infile.getline(name,MAX))
{
strcpy(item[count],name);
infile.getline(name,MAX);
price[count] = atof(name);
sum += atof(name);
cout<<item[count]<<" "<<price[count]<<endl;
count++;
}
cout<<"Sum of Expenses: $"<<sum<<endl;
if(sum>budget)
cout<<"Careful, you have exceeded your budget by $"<<sum-budget<<endl;
else
cout<<"Congratulations!!, your monthly Expenses are within budget. Savings of $"<<budget-sum<<endl;
int i,max_i = 0,min_i = 0;
for(i=1;i<count;i++)
if(price[i]>price[max_i])
max_i = i;
for(i=1;i<count;i++)
if(price[i]<price[min_i])
min_i = i;
cout<<"Largest budget item: $"<<price[max_i]<<" for "<<item[max_i]<<endl;
cout<<"Smallest budget item: $"<<price[min_i]<<" for "<<item[min_i]<<endl;
return 1;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.