IO stream

O

oLgAa25

I have the following question,
I am trying to read from a file and output to another file.
But I am not getting exactly the expected output.

Can anyone help please!


using namespace std;



void calculateAverage(double test1, double test2, double test3, double
test4, double test5, double& studentAverage);
int calculateGrade(double grade);

int main()
{

string studentName;
int numberOfStudents = 0;
double classAverage = 0;
double studentAverage = 0;
double totalAverage = 0; //To add the average of all student
averages
char grade;
double test1, test2, test3, test4, test5;

ifstream inFile; // input stream variable for the student file
ofstream outFile; // output stream variable

inFile.open("c:\\studentFile.txt");
outFile.open("c:\\Student_out.txt");

cout << setfill(' ') <<setiosflags(ios:: left) << setw(11) <<
"Student" << setw(10) << "Test1"
<< setw(8) << "Test2" << setw(8) << "Test3" << setw(8) << "Test4"
<< setw(8) << "Test5"
<< setw(8) << "Average" << setw(8) << "Grade" <<endl;

outFile << setfill(' ') <<setiosflags(ios:: left) << setw(11) <<
"Student" << setw(10) << "Test1"
<< setw(8) << "Test2" << setw(8) << "Test3" << setw(8) << "Test4"
<< setw(8) << "Test5"
<< setw(8) << "Average" << setw(8) << "Grade" << endl;


if (!inFile)
{
cout << "Unable to open the file." <<endl;
return 1;
}

while(inFile)
{
outFile.setf(ios::fixed, ios::floatfield);
outFile.setf(ios::showpoint);
outFile << setprecision(2);

inFile >> studentName >> test1 >> test2 >> test3 >> test4 >> test5
;

calculateAverage(test1, test2, test3, test4, test5,
studentAverage);

grade = calculateGrade(studentAverage);

cout << setfill(' ') << setiosflags(ios::left) << setw(11) <<
studentName << setw (10) << test1
<< setw(8) << test2 << setw(8) << test3 << setw(8) << test4
<< setw(8) << test5 << setw(9) << studentAverage << setw(8) <<
grade<<endl;
outFile << setfill(' ') << setiosflags(ios::left) << setw(11) <<
studentName << setw (10) << test1
<< setw(8) << test2 << setw(8) << test3 << setw(8) << test4
<< setw(8) << test5 << setw(9) << studentAverage << setw(8) <<
grade<<endl;

totalAverage = totalAverage + studentAverage;

numberOfStudents++;
classAverage = totalAverage / numberOfStudents;
}


outFile << endl << setprecision(2)<< "Class average is:" <<
classAverage << endl;

inFile.close();
outFile.close();

system("PAUSE");
return 0;
}



//function to calculate the average
void calculateAverage(double test1, double test2, double test3, double
test4, double test5, double& studentAverage)
{

studentAverage = static_cast<double>(test1 + test2 + test3 + test4
+ test5) / 5.0;

}




int calculateGrade(double studentAverage)
{
char grade;

if (studentAverage <= 100 && studentAverage >= 90)
grade = 'A';
else if (studentAverage < 90 && studentAverage >= 80)
grade = 'B';
else if (studentAverage < 80 && studentAverage >= 70)
grade = 'C';
else if (studentAverage < 70 && studentAverage >= 60)
grade = 'D';
else if (studentAverage < 60 && studentAverage >= 0)
grade = 'F';
else
cout << "Invalid grade " << endl;

return grade;

}
 
M

mlimber

oLgAa25 said:
I have the following question,
I am trying to read from a file and output to another file.
But I am not getting exactly the expected output.

What is wrong with the output? Are there random characters in it? Are
the calculated values incorrect? Does the file end prematurely? Be more
specific in your question.
Can anyone help please!

I'll make some preliminary comments below.
using namespace std;



void calculateAverage(double test1, double test2, double test3, double
test4, double test5, double& studentAverage);

Why not just return the average rather than passing it in by reference?
You're not using the existing value of studentAverage, after all.
int calculateGrade(double grade);

Why not return a char instead of an int?
int main()
{

string studentName;
int numberOfStudents = 0;
double classAverage = 0;
double studentAverage = 0;
double totalAverage = 0; //To add the average of all student
averages
char grade;
double test1, test2, test3, test4, test5;

Don't declare variables until you can initialize and use them. C
requires that you declare all variables at the start of scope, but C++
does not. Prefer C++ idioms when programming in C++.
ifstream inFile; // input stream variable for the student file
ofstream outFile; // output stream variable

inFile.open("c:\\studentFile.txt");
outFile.open("c:\\Student_out.txt");

Prefer to open and close via the constructor and destructor:

ifstream inFile("c:\\studentFile.txt");
ofstream outFile("c:\\Student_out.txt");

cout << setfill(' ') <<setiosflags(ios:: left) << setw(11) <<
"Student" << setw(10) << "Test1"
<< setw(8) << "Test2" << setw(8) << "Test3" << setw(8) << "Test4"
<< setw(8) << "Test5"
<< setw(8) << "Average" << setw(8) << "Grade" <<endl;

outFile << setfill(' ') <<setiosflags(ios:: left) << setw(11) <<
"Student" << setw(10) << "Test1"
<< setw(8) << "Test2" << setw(8) << "Test3" << setw(8) << "Test4"
<< setw(8) << "Test5"
<< setw(8) << "Average" << setw(8) << "Grade" << endl;


if (!inFile)
{
cout << "Unable to open the file." <<endl;
return 1;
}

Good, but you didn't check outFile to see if it opened successfully.
while(inFile)
{
outFile.setf(ios::fixed, ios::floatfield);
outFile.setf(ios::showpoint);
outFile << setprecision(2);

inFile >> studentName >> test1 >> test2 >> test3 >> test4 >> test5
;

calculateAverage(test1, test2, test3, test4, test5,
studentAverage);

grade = calculateGrade(studentAverage);

cout << setfill(' ') << setiosflags(ios::left) << setw(11) <<
studentName << setw (10) << test1
<< setw(8) << test2 << setw(8) << test3 << setw(8) << test4
<< setw(8) << test5 << setw(9) << studentAverage << setw(8) <<
grade<<endl;
outFile << setfill(' ') << setiosflags(ios::left) << setw(11) <<
studentName << setw (10) << test1
<< setw(8) << test2 << setw(8) << test3 << setw(8) << test4
<< setw(8) << test5 << setw(9) << studentAverage << setw(8) <<
grade<<endl;

totalAverage = totalAverage + studentAverage;

numberOfStudents++;

Make a habit of prefix notation (see
http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.15).
classAverage = totalAverage / numberOfStudents;

Don't you mean for this line to be outside the loop?
}


outFile << endl << setprecision(2)<< "Class average is:" <<
classAverage << endl;

inFile.close();
outFile.close();

Unnecessary. The destructor does this for you. Why add extra code when
you don't need it?
system("PAUSE");
return 0;
}



//function to calculate the average
void calculateAverage(double test1, double test2, double test3, double
test4, double test5, double& studentAverage)
{

studentAverage = static_cast<double>(test1 + test2 + test3 + test4
+ test5) / 5.0;

}




int calculateGrade(double studentAverage)
{
char grade;

if (studentAverage <= 100 && studentAverage >= 90)
grade = 'A';
else if (studentAverage < 90 && studentAverage >= 80)
grade = 'B';
else if (studentAverage < 80 && studentAverage >= 70)
grade = 'C';
else if (studentAverage < 70 && studentAverage >= 60)
grade = 'D';
else if (studentAverage < 60 && studentAverage >= 0)
grade = 'F';
else
cout << "Invalid grade " << endl;

If the else condition is executed, grade is left uninitialized but
returned to the caller.
return grade;

}

Cheers! --M
 
O

oLgAa25

What is wrong with the output? Are there random characters in it? Are
the calculated values incorrect? Does the file end prematurely? Be more

specific in your question.

Here is what is wrong
I will post it here

Student Test1 Test2 Test3 Test4 Test5 Average Grade
Balto85.00 83.00 77.00 91.00 76.00 82.40 B
Mickey80.00 90.00 95.00 93.00 48.00 81.20 B
Minnie79.00 81.00 11.00 90.00 73.00 66.80 D
Doc92.00 83.00 30.00 69.00 87.00 72.20 C
Goofy23.00 45.00 96.00 38.00 59.00 52.20 F
Duckey60.00 85.00 45.00 39.00 67.00 59.20 F
Grumby27.00 31.00 52.00 74.00 83.00 53.40 F
Sunny93.00 94.00 89.00 77.00 97.00 90.00 A
Piggy79.00 85.00 28.00 93.00 82.00 73.40 C
Pluto85.00 72.00 49.00 75.00 63.00 68.80 D
Pluto85.00 72.00 49.00 75.00 63.00 68.80 D

Class average is:69.85

and here is my original file

Balto 85 83 77 91 76
Mickey 80 90 95 93 48
Minnie 79 81 11 90 73
Doc 92 83 30 69 87
Goofy 23 45 96 38 59
Duckey 60 85 45 39 67
Grumby 27 31 52 74 83
Sunny 93 94 89 77 97
Piggy 79 85 28 93 82
Pluto 85 72 49 75 63

Why is Pluto printed out twice?

and for the void function, That s the requirement of the question.

//*********************************************************************************
// Write a program that reads a student's name together with his or her
test *
// scores. The program should then compute the average test score for
each student*
// and assign the appropriate grade. The grade scale is as follows:
*
// 90-100, A; 80-89, B ; 70-79, C; 60-69, D; 0-59, F.
*
// Your program must use the following functions:
*
//
*
//a. A void function, calculateAverage, to determine the average of the
five *
// test score for each student. Use a loop to read and sum the five
test score *
// This function doesn't output the average test score. That task
must be done *
// in the function main)
*
//
*
// b. A value return function, calculateGrade, to determine and return
each *
// student's grade. This function doesn't output the grade. That
task must be *
// done in the function main)
*
//*******************************************************************************
*

#include <iostream.h>
#include <stdlib.h>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;



void calculateAverage(double test1, double test2, double test3, double
test4, double test5, double& studentAverage);
char calculateGrade(double grade);

int main()
{

string studentName;
int numberOfStudents = 0;
double classAverage = 0;
double studentAverage = 0;
double totalAverage = 0; //To add the average of all student
averages
char grade;
double test1, test2, test3, test4, test5;

ifstream inFile; // input stream variable for the student file
ofstream outFile; // output stream variable

inFile.open("c:\\studentFile.txt");
outFile.open("c:\\Student_out.txt");

cout << setfill(' ') <<setiosflags(ios:: left) << setw(11) <<
"Student" << setw(10) << "Test1"
<< setw(8) << "Test2" << setw(8) << "Test3" << setw(8) << "Test4"
<< setw(8) << "Test5"
<< setw(8) << "Average" << setw(8) << "Grade" <<endl;

outFile << setfill(' ') <<setiosflags(ios:: left) << setw(11) <<
"Student" << setw(10) << "Test1"
<< setw(8) << "Test2" << setw(8) << "Test3" << setw(8) << "Test4"
<< setw(8) << "Test5"
<< setw(8) << "Average" << setw(8) << "Grade" << endl;


if (!inFile)
{
cout << "Unable to open the file." <<endl;
return 1;
}

while(inFile)
{
outFile.setf(ios::fixed, ios::floatfield);
outFile.setf(ios::showpoint);
outFile << setprecision(2);

inFile >> studentName >> test1 >> test2 >> test3 >> test4 >> test5
;

calculateAverage(test1, test2, test3, test4, test5,
studentAverage);

grade = calculateGrade(studentAverage);

cout << setfill(' ') << setiosflags(ios::left) << setw(11) <<
studentName << setw (10) << test1
<< setw(8) << test2 << setw(8) << test3 << setw(8) << test4
<< setw(8) << test5 << setw(9) << studentAverage << setw(8) <<
grade<<endl;
outFile << setfill(' ') << setiosflags(ios::left) << setw(11) <<
studentName << setw (10) << test1
<< setw(8) << test2 << setw(8) << test3 << setw(8) << test4
<< setw(8) << test5 << setw(9) << studentAverage << setw(8) <<
grade<<endl;

totalAverage = totalAverage + studentAverage;

numberOfStudents++;
//classAverage = totalAverage / numberOfStudents;
}
classAverage = totalAverage / numberOfStudents;

outFile << endl << setprecision(2)<< "Class average is:" <<
classAverage << endl;

inFile.close();
outFile.close();

system("PAUSE");
return 0;
}



//function to calculate the average
void calculateAverage(double test1, double test2, double test3, double
test4, double test5, double& studentAverage)
{

studentAverage = static_cast<double>(test1 + test2 + test3 + test4
+ test5) / 5.0;

}




char calculateGrade(double studentAverage)
{
char grade;

if (studentAverage <= 100 && studentAverage >= 90)
grade = 'A';
else if (studentAverage < 90 && studentAverage >= 80)
grade = 'B';
else if (studentAverage < 80 && studentAverage >= 70)
grade = 'C';
else if (studentAverage < 70 && studentAverage >= 60)
grade = 'D';
else if (studentAverage < 60 && studentAverage >= 0)
grade = 'F';
else
cout << "Invalid grade " << endl;

return grade;

}
 
O

osmium

oLgAa25 said:
I have the following question,
I am trying to read from a file and output to another file.
But I am not getting exactly the expected output.

Can anyone help please!

