C++
Write a program that writes the grades for 3 students. Declare a variable of type ofstream which is used to output a stream into a file: ofstream output; // output is the name of the variable Prompt the user to input values for the grades of 3 students. Use the output operator (<<) to write the grades into grades.txt: output << grade1 << " " << grade2 << " " << grade3 << endl; You may use IO manipulation commands (e.g. setw) in the above statement.
#include<iostream>
#include<fstream>
using namespace std;
int main(){
int grade1,grade2,grade3;
ofstream outfile;
outfile.open("grades.txt");//open out put file to write data
cout<<"Enter grades:";
cin>>grade1>>grade2>>grade3;//read grades
outfile<<grade1<<" "<<grade2<<"
"<<grade3<<endl;//write to file
outfile.close();//close file
}
Screenshots:
The screenshots are attached below for reference.
Please follow them.
Get Answers For Free
Most questions answered within 1 hours.