Write a C++ program to find maximum marks stored in marks.txt file:
#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;
}
Comments
Leave a comment