You don't say what the problems are, but I will note a few things anyway,
they may contribute a bit to an eventaul solution. Your problem is
remarkably similiar to a problem posted to comp.lang.learn.c-c++ with the
thread title"class average program". You might look at what has been said
there, too. I think you should minimize the "pretty output" code until you
get the program working. At this point it just clutters up the mind and
tends to obscure the underlying problems. I am speaking of setw() and
fiddling with stream flags and such.
using namespace std;



void calculateAverage(double test1, double test2, double test3, double
test4, double test5, double& studentAverage);
int calculateGrade(double grade);

int main()
{

string studentName;
int numberOfStudents = 0;
double classAverage = 0;
double studentAverage = 0;
double totalAverage = 0; //To add the average of all student
averages

The word grand_mean would be a good name for that variable.
char grade;
double test1, test2, test3, test4, test5;

I doubt if you need five names. Get a grade, process it, and wash your
hands of it. Simply add the number, something like this:

int student_sum = 0;

// and later

int student_sum += test;

That's all you will need to compute an average later on.
ifstream inFile; // input stream variable for the student file
ofstream outFile; // output stream variable

inFile.open("c:\\studentFile.txt");
outFile.open("c:\\Student_out.txt");

cout << setfill(' ') <<setiosflags(ios:: left) << setw(11) <<
"Student" << setw(10) << "Test1"

"Test1" will print Test1 as a string of characters. I suspect you wanted,
simply Test1
<< setw(8) << "Test2" << setw(8) << "Test3" << setw(8) << "Test4"
<< setw(8) << "Test5"
<< setw(8) << "Average" << setw(8) << "Grade" <<endl;

outFile << setfill(' ') <<setiosflags(ios:: left) << setw(11) <<
"Student" << setw(10) << "Test1"
<< setw(8) << "Test2" << setw(8) << "Test3" << setw(8) << "Test4"
<< setw(8) << "Test5"
<< setw(8) << "Average" << setw(8) << "Grade" << endl;


if (!inFile)
{
cout << "Unable to open the file." <<endl;
return 1;
}

while(inFile)
{
outFile.setf(ios::fixed, ios::floatfield);
outFile.setf(ios::showpoint);
outFile << setprecision(2);

inFile >> studentName >> test1 >> test2 >> test3 >> test4 >> test5
;

calculateAverage(test1, test2, test3, test4, test5,
studentAverage);

grade = calculateGrade(studentAverage);

cout << setfill(' ') << setiosflags(ios::left) << setw(11) <<
studentName << setw (10) << test1
<< setw(8) << test2 << setw(8) << test3 << setw(8) << test4
<< setw(8) << test5 << setw(9) << studentAverage << setw(8) <<
grade<<endl;
outFile << setfill(' ') << setiosflags(ios::left) << setw(11) <<
studentName << setw (10) << test1
<< setw(8) << test2 << setw(8) << test3 << setw(8) << test4
<< setw(8) << test5 << setw(9) << studentAverage << setw(8) <<
grade<<endl;

totalAverage = totalAverage + studentAverage;

numberOfStudents++;
classAverage = totalAverage / numberOfStudents;
}


outFile << endl << setprecision(2)<< "Class average is:" <<
classAverage << endl;

inFile.close();
outFile.close();

system("PAUSE");
return 0;
}



//function to calculate the average
void calculateAverage(double test1, double test2, double test3, double
test4, double test5, double& studentAverage)

Normal practice would be to have such a function return a value, like this:

double calculateAverage( ...)

What you have is not necessarily wrong, just a "heads up"
..
{

studentAverage = static_cast<double>(test1 + test2 + test3 + test4
+ test5) / 5.0;

}




int calculateGrade(double studentAverage)

Is there a good reason to not return a char?
Honesty is a good policy - but not the best.
 
M

mlimber

oLgAa25 said:
What is wrong with the output? Are there random characters in it? Are
the calculated values incorrect? Does the file end prematurely? Be more

specific in your question.

Here is what is wrong
I will post it here

Student Test1 Test2 Test3 Test4 Test5 Average Grade
Balto85.00 83.00 77.00 91.00 76.00 82.40 B
Mickey80.00 90.00 95.00 93.00 48.00 81.20 B
Minnie79.00 81.00 11.00 90.00 73.00 66.80 D
Doc92.00 83.00 30.00 69.00 87.00 72.20 C
Goofy23.00 45.00 96.00 38.00 59.00 52.20 F
Duckey60.00 85.00 45.00 39.00 67.00 59.20 F
Grumby27.00 31.00 52.00 74.00 83.00 53.40 F
Sunny93.00 94.00 89.00 77.00 97.00 90.00 A
Piggy79.00 85.00 28.00 93.00 82.00 73.40 C
Pluto85.00 72.00 49.00 75.00 63.00 68.80 D
Pluto85.00 72.00 49.00 75.00 63.00 68.80 D

