Allow the user to enter file name for data file; id, name and grade of each stud
ID: 3631637 • Letter: A
Question
Allow the user to enter file name for data file; id, name and grade of each student as well as the total number of students. A header should be shown in the data file as follows. Here is an example of the dada file.ID NAME GRADE
01 Mary 100.00
02 Tom 98.35
03 John 88.99
(2) If the grade is <0 or >100, ask the user to re-enter the correct grade. (3) After the data file is generated, read the file and print the content to the screen. Use one program to complete the tasks.
This is my code:
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
using namespace std;
int main()
{
double grade, ID, total;
char count;
ofstream fout;
string filename, name;
cout<<"Please enter the name for the datafile."<<endl;
cin>>filename;
fout.open(filename.c_str());
do
{
cout<<"Please enter the total number of students."<<endl;
cin>>total;
}
while (total<0);
fout<<setw(3)<<""<<setw(5)<<"ID"<<setw(16)<<"Name"<<setw(9)<<"Grade"<<endl<<endl;
for (count=1;count<=total;count++);
{
while(count<=total)
{
cout<<"Please enter the student's ID."<<endl;
cin>>ID;
cout<<"Please enter the student's name."<<endl;
cin>>name;
cout<<"Please enter the student's grade."<<endl;
cin>>grade;
while(grade<0 || grade>100);
{
cout<<"Please re-enter a correct grade between 0 and 100."<<endl;
cin>>grade;
}
fout<<setw(3)<<count<<setw(5)<<ID<<setw(16)<<name<<setw(9)<<grade<<endl;
}
}
fout.eof();
ifstream fin;
fin.open(filename.c_str());
string temp;
while (! fin.eof())
{
getline(fin,temp);
cout<<temp<<endl;
fin.close();
}
system("pause");
return 0;
}
Explanation / Answer
#include #include using namespace std; int main() { ofstream out; out.open("Studentgrade.txt"); int total; int id; int grade; string name; do { coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.