Saturday, October 29, 2016

Take input twenty marks from console and keep it in a file and find total as well as average using file in C++

Corresponding Code is given below:

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
    double a,x,total=0;

    ofstream out("marks.txt");

    cout<<"Enter 20 marks : ";

    for(x=0;x<20;x++)
    {
        cin>>a;
        out<<a<<endl;
    }

    out.close();

    ifstream in;
    in.open("marks.txt");

    while(!in.eof())
    {
        in>>a;
        total=total+a;

    }

    in.close();

    double average=total/20;

    cout<<"total is : "<<total<<endl<<"average is : "<<average<<endl;

    return 0;
}

No comments:

Post a Comment