Class average is:69.85

and here is my original file

Balto 85 83 77 91 76
Mickey 80 90 95 93 48
Minnie 79 81 11 90 73
Doc 92 83 30 69 87
Goofy 23 45 96 38 59
Duckey 60 85 45 39 67
Grumby 27 31 52 74 83
Sunny 93 94 89 77 97
Piggy 79 85 28 93 82
Pluto 85 72 49 75 63

Why is Pluto printed out twice? [snip]
while(inFile)
{
outFile.setf(ios::fixed, ios::floatfield);
outFile.setf(ios::showpoint);
outFile << setprecision(2);

inFile >> studentName >> test1 >> test2 >> test3 >> test4 >> test5
;
[snip]

Because your input file wasn't yet at the end of file (e.g., because it
had a space or a blank line or something after the last line), your
while condition was true, but the actual input operation failed since
there was no more data to be read. Add something like this after the
input line:

if( !inFile ) break;

Cheers! --M
 
M

mlimber

oLgAa25 wrote:
[snip]
void calculateAverage(double test1, double test2, double test3, double
test4, double test5, double& studentAverage)
{

studentAverage = static_cast<double>(test1 + test2 + test3 + test4
+ test5) / 5.0;

}
[snip]

One more thing: there's no need for this static_cast. It just adds
clutter. Delete it.

Cheers! --M
 
O

oLgAa25

Hahaha,
Not really, I mean
what difference does it make if I use, char, or int, It is giving me
the same output
I did change it any ways.

well now to explain my question, If you look at my output textFile, you
will see that the last name Pluto, prints twice?
and again I am still a beginner " I would love to be in your level now"
:) But I doubt that will take FOREVER

and Thank you for taking the time to read my code
Olga
 
R

Rolf Magnus

mlimber said:
oLgAa25 said:
What is wrong with the output? Are there random characters in it? Are
the calculated values incorrect? Does the file end prematurely? Be more

specific in your question.

Here is what is wrong
I will post it here

Student Test1 Test2 Test3 Test4 Test5 Average Grade
Balto85.00 83.00 77.00 91.00 76.00 82.40 B
Mickey80.00 90.00 95.00 93.00 48.00 81.20 B
Minnie79.00 81.00 11.00 90.00 73.00 66.80 D
Doc92.00 83.00 30.00 69.00 87.00 72.20 C
Goofy23.00 45.00 96.00 38.00 59.00 52.20 F
Duckey60.00 85.00 45.00 39.00 67.00 59.20 F
Grumby27.00 31.00 52.00 74.00 83.00 53.40 F
Sunny93.00 94.00 89.00 77.00 97.00 90.00 A
Piggy79.00 85.00 28.00 93.00 82.00 73.40 C
Pluto85.00 72.00 49.00 75.00 63.00 68.80 D
Pluto85.00 72.00 49.00 75.00 63.00 68.80 D

Class average is:69.85

and here is my original file

Balto 85 83 77 91 76
Mickey 80 90 95 93 48
Minnie 79 81 11 90 73
Doc 92 83 30 69 87
Goofy 23 45 96 38 59
Duckey 60 85 45 39 67
Grumby 27 31 52 74 83
Sunny 93 94 89 77 97
Piggy 79 85 28 93 82
Pluto 85 72 49 75 63

Why is Pluto printed out twice? [snip]
while(inFile)
{
outFile.setf(ios::fixed, ios::floatfield);
outFile.setf(ios::showpoint);
outFile << setprecision(2);

inFile >> studentName >> test1 >> test2 >> test3 >> test4 >> test5
;
[snip]

Because your input file wasn't yet at the end of file (e.g., because it
had a space or a blank line or something after the last line), your
while condition was true, but the actual input operation failed since
there was no more data to be read. Add something like this after the
input line:

if( !inFile ) break;

Or turn the loop into:

while(inFile >> studentName >> test1 >> test2 >> test3 >> test4 >> test5)
{
outFile.setf(ios::fixed, ios::floatfield);
outFile.setf(ios::showpoint);
outFile << setprecision(2);
//...
}
 
D

Daniel T.

"oLgAa25 said:
What is wrong with the output? Are there random characters in it? Are
the calculated values incorrect? Does the file end prematurely? Be more

specific in your question.

Here is what is wrong
I will post it here

