Topic: Question for I/O Stream Buff's
Author: abowen@ix.netcom.com (ARB )
Date: 1995/06/26 Raw View
I have been working on a program that accepts the following data and
writes it to a sequential output file:
Student Name Course Grade
Jim West Eng. 101 A
Jane East Mat. 301 B
John South Bio. 401 C
etc...
the program then reads the file and creates a student grade report on
to the screen.
Does anyone know how I can format the display to only have the header
once? Now I get:
Student Name Course Grade
Jim West Eng. 101 A
Student Name Course Grade
Jane East Mat. 301 B
etc...
Also, is there an easy way to line the columns up? Now I'm using the
setw(); function and it sort of seems to be hit and miss.
One last question, How do you get the cout and cin operators to accept
white spaces like in Eng_101? or both first and last names within one
string?
Here is a copy of my code so far;
#include <fstream.h>
#include <iomanip.h>
class Student
{
protected:
char fname[15];
char lname[15];
char course[40];
char grade;
public:
void getStudent(void)
{
cout << " First Name: ";
cin >> fname;
cout << " Last Name: ";
cin >> lname;
cout << "Enter Course Taken: ";
cin >> course;
cout << "Enter Grade Earned: ";
cin >> grade;
}
void showStudents()
{
cout << endl <<" Student Name Course Grade " <<
endl;
cout << setw(8)<< lname << setw(10)<< fname << setw(10)<<
course << setw(10) << grade << endl;
}
};
void main(void)
{
Student s1;
fstream file;
char yn;
ofstream outfile("student.dat", ios::binary);
file.open("student.dat", ios::app | ios::out | ios::in |
ios::binary );
do
{
cout << "\nEnter the Students Information:" <<
endl;
s1.getStudent();
file.write( (char*)&s1, sizeof(s1) );
cout << "\nDo You Wish to Enter Another Student
(Y/N)? ";
cin >> yn;
}
while (yn=='y');
file.seekg(0);
file.read( (char*)&s1, sizeof(s1) );
while( !file.eof() )
{
s1.showStudents();
file.read( (char*)&s1, sizeof(s1) );
}
}