(1) Generate a data file to save students' grade. Allow the user to enter id, name and grade of each student as well as the total number of students. There should be a header in the first row. Here is an example of the data 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.
The tasks describe above should be completed in one program.
I have created a program:
#include<fstream>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
int main()
{
int ID, amountofstudents;
double Grade;
string Name;
ofstream OOO;
OOO.open ("code.dat");
cout<<"Please enter amount of students"<<endl;
cin>>amountofstudents;
//header
for( int i=1; i<=ID ; ++i )
cout << left << setw(5) << i ;
cout<<endl<<"--";
for( int i=1; i<=amountofstudents ; ++i )
{
cout<<"Please enter ID of student."<<endl;
cin>>ID;
cout<<"Please enter Name of student"<<endl;
cin>>Name;
cout<<"Please enter grade of student"<<endl;
cin>>Grade;
while (Grade<0 || Grade>100)
{
cout<<"Unable to calculate, please enter grade again"<<endl;
cin>>Grade;
}
OOO<<setw(10)<<ID<<setw(10)<<Name<<setw(10)<<Grade<<endl;
}
system("pause");
return 0;
}
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
//int main(void)
{
int ID, amountofstudents;
double Grade;
string Name;
//ifstream III.;
//string filename;
//III.open("datafile.dat");
//III>>S1>>S2
//int main()
ifstream III;
III.open("datafile.dat");
if(!III)
{
cerr << "Error: file could not be opened" << endl;
exit(1);
}
III >> Grade;
while ( !III.eof() )
{
cout<< "The result is" << Grade << endl;
III>> amountofstudents>>ID;
}
III.close();
cout << "End-of-file reached.." << endl;
return 0;
}
This post has been edited by JackOfAllTrades: 03 March 2010 - 10:50 AM
Reason for edit:: Added code tags. PLEASE!!! [code]...PUT YOUR CODE IN HERE!!!...[/code]

New Topic/Question
Reply




MultiQuote





|