Rational for answer: OP is headed in many wrong directions all at once and I think a few of them can be headed off with one good example.
Problem 1: EOF loop is a myth. Read more here: Why is iostream::eof inside a loop condition considered wrong?
Problem 2: using char
to represent numbers. Technically char
is an integer type, but it is most commonly associated with a single character and its input/output routines are overloaded accordingly. Eg. if given input "1234" std::cin >> a_char;
will result in char a_char
containing the character '1', but std::cin >> an_int;
will result in int an_int
containing the the number 1234. In addition using a character to represent a number can often lead to confusion for thew reader who may misinterpret what your code is supposed to do. You can do it and sometimes it is the appropriate choice, but this time it doesn't make much sense.
Problem 3 Scattershot and unchecked IO. It's generally best to read in everything you want and then, if the input is good, make a decision on all of it all at once. If a student's record is not complete without 27 integers, read all 27 integers and check that each and every one read correctly. If you can't read them all, that's an error. If some are not integers, that's an error. If you have an error, the student is not a valid input and should either be discarded or investigated more closely.
Problem 4: Not using a data structure to contain associated data. A student is represented by a bunch of information. It is convenient to package that information with the methods by which the information can be accessed and manipulated. Read up on encapsulation.
Minor nag: Why is "using namespace std" considered bad practice?
So here we go:
#include <iostream>
#include <fstream>
#include <sstream>
// using namespace std; avoid using this. Can be very dangerous to the unwary.
// container class to aggregate student stats and provide manipulators to read
// and display a student
class Student
{
static int count; // only the students use the counter. No need for it to be global.
int student_number;
// int telegraphs your intent much better than char
// These variables are numbers, not characters and should be treated like them
int lab;
int assgniment_score;
int quiz_score;
int test_score;
float final_total_average;
public:
// simple input function. This is a bit too simple for OP, but should be
// enough for OP to get the idea.
friend std::istream & operator>>(std::istream & in, Student & student)
{
std::string line;
student.student_number = count ++;
if (std::getline(in, line))
{
std::stringstream stream(line);
if (stream >> student.lab
>> student.assgniment_score
>> student.quiz_score
>> student.test_score)
{ // if we read all the variables we needed
// divided by 4.0 because division of int by int will
// give an int, not a float.
student.final_total_average = (student.lab +
student.assgniment_score +
student.quiz_score +
student.test_score) / 4.0;
}
else
{ // failed to read. Mark stream bad.
in.setstate(std::istream::failbit);
}
}
return in;
}
// simple output function OP shouldn't have to change much here.
friend std::ostream & operator<<(std::ostream & out, Student & student)
{
//endl is line feed and IO flush very expensive, so use only
// when you absolutely need to flush the stream. This turns out to be
// almost enver.
out << "Student " << student.student_number << '\n'
<< "Student Lab grade is : " << student.lab << '\n'
<< "Student Assignment grade is : " << student.assgniment_score << '\n'
<< "Student Quiz grade is : " << student.quiz_score << '\n'
<< "Student Exam grade is : " << student.test_score << '\n'
<< "Student Final grade is : " << student.final_total_average << '\n';
return out;
}
};
// allocate storage for student counter
int Student::count = 1;
int main()
{
// create and open file. As with the counter, no need for it to be global
// because it can easily be passed by reference into functions that need it.
std::ifstream myfile("infile.txt");
Student student; // allocate a dummy student to read into and print
// because we have smart rules for reading in a student, this is easy.
while (myfile >> student) // read students until error or end of file
{
std::cout << student; // write the current student
}
}