inFile with an if/else

Mar 22, 2011 at 1:12am
I am new to C++ (currently taking it in school) and I have actually been doing well up until this point. But now I am conused and I am hoping someone can help.
We have to write a program that reads data from an input file. The students name, students year (Senior, Junior, Sophomore, Freshman), and five grades for the student.
That part I get. Then we have to take and find the students average based off of the 5 grades. I get that too.
This is where I am stuck. What the computer shows out in the report is based on the students year and average. If student is a senior with an average >= 65 they meet graduation requirements. If student is a senior with an average <= 65 they do not. Same pretty much for the underclassmen except for that they have a commendable achievement or are on academic probation.
I can get the if / else statement to work with the average, but it doesn't seem to read in the student year and I can't figure out why.
Can anyone give me a hand with this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>   

using namespace std;

const int TOTAL_STUDENT_GRADES = 5;

int main()
{
	
	string studentName; string studentYear; string Senior;
	float studentGrade1; float studentGrade2; float studentGrade3;  float studentGrade4; float studentGrade5;
	float studentAverage;


	ifstream infile;  //  Input File Stream object (variable)
	infile.open("student_data.txt");  //  Attach the stream to the file

	// Validate that the file opened successfully

	int i;
	for (i = 1; i <= 3; i++)
	{
		getline(infile, studentName);
		infile >> studentYear;
		infile >> studentGrade1 >> studentGrade2 >> studentGrade3 >> studentGrade4 >> studentGrade5;

		studentAverage = (studentGrade1 + studentGrade2 + studentGrade3 + studentGrade4 + studentGrade5)/TOTAL_STUDENT_GRADES;
	
		cout << "Student Name: " << studentName << endl;
		cout << "Student Year: " << studentYear << endl;
		cout << "Student Grades: " << studentGrade1 << " " << studentGrade2 << " " << studentGrade3 << " " << studentGrade4 << " " << studentGrade4 << endl;
		cout << "Student Average: " << studentAverage << endl;

		infile.ignore(1, '\n');  

		if (studentYear == Senior && studentAverage >= 65)
		{
			cout << "Student DOES meet graduation requirements" << endl;
		}
		else if (studentYear == Senior && studentAverage <= 65)
		{
			cout << "Student DOES NOT meet graduation requirements" << endl;
		}
		else if (studentYear != Senior && studentAverage >= 65)
		{
			cout << "Commendable Achievement!" << endl;
		}
		else
			cout << "Student is on academic probation." << endl;
	}

	infile.close();  //  Close the input stream

	return 0;
}


**I thought inFile pretty much worked as cin, so why isn't it seeing what studentYear is when it reads in and working on my if statements??

VERY VERY CONFUSED!!!
Mar 22, 2011 at 1:15am
What is your input file?
Mar 22, 2011 at 1:32am
my input file is named "student_data.txt"

The example we are using has the following data:
Theresa A. Smith
Freshman
97 95 93 88 91
Robert J. Jones III
Senior
99 96 95 100 97
Joan K. Banks
Senior
67 69 75 55 50
Mar 22, 2011 at 1:42pm
closed account (zwA4jE8b)
1
2
getline(infile, studentName);
infile >> ws;


getline does not automatically skip whitespace like the regular extractor

so what is probably happening is that studentName is beginning with a newline char.
Last edited on Mar 22, 2011 at 1:55pm
Mar 22, 2011 at 1:55pm
closed account (zwA4jE8b)
1
2
3
getline(infile, studentName);
infile >> ws >>  studentYear;
infile >> studentGrade1 >> studentGrade2 >> studentGrade3 >> studentGrade4 >> studentGrade5;
Last edited on Mar 22, 2011 at 1:56pm
Topic archived. No new replies allowed.