Answer to Question #253774 in C++ for Bilal Tanveer

Question #253774

Write a C++ program to find maximum marks stored in marks.txt file:

  1. Display it
  2. find average and display it.
  3. Write the average in given file.
1
Expert's answer
2021-10-20T00:06:41-0400
#include <iostream>
#include <fstream>
using namespace std;
int main()
    {
    int num, Maximum, minimum, add;
    ifstream f;
    ofstream O;
    O.open("marks.txt");
    f.open("copy.txt");
   
    if (!f)
    {
        cout << "Error cannot open the file" << endl;        
        exit(1);
    }
    else
    {
        cout << "file opened successfully" << endl;
        O << "Opening marks file" << endl;
    }                                                                     
    while (!f.eof())  
    {  
        for(int x=1; x<=8; x++){       
            cout <<"\nLine "<<x<<": ";
            int add = 0;
            Maximum = 0;
			minimum=0;
            int n=1;
            do
            {
                f >> num;
                if(Maximum == 0){
                   Maximum= num;
                }
                if(num > Maximum){
                   Maximum = num;
            }
                O << num << " ";
                cout << num << " ";
                n++;
                add += num;
            } while (n <= 7);
            O << "Maximum: "<<Maximum<<", Sum: "<<add<<", Average: "<<add/7<<endl;


        }
     }
      string line;//writing to another file
    if(O && f){
 
        while(getline(f,line)){
            O << line << "\n";
        }}
    O.close();
    f.close();
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS