Restarting C++ class after lengthy break, seems I've forgotten a lot. I can't seem to get a cout to indicate that a file record has been read.

I created a text file of just one record to keep it simple: an SS number, first and last names, and five exam scores.

Any help will be appreciated; once I have read the data from the file, I don't anticipate any problems in formatting a report from it.

Thanks,

Rich

/* Rich Mansfield  0457321  Assignment 01
 This program produces a student grade report for Wexler University. 
 It displays 5 exam scores for each student, the student's average 
 grade, and the class average (average of the averages).
 
 The data file used is "WUexamp1.txt"
 The report file produced is "Wexler U Student Grade Report1.txt"
*/

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;

// global variables

//named constant for number of exams
const int NUM_EXAMS = 5;

// input/output objects
ifstream input ("WUexamp1.txt");
ofstream output("Wexley U Student Grade Report1.txt");

// input data fields
string studentNo, firstName, lastName;
int examScore[NUM_EXAMS];

// accumulator
int sumOfTheAverages, sumOfTheScores;

//counter
int numberOfStudents;

//calculated field
int studentAverage, classAverage;

//function prototypes
void initialize();
void processRecord();
void finishUp();
void readRecord();
void calculateStudentAverage();
void calculateClassAverage();
void printDetailLine();
//--------------------------------------------------------------

int main()
{
	initialize();
	processRecord();
/*
	while ( !input.eof() )
	{
		processRecord();
	}	

	finishUp();
*/
	return 0;
}

//--------------------------------------------------------------

void initialize()
{
	input.open ("WUexamp1.txt");
}

//--------------------------------------------------------------

void processRecord()
{
	readRecord();
	calculateStudentAverage();
	cout	<< "Student average is: "	<< studentAverage	<< endl;
}

//--------------------------------------------------------------

void finishUp()
{
}

//--------------------------------------------------------------

void readRecord()
{
	input	>> studentNo	>> firstName	>> lastName;
	int i = 0;
	while (i < NUM_EXAMS)
		input	>> examScore[i];
		sumOfTheScores += examScore[i];
		i++;
	cout	<< "\n\nI have read the record\n\n"	<< endl; //doesn't get this far.
}

//--------------------------------------------------------------

void calculateStudentAverage()
{
	studentAverage = floor( (double) sumOfTheScores / NUM_EXAMS + .50 );
}

//--------------------------------------------------------------

void calculateClassAverage()
{
	classAverage = floor( (double) sumOfTheAverages / numberOfStudents + .50 );
}

//--------------------------------------------------------------
void printDetailLine()
{
}

you have no braces around your while function. should be

while (i < NUM_EXAMS)
{  // <-  dont forget these for multy line statements.
        input >> examScore[i];
        sumOfTheScores += examScore[i];
        i++
}  // <-  same here.
commented: Good catch! +1

Thanks - good catch. I have indeed forgotten almost more than I had learned! Unfortunately, even though the program proceeds to the point of showing a student average now, a problem persists; the average is coming out zero, when it should be 69. What might be causing that?

Rich

i would set sumOfTheScores to 0 when you declare it and then see what happens. ie. int sumOfTheScores = 0;

// accumulator
int sumOfTheAverages = 0, sumOfTheScores = 0;

I tried it up here in the declarations part, but got no different result. Is there a way I can use Apple's XCode Debugger to home in on what's going on?

Rich

i have no idea. what i do is go put a breakpoint around the code i dont think is working and then step through the code line by line and see what happens to the variables

void initialize()
{
	input.open ("WUexamp1.txt");
}

Files should not be opened this way. You should pass the file name and the fstream object as reference. Like this:

char fileName[] = "input.txt";

bool initialize(char[] fName, fstream& iFile) {
    iFile(fName, ios::input | ios::beg);

    if (iFile.fail())
        return false;  // Indicates file open error
    else return true;
}

Hi, Nathan - Solved it! These two lines turn out to be redundant; the latter one tries to open an already open file, which results in an empty file. Thanks for the help!
ifstream input ("WUexamp1.txt");
input.open ("WUexamp1.txt");

Rich

Hi, Hag++ - Solved it! These two lines turn out to be redundant; the latter one tries to open an already open file, which results in an empty file. Thanks for the help!
ifstream input ("WUexamp1.txt");
input.open ("WUexamp1.txt");

Rich

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.