Student Test1 Test2 Test3 Test4 Test5 Average Grade
Balto85.00 83.00 77.00 91.00 76.00 82.40 B
Mickey80.00 90.00 95.00 93.00 48.00 81.20 B
Minnie79.00 81.00 11.00 90.00 73.00 66.80 D
Doc92.00 83.00 30.00 69.00 87.00 72.20 C
Goofy23.00 45.00 96.00 38.00 59.00 52.20 F
Duckey60.00 85.00 45.00 39.00 67.00 59.20 F
Grumby27.00 31.00 52.00 74.00 83.00 53.40 F
Sunny93.00 94.00 89.00 77.00 97.00 90.00 A
Piggy79.00 85.00 28.00 93.00 82.00 73.40 C
Pluto85.00 72.00 49.00 75.00 63.00 68.80 D
Pluto85.00 72.00 49.00 75.00 63.00 68.80 D

Class average is:69.85

and here is my original file

Balto 85 83 77 91 76
Mickey 80 90 95 93 48
Minnie 79 81 11 90 73
Doc 92 83 30 69 87
Goofy 23 45 96 38 59
Duckey 60 85 45 39 67
Grumby 27 31 52 74 83
Sunny 93 94 89 77 97
Piggy 79 85 28 93 82
Pluto 85 72 49 75 63

Why is Pluto printed out twice?

Because when you pull Pluto's last test score the file is still good, so
your loop goes back around and tries to pull out the next student, but
there isn't one so the line:

inFile >> studentName >> test1 >> test2 >> test3 >> test4 >> test5;

makes the inFile bad and leaves Pluto's data in the variables.

Rather than starting your loop with:

while ( inFile )

start it with :

while ( inFile >> studentName >> test1 >> test2 >>
test3 >> test4 >> test5 )

It's funny, we were helping someone else with the exact same assignment
yesterday. Are you the same person? I don't think so, the code is too
different.

Note: Your assignment doesn't meet the spec requirements,
calculateAverage must use a loop. I suspect you will be graded down
heavily if you don't fix it.

When is this assignment due? Maybe we'll have another student asking for
help soon. ;-)
 
O

oLgAa25

Hey, That was nice of you :)
you are fun guys around here. and I love that.
well my assignment was due on Sunday, but due to other reasons I got
extension.
No, I am not the same person.
I just became so desperate " Like desperate housewives" Which I am one
of them!! and was seeking help on the net
But since you are nice and helping me, I am going to be greedy and send
you the second code, the one with loop, but no output:)
any help is appreciated, *But no laughter*;-)


#include <iostream.h>
#include <stdlib.h>
#include <iomanip.h>
#include <string>
#include <fstream>

using namespace std;

void calculateAverage(ifstream& inFile, double& studentAverage);
char calculateGrade(char Average);
int main()
{
ifstream inFile;// input stream variable
ofstream outFile;
string studentName;
int numberOfStudents = 0;
double studentAvg = 0;
double totalAverage = 0;
double classAverage;
char grade;
inFile.open("c:\\studentFile.txt");
outFile.open("c:\\student_out.txt");

if (!inFile)
{
cout << "Unable to open the file." <<endl;
return 1;
}

while(inFile)
{


outFile.setf(ios::fixed, ios::floatfield);
outFile.setf(ios::showpoint);
outFile << setprecision(2);



//print heading
outFile << setfill(' ') <<setiosflags(ios:: left) << setw(11) <<
"Student" << setw(10) << "Test1"
<< setw(8) << "Test2" << setw(8) << "Test3" << setw(8) << "Test4"
<< setw(8) << "Test5"
<< setw(8) << "Average" << setw(8) << "Grade" << endl;
inFile >> studentName;
calculateAverage(inFile, studentAvg);
grade = calculateGrade(char studentAvg);
totalAverage = totalAverage + studentAvg;

numberOfStudents++;
}
classAverage = totalAverage / numberOfStudents;
outFile << "average" << classAverage <<endl;

outFile << setfill(' ') << setiosflags(ios::left) << setw(11) <<
studentName << setw (10) << testScore
<< setw(8) << testScore << setw(8) << testScore << setw(8)
<< testScore << setw(8) << testScore
<< setw(9) << studentAvg << setw(8) << grade<<endl;
inFile.close();
outFile.close();
system("PAUSE");
return 0;
}

void calculateAverage(ifstream& inFile, double& studentAverage)
{
double totalScore = 0;
int numberOfTests;
int testScore;

inFile>> testScore;

while(numberOfTests <= 5)
{
totalScore = totalScore + testScore;
numberOfTests++;
inFile >> testScore;
}
studentAverage = ( totalScore / numberOfTests);
}

