Problem Statement: File Handling in C/C++
On the basis of the given scenario create a file Expenses.txt, Open the file and copy into another Expenses2.txt, Also show output on screen
A college has announced the total budget of 50,000Rs.for each game. Games are done four times in a year. Take expenses as an input from user.
Calculate the average expenses for a game:

1. If the expenses greater than 80% show as” Very Expensive”.
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% than show “Not Costly”.
5. If the expenses are less than 40% than show “Best”.

Code:

/* Program that create the File Expenses.txt, open the file and copy into 
another file Expenses2.txt, and show its output on the screen.*/

//Declaring header files
#include <iostream>
#include<fstream>
#include<conio.h>

using namespace std;
int main()

{
    int cost1,cost2,cost3,cost4,total,percent,n,i;
        string game;
        ofstream output;
        ifstream input;
        output.open("Expenses.txt");  
              
        if(output.fail())                   
        { 
          cout<<"Error opening file Expenses.txt. Please check\n";
          system("pause");
          return 1;
        }
          cout<<"Enter the number of games:";
          cin>>n;
        
        for(i=0;i<n;i++)
        {  
           cout<<"Enter the Name of game: ";
           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;
           
           total=cost1 + cost2 + cost3 + cost4;
           
            
           
           percent=total/50000.*100.;
           output<<game<<" "<<cost1<<" "<<cost2<<" "<<cost3<<" "<<cost4<<" "<<percent<<" ";

           if(percent>=80)
                output<<"Very Expesive\n";
           else if(percent>=60)
                output<<"Expesive\n";
           else if(percent>=50)
                output<<"Less Expesive\n";
           else if(percent>=40)
                output<<"Not Costly\n";
           else
                output<<"Best\n";
           }
           
           output.close();
           input.open("Expenses.txt"); 
                   
           if(input.fail())          
           { 
             cout<<"Expenses file did not open for input please check it\n";
             system("pause");
             return 1;
           }
           
           output.open("Expenses2.txt");        
           if(output.fail())           
           { 
             cout<<"Expenses2 file did not open for output please check it\n";
             system("pause");
             return 1;
           }
             cout<<"The expenses are\n";                  
             for(i=0;i<n;i++)
             {getline(input,game);
             cout<<game<<endl;
             output<<game<<endl;
           }
           
           output.close();
           input.close();
           system("pause");
           return 0;
}

Please help me in this program.My problem is that we are given that total budget of a game is 50000 but when the total expenses increases what should we do .....

Recommended Answers

All 3 Replies

There are at least two other threads about this exact homework problem on DaniWeb alone with countless others all over the internet.

well i guess ur asking if total expenses increases more than 50,000 ..?

In that case u can just check the variable total..
Which is been calculated in line 45 of your program .

Just put a condition checking for the expense to be less than or equal to 50000 . if condition is satisfied then proceed , or give a message and exit (or whatever needful is to be done) .

Check this out:
http://www.daniweb.com/forums/thread240216.html

Please help me in this program.My problem is that we are given that total budget of a game is 50000 but when the total expenses increases what should we do .....

You calculate a percentage of 50,000. Then it either falls into one of five categories:

1. If the expenses greater than 80% show as” Very Expensive”.
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% than show “Not Costly”.
5. If the expenses are less than 40% than show “Best”.

Display output based on percentage. Even if you have an expense that is greater than 50,000, it will still qualify as case #1 in the above assignment criteria.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.