§ Important : § Code should be properly aligned and wellcommented. § Follow c/c+
ID: 3615154 • Letter: #
Question
§ Important:
§ Code should be properly aligned and wellcommented.
§ Follow c/c++ rules while writing variables names,function names etc
On the basis of the given scenario create a file Expenses.txt,Open the file and copy into another Expenses2.txt, Also showoutput on screen
A college has announced the total budget of 50,000Rs.for eachgame. Games are done four times in a year. Take expenses as aninput from user.
2. If the expenses are greater than 60% and less than 80%than show “Expensive ”
3. If the expenses are greater than 50% and less than 60%than show “Less Expensive ”
4. If the expenses are greater than 40% and less than 50% thanshow “Not Costly”.
5. If the expenses are less than 40% than show“Best”.
Explanation / Answer
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int cost1,cost2,cost3,cost4,tot,percent,n,i;
string game;
ofstream output;
ifstream input;
output.open("Expenses.txt");
if(output.fail())
{ cout<<"Expenses file did not open for inputplease check it ";
system("pause");
return 1;
}
cout<<"how many games are there? ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter the game name: ";
cin>>game;
cout<<"Enter cost for game 1: ";
cin>>cost1;
cout<<"Enter cost for game 2: ";
cin>>cost2;
cout<<"Enter cost for game 3: ";
cin>>cost3;
cout<<"Enter cost for game 4: ";
cin>>cost4;
tot=cost1+cost2+cost3+cost4;
percent=tot/50000.*100.;
output<<game<<" "<<cost1<<""<<cost2<<" "<<cost3<<""<<cost4<<" "<<percent<<" ";
if(percent>=80)
output<<"Very Expesive ";
else if(percent>=60)
output<<"Expesive ";
else if(percent>=50)
output<<"Less Expesive ";
else if(percent>=40)
output<<"Not Costly ";
else
output<<"Best ";
}
output.close();
input.open("Expenses.txt");
if(input.fail())
{ cout<<"Expenses filedid not open for input please check it ";
system("pause");
return 1;
}
output.open("Expenses2.txt");
if(output.fail())
{ cout<<"Expenses2 file did not open for outputplease check it ";
system("pause");
return 1;
}
cout<<"The expensesare ";
for(i=0;i<n;i++)
{getline(input,game);
cout<<game<<endl;
output<<game<<endl;
}
output.close();
input.close();
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.