char calculateGrade(char Average)
{
if (Average >= 90)
return 'A';
if (Average >= 80)
return 'B';
if (Average >= 70)
return 'C';
if (Average >= 60)
return 'D';
if (Average >= 50)
return 'F';
}
 
D

Daniel T.

"oLgAa25 said:
Hey, That was nice of you :)
you are fun guys around here. and I love that.
well my assignment was due on Sunday, but due to other reasons I got
extension.
No, I am not the same person.
I just became so desperate " Like desperate housewives" Which I am one
of them!! and was seeking help on the net
But since you are nice and helping me, I am going to be greedy and send
you the second code, the one with loop, but no output:)
any help is appreciated, *But no laughter*;-)

You took a baby step backward, the code below doesn't compile.

#include <iostream.h>
#include <stdlib.h>
#include <iomanip.h>
#include <string>
#include <fstream>

using namespace std;

void calculateAverage(ifstream& inFile, double& studentAverage);
char calculateGrade(char Average);

You have 'calculateGrade' taking a char for the average. A persons
average score is not a character, it's a real number (ie a double.)
int main()
{
ifstream inFile;// input stream variable
ofstream outFile;
string studentName;
int numberOfStudents = 0;
double studentAvg = 0;
double totalAverage = 0;
double classAverage;
char grade;
inFile.open("c:\\studentFile.txt");
outFile.open("c:\\student_out.txt");

if (!inFile)
{
cout << "Unable to open the file." <<endl;
return 1;
}

while(inFile)
{


outFile.setf(ios::fixed, ios::floatfield);
outFile.setf(ios::showpoint);
outFile << setprecision(2);



//print heading
outFile << setfill(' ') <<setiosflags(ios:: left) << setw(11) <<
"Student" << setw(10) << "Test1"
<< setw(8) << "Test2" << setw(8) << "Test3" << setw(8) << "Test4"
<< setw(8) << "Test5"
<< setw(8) << "Average" << setw(8) << "Grade" << endl;
inFile >> studentName;
calculateAverage(inFile, studentAvg);
grade = calculateGrade(char studentAvg);

The 'char' in the line above is misplaced. What are you trying to do
here?
totalAverage = totalAverage + studentAvg;

numberOfStudents++;
}
classAverage = totalAverage / numberOfStudents;
outFile << "average" << classAverage <<endl;

outFile << setfill(' ') << setiosflags(ios::left) << setw(11) <<
studentName << setw (10) << testScore
<< setw(8) << testScore << setw(8) << testScore << setw(8)
<< testScore << setw(8) << testScore
<< setw(9) << studentAvg << setw(8) << grade<<endl;

In the above line, "testScore" is undefined. What is it?
inFile.close();
outFile.close();
system("PAUSE");
return 0;
}

void calculateAverage(ifstream& inFile, double& studentAverage)
{
double totalScore = 0;
int numberOfTests;
int testScore;

inFile>> testScore;

while(numberOfTests <= 5)
{
totalScore = totalScore + testScore;
numberOfTests++;
inFile >> testScore;
}

Look very carefully at the code above. How many scores will it try to
take out of the inFile? Not five...
 
O

oLgAa25

Ok here is my problem,
How am I supposed to get testScore?
I know that was wrong,
I fixed the function, char,
but I still have this error.
Thanks a million
 
O

oLgAa25

Hello again,
I am back with Good and bad news.
The good news is: it compiles and no errors;-)
the bad news is the output is not what we are expected to do.
so helppppppppppp please
Olga
#include <iostream.h>
#include <stdlib.h>
#include <iomanip.h>
#include <string>
#include <fstream>

using namespace std;
void calculateAverage(ifstream& inFile, double& studentAverage);
char calculateGrade(double studentAvg);
int main()
{
ifstream inFile;// input stream variable
ofstream outFile;
string studentName;
int numberOfStudents = 0;
double studentAvg = 0;
double totalAverage = 0;
double classAverage;
char grade;
double testScore;
inFile.open("c:\\studentFile.txt");
outFile.open("c:\\student_out.txt");

if (!inFile)
{
cout << "Unable to open the file." <<endl;
return 1;
}
outFile << setfill(' ') <<setiosflags(ios:: left) << setw(11) <<
"Student" << setw(10) << "Test1"
<< setw(8) << "Test2" << setw(8) << "Test3" << setw(8) << "Test4"
<< setw(8) << "Test5"
<< setw(8) << "Average" << setw(8) << "Grade" << endl;
while(inFile)
{


outFile.setf(ios::fixed, ios::floatfield);
outFile.setf(ios::showpoint);
outFile << setprecision(2);





inFile >> studentName;
inFile >> testScore;
calculateAverage(inFile, studentAvg);
grade = calculateGrade(studentAvg);
totalAverage = totalAverage + studentAvg;

numberOfStudents++;
}
classAverage = totalAverage / numberOfStudents;
outFile << "average" << classAverage <<endl;

outFile << setfill(' ') << setiosflags(ios::left) << setw(11) <<
studentName << setw (10) << testScore
<< setw(8) << testScore << setw(8) << testScore << setw(8)
<< testScore << setw(8) << testScore
<< setw(9) << studentAvg << setw(8) << grade<<endl;
inFile.close();
outFile.close();
system("PAUSE");
return 0;
}

void calculateAverage(ifstream& inFile, double& studentAverage)
{
double totalScore = 0;
int numberOfTests;
int testScore;

inFile>> testScore;

while(numberOfTests <= 5)
{
totalScore = totalScore + testScore;
numberOfTests++;
inFile >> testScore;
}
studentAverage = ( totalScore / numberOfTests);
}

char calculateGrade(double Average)
{
if (Average >= 90)
return 'A';
if (Average >= 80)
return 'B';
if (Average >= 70)
return 'C';
if (Average >= 60)
return 'D';
if (Average >= 50)
return 'F';
}

Student Test1 Test2 Test3 Test4 Test5 Average Grade
average0.00
4975.00 75.00 75.00 75.00 75.00 0.00

help help please
 
B

BobR

oLgAa25 wrote in message
Ok here is my problem,
How am I supposed to get testScore?
I know that was wrong,
I fixed the function, char,
but I still have this error.
Thanks a million

If you are not Kathy, what are you doing with Kathy's code!!

You won't learn if we do your work for you (like last time with the 'Truck'
program).
Bouncing back and forth between here and
alt.comp.lang.learn.c/c++(DrNoose){1} is a cute trick. If you are that smart,
why can't you just be straight with us? We are only trying to help.

I hope I'm wrong and out-of-line, but, if I'm right, you can only lose by
doing things this way.

If I am wrong, I appologise.
 
O

oLgAa25

let me tell you this
I have no reason to lie,
I am not Kathy, I have my google, and Yahoo, accounts. and I have never
been to that group, but to be honest I will check it out

I have spend so much time on this code, and I am not here to play.
If you have something to say, that would be nice, If not, then may be
you can let someone else help.
If I can prove to you that I am not Kathy, Let me do it.
my e-mail is (e-mail address removed)
or (e-mail address removed)
would you like to have my credit card info too?
 
O

oLgAa25

one more thing
who is Kathy?
may be I can share with her my code, and we both can figure a solution
my program compiles and doesn't perform.
I don't know Kathy or anyone else
But I will check my class list to see if there is Kathy
 
O

oLgAa25

and one very last thing
I am posting my college account, and you can e-mail me there, and then
I will e-mail you back to see that my name is Olga
(e-mail address removed)

I am not doing it to make you happy, but after spending days on my
code, an then you come an accuse me of using someone else's this is not
fair
OK?
 
O

oLgAa25

one more thing,
how do I get to that group. ;-)
it would be nice to find out how,

and apologies accepted;-)
 
B

BobR

oLgAa25 wrote in message
and one very last thing
I am posting my college account, and you can e-mail me there, and then
I will e-mail you back to see that my name is Olga [snip address]

I am not doing it to make you happy, but after spending days on my
code, an then you come an accuse me of using someone else's this is not
fair
OK?

If you are Olga, then you are Olga. No need to prove anything to me (I did
put an apology at the bottom of my post). I just found it odd that your code
was identical to the other persons (maybe (s)he ripped you off!)(is the code
shared by the whole class?).

Do go over to alt.comp.lang.learn.c/c++ NG and take a look at the DrNoose
thread. You'll see why I questioned your identity (it has happened before,
deju vu all over again! <G>).

I, and others, will be glad to help you, but, you must help us:
1 - post the *shortest* program that exibits the problem you are having.
2 - clearly state the problem.
3 - show us the first few error msgs you get from your compiler, if any.

So, post your code (after reviewing 'DrNoose'), and we'll get you going. OK?

One thing to fix in your code:

// >#include <iostream.h>
#include <iostream> // C++
// >#include <stdlib.h>
#include <cstdlib> // C++
// >#include <iomanip.h>
#include <iomanip> // C++


I'm glad I was wrong, and again I offer my apology.
[ I did not mean to anger you. <G>]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Chatbot 0
Linker Error Help!! 2
setfill('\t') 5
NullPointerException error 3
Questions about new range for 2
clearing contents of ostringstream object 4
C++ help 3
Little help please 7

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top