473,322 Members | 1,718 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

IO stream

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;

}

Feb 28 '06 #1
56 3687
oLgAa25 wrote:
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-lit...tml#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

Feb 28 '06 #2
Oh thank you, but we are not at the constructors and destructors yet

Feb 28 '06 #3
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;

}

Feb 28 '06 #4
"oLgAa25" writes:
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.
{
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;

}

Feb 28 '06 #5
oLgAa25 wrote:
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

Feb 28 '06 #6
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

Feb 28 '06 #7
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

Feb 28 '06 #8
mlimber wrote:
oLgAa25 wrote:
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);
//...
}

Feb 28 '06 #9
In article <11**********************@v46g2000cwv.googlegroups .com>,
"oLgAa25" <ol********@yahoo.com> wrote:
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. ;-)
Feb 28 '06 #10
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';
}

Feb 28 '06 #11
In article <11**********************@e56g2000cwe.googlegroups .com>,
"oLgAa25" <ol********@yahoo.com> wrote:
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...
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';
}


--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 28 '06 #12
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

Feb 28 '06 #13
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

Feb 28 '06 #14

oLgAa25 wrote in message
<11*********************@i40g2000cwc.googlegroups. com>...
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.
--
Bob R
POVrookie
{1} one thru Comcast, one thru Google with an @news.east.earthlink.net
connection.
Feb 28 '06 #15
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 ol********@earthlink.net
or ol********@yahoo.com
would you like to have my credit card info too?

Mar 1 '06 #16
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

Mar 1 '06 #17
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
n0*******@imail.brevardcc.edu

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?

Mar 1 '06 #18
one more thing,
how do I get to that group. ;-)
it would be nice to find out how,

and apologies accepted;-)

Mar 1 '06 #19

oLgAa25 wrote in message
<11**********************@i39g2000cwa.googlegroups .com>...
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>]
--
Bob R
POVrookie
Mar 1 '06 #20
How do I go to DrNoose? I don't know how?
I don't know how
Thank you
and no,we don't share codes, but my code has been on the net, so may be
someone else took it.
and I don't see Kathy in my class list.
and again, my code compiles with no errors, . But here is my problem,
I don't know where I went wrong.
yes you got me angry, I have ignored my kids on Saturday and Sunday to
write the code, and then I don't get credit for it,even if it wasn't
working

C:\Documents and Settings\Owner\Desktop\COP_2334\AvgTestScore.cpp
compiled successfully

Mar 1 '06 #21

oLgAa25 wrote in message
<11*********************@e56g2000cwe.googlegroups. com>...
one more thing,
how do I get to that group. ;-)
it would be nice to find out how,

and apologies accepted;-)


Oooops! I 'slashed' where I should have 'dashed'!

news:alt.comp.lang.learn.c-c++

I don't know Mozilla, but, you should be able to paste the above into Google
as a last resort.
However you got here, you should be able to get there. <G>
(assuming your ISP carries that NG)

--
Bob R
POVrookie
Mar 1 '06 #22
This is more frustrating than solving my problem.
I got there, but then I have to set up my outlook, which I don' know
how. I need to set it up.

Mar 1 '06 #23

Ok here is the latest with me,
I have listed again the program
it compiles fine, but when I run it, I get this output

Student Test1 Test2 Test3 Test4 Test5 Average Grade
Balto85.00 85.00 85.00 85.00 85.00 0.00 F
7791.00 91.00 91.00 91.00 91.00 0.00 F
Mickey80.00 80.00 80.00 80.00 80.00 0.00 F
9593.00 93.00 93.00 93.00 93.00 0.00 F
Minnie79.00 79.00 79.00 79.00 79.00 0.00 F
1190.00 90.00 90.00 90.00 90.00 0.00 F
Doc92.00 92.00 92.00 92.00 92.00 0.00 F
3069.00 69.00 69.00 69.00 69.00 0.00 F
Goofy23.00 23.00 23.00 23.00 23.00 0.00 F
9638.00 38.00 38.00 38.00 38.00 0.00 F
Duckey60.00 60.00 60.00 60.00 60.00 0.00 F
4539.00 39.00 39.00 39.00 39.00 0.00 F
Grumby27.00 27.00 27.00 27.00 27.00 0.00 F
5274.00 74.00 74.00 74.00 74.00 0.00 F
Sunny93.00 93.00 93.00 93.00 93.00 0.00 F
8977.00 77.00 77.00 77.00 77.00 0.00 F
Piggy79.00 79.00 79.00 79.00 79.00 0.00 F
2893.00 93.00 93.00 93.00 93.00 0.00 F
Pluto85.00 85.00 85.00 85.00 85.00 0.00 F
4975.00 75.00 75.00 75.00 75.00 0.00 F
4975.00 75.00 75.00 75.00 75.00 0.00 F
average 0.00
4975.00 75.00 75.00 75.00 75.00 0.00 F

while my output shouldn't be that way,

It should be as follows
Student Test1 Test2 Test3 Test4 Test5 Average Grade
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

classAverage =

I know I missing up a loop somewhere, but I have no idea where.
My brain is dead
//************************************************** *******************************
// 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>
#include <stdlib.h>
#include <iomanip>
#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;

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;

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 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;

}

Mar 1 '06 #24

BobR wrote:
oLgAa25 wrote in message
<11*********************@e56g2000cwe.googlegroups. com>...
one more thing,
how do I get to that group. ;-)
it would be nice to find out how,

and apologies accepted;-)


Oooops! I 'slashed' where I should have 'dashed'!

news:alt.comp.lang.learn.c-c++

I don't know Mozilla, but, you should be able to paste the above into Google
as a last resort.
However you got here, you should be able to get there. <G>
(assuming your ISP carries that NG)

--
Bob R
POVrookie


Mar 1 '06 #25
"oLgAa25" writes:
Ok here is the latest with me,
I have listed again the program
it compiles fine, but when I run it, I get this output

Student Test1 Test2 Test3 Test4 Test5 Average Grade
Balto85.00 85.00 85.00 85.00 85.00 0.00 F
7791.00 91.00 91.00 91.00 91.00 0.00 F
Mickey80.00 80.00 80.00 80.00 80.00 0.00 F
9593.00 93.00 93.00 93.00 93.00 0.00 F
Minnie79.00 79.00 79.00 79.00 79.00 0.00 F
1190.00 90.00 90.00 90.00 90.00 0.00 F
Doc92.00 92.00 92.00 92.00 92.00 0.00 F
3069.00 69.00 69.00 69.00 69.00 0.00 F
Goofy23.00 23.00 23.00 23.00 23.00 0.00 F
9638.00 38.00 38.00 38.00 38.00 0.00 F
Duckey60.00 60.00 60.00 60.00 60.00 0.00 F
4539.00 39.00 39.00 39.00 39.00 0.00 F
Grumby27.00 27.00 27.00 27.00 27.00 0.00 F
5274.00 74.00 74.00 74.00 74.00 0.00 F
Sunny93.00 93.00 93.00 93.00 93.00 0.00 F
8977.00 77.00 77.00 77.00 77.00 0.00 F
Piggy79.00 79.00 79.00 79.00 79.00 0.00 F
2893.00 93.00 93.00 93.00 93.00 0.00 F
Pluto85.00 85.00 85.00 85.00 85.00 0.00 F
4975.00 75.00 75.00 75.00 75.00 0.00 F
4975.00 75.00 75.00 75.00 75.00 0.00 F
average 0.00
4975.00 75.00 75.00 75.00 75.00 0.00 F

while my output shouldn't be that way,

It should be as follows
Student Test1 Test2 Test3 Test4 Test5 Average Grade
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

classAverage =

I know I missing up a loop somewhere, but I have no idea where.
My brain is dead
//************************************************** *******************************
// 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>
#include <stdlib.h>
#include <iomanip>
#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;
What is the purpose of that line? To read the first test score? In a
moment you are going to tell calculate average to read the test scores.
Files are like miniature tape players. If you go forward, you have to
rewind (or at least reposition) if you want to hear something again. I'm
sure there is a lot more to be found but that is the first thing I noticed.
calculateAverage(inFile, studentAvg);
grade = calculateGrade(studentAvg);
totalAverage = totalAverage + studentAvg;

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;

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 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;

}

Mar 1 '06 #26
what line?

Mar 1 '06 #27
"oLgAa25" writes:
what line?


Do you see a blue line that says "Show quoted text"?

That is not an advisement for a shoe store. It is something to press to
make that idiotic interface semi-useful.
Mar 1 '06 #28
How about This,
why don't you guys stop making fun of people like us. May be if you had
a useful thing to say, then that would be appreciated, but just sitting
down and comparing the blue link to a shoe store, that is not fun. It
is called disrespect.

Yes I saw the blue link last night , and I am not sure what line you
are talking about.
I mean I know that I have to add a line to get the next score, but my
question is where?
you help is appreciated, but your disrespect is not.

Mar 1 '06 #29

"oLgAa25" <ol********@yahoo.com> wrote in message
news:11**********************@p10g2000cwp.googlegr oups.com...
How about This,
why don't you guys stop making fun of people like us. May be if you had
a useful thing to say, then that would be appreciated, but just sitting
down and comparing the blue link to a shoe store, that is not fun. It
is called disrespect.

Yes I saw the blue link last night , and I am not sure what line you
are talking about.
I mean I know that I have to add a line to get the next score, but my
question is where?
you help is appreciated, but your disrespect is not.
Google has single handedly just about ruined Usenet. Much of the
frustration is actually directed at them. They stupidly ignored ten years
of successful usage and said "I have seen the future and here it is". So we
now have Googlenet embedded in Usenet.

This is a copy from Google with my suggested reading mode.
------------ snip ------------
From: osmium - view profile
Date: Tues, Feb 28 2006 7:59 pm
Email: "osmium" <r124c4u...@comcast.net>
Groups: comp.lang.c++
Not yet ratedRating:
show options
Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Report Abuse | Find messages by this author


- Hide quoted text -
- Show quoted text -
#include <iostream>
#include <stdlib.h>
#include <iomanip>
#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;


What is the purpose of that line? To read the first test score? In a
moment you are going to tell calculate average to read the test scores.
Files are like miniature tape players. If you go forward, you have to
rewind (or at least reposition) if you want to hear something again. I'm
sure there is a lot more to be found but that is the first thing I noticed.

------------- end snip----------------

"that line" refers to the line immediately above the point where I inserted
my comment. The one that says
inFile >> testScore;

I believe the assignment is worded such that a non-ugly solution can not be
made. You are forbidden to use the function calculate average (whatever the
formal name is ) as a function, i.e., you are directed to return a void. IMO
it should return a bool, indicating whether EOF was detected or not. Then,
most of the logic could be done in the calculate function. The reading of
the file, capturing the needed data, and writing the output file. It
returns to the caller (main) whether it was successful or not and the
numeric score for the most recent student. main would be almost empty, it
would open and close the two files, aggregate the cumulative class score and
keep track of how many are in the class. After the last student is
processed it would compute and print the class average. Reading of the
students name and the five scores would all be done in the calculate average
function. The best workaround I see is this prototype:

void calculateAverage(ifstream& in, ofstream& out, double& numeric_score,
bool& success);

The last parameter passes the EOF information back to the caller despite the
straight jacket you instructor has put you in.

Note that you do not need names for five test scores *or* an array of test
scores.
Mar 1 '06 #30
Olga wrote:

I just re-read you post.
I mean I know that I have to add a line to get the next score, but my
question is where?


The point was to read all five scores in one place. You seems to be reading
one score here and then five more later on. But there are only five scores,
not six.

Mar 1 '06 #31
so basically, I will have to change my calculateAverage Function,
hmmm

I have changed the test scores names, I am no longer taking 5 test
scores, I am just doing a loop in the CalculateAverage Function

and thank you again for the input.Although I don't understand what you
said exactly about Google ;-) but for now I will live with that.

Olga

Mar 1 '06 #32
"oLgAa25" wrote:
so basically, I will have to change my calculateAverage Function,
hmmm


No, you don't *have* to. I am just saying it is plug ugly to read the
student name in one function and read the scores in another function. A
student *record* is a student's name and his five test scores. I prefer to
concentrate responsibility in one place. You can patch up what you have
and continue if you prefer. I realize the clock is running....
Mar 1 '06 #33
In article <11**********************@e56g2000cwe.googlegroups .com>,
"oLgAa25" <ol********@yahoo.com> wrote:
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


Olga, you still have the same problem. You are trying to extract 7
testscores for each student, but they only each have 5. You extract one
in main right after reading the students name, then you go into
calculateAverage where you go through a loop 6 times (0, 1, 2, 3, 4, 5)
is 6 iterations.

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Mar 1 '06 #34
Ok this is it for me, I am going Nutssss.... here. I don't know what I
am doing wrong any more
I started out by having a problem with my output coming out in bad
way, and now I am back to no where :(

I don't know what else to do

Thank you all for the help

Mar 1 '06 #35
oLgAa25 wrote:
Ok this is it for me, I am going Nutssss.... here. I don't know what I
am doing wrong any more
I started out by having a problem with my output coming out in bad
way, and now I am back to no where :(

I don't know what else to do


Have you learnt how to use your debugger yet?

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 1 '06 #36
nop:(

Mar 1 '06 #37
Guys, do you get paid, or do you that because you like helping people
like me?
because you do a good job, and I realy appreciate that.

Olga

Mar 1 '06 #38
oLgAa25 wrote:
nop:(


Then now would be the time.

If you need help, then visit the appropriate group for that debugger as
it's off topic here.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 1 '06 #39
Then why do I need to learn it. ? what for ? can you give details ?
Thank you
Olga

Mar 1 '06 #40
oLgAa25 wrote:
Then why do I need to learn it. ? what for ? can you give details ?


It allows you to step through your code line by line and analyse the
values of variables at runtime.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 1 '06 #41
Olga wrote:
Ok this is it for me, I am going Nutssss.... here. I don't know what I
am doing wrong any more
I started out by having a problem with my output coming out in bad
way, and now I am back to no where :(


I took another look at your code from 7:14 last night It looks pretty bad.
You read the individual scores in the average function but then you try to
write them in main. They are gone now, like my tape player analogy.
Whoever does the reading should also do the writing. I assume you know about
arrays - you shouldn't have been given this assignment if you don't. Try
changing the signature of the average function to (my variable names)

void student_average(int scores[5], double& average_score)

The function can no longer read the file. main must do all the file
manipulation, collect five scores, put them in an array and pass them to
student_average It's a convoluted way to do this, but ISTM to be what your
instructor wants.

The 7:14 edition has EOF detection wrong, so you get two Plutos. There is
an uninitialized variable "numberof tests" or some such, in the average
function.

Leave almost all of the formatting out, it isn't necessary and it just makes
something rather simple look complicated. And it takes time to type and
correct the typing and syntax errors. Get it working. Then, and only then,
make it pretty.

Check for end of file immediately after you read a student name. *Then* is
when the system realizes it is at end of file.
Mar 1 '06 #42
Nop, no arrays, and that is the problem. We are still not there yet.

Mar 1 '06 #43

oLgAa25 wrote in message ...
Guys, do you get paid,
Yes, when we do the work. If we do your homework, we expect you to transfer
the grades and course credits to our accounts. <G> But, that's why we don't
do the work, you would not learn anything! If I (or others) posted a program
for you, using our experience, and you turned it in, your teacher would fail
you for obvious reasons (you don't know that stuff yet.).
or do you that because you like helping people
like me?
because you do a good job, and I realy appreciate that.
Olga


I started helping because I was helped, and it feels good when I get it
right. Matter of fact, Mr. 'osmium' is one of the many who have aided me over
the last five years ( I came up to C++ from Assembler in 2000.(hobbiest,
wanna be pro<g>)).

A good book(s) is a must, but, you can learn much from reading this and other
(C++) newsgroups.

--
Bob R
POVrookie
Mar 1 '06 #44

oLgAa25 wrote in message
<11**********************@i39g2000cwa.googlegroups .com>...
Ok this is it for me, I am going Nutssss.... here. I don't know what I
am doing wrong any more
I started out by having a problem with my output coming out in bad
way, and now I am back to no where :(
I don't know what else to do
Thank you all for the help


One way to find problem code:
Comment-out code until it compiles and runs as expected. Then un-comment, a
few lines at a time, until the errors crop up. Now you've isolated the
problem code, and can concentrate on that area.
Always break-up the program into small tasks. Don't try to do everything in
one function.

I know you are pressed for time right now, but, read-up on your debugger when
you get time. It is a valuable tool.

--
Bob R
POVrookie
Mar 1 '06 #45
"oLgAa25" writes:
Nop, no arrays, and that is the problem. We are still not there yet.


Well, try this then. Write code for this function.

void stud_average(ifstrean& in, ofstream& out, double& mean score)

Read the student name and detect end of file in main. Then call
stud_average which will read five scores and as each one is read it is
written to the output file. it computes the mean score and then returns to
main. main will write the score returned, compute and store the letter
grade, and then (attempt to ) read the next students name. No arrays and no
need for five variable names. I don't really like it but one has to follow
the rules. AFAIK this complies with all the rules, will work and doesn't
depend on any additional knowledge on your part.
Mar 1 '06 #46
Oh sure ;-), well Thank you for volunteering your time.
what is a good book to learn, because my book talk more than it
delivers.

I tried to to go to the newsgroups website , but God, it took me to an
unwanted place.
so I need to setup my earth link account.

I need to find the setting directions.

Good sense of humor guys, nice to have troubles with you;-)

Mar 1 '06 #47
see you are right,
I wrote my void function and my calculateAverage and I had no errros,
but when it came to putting my code together, that is when I paniced at
firct, and then *Never Worked* ;-)

but again I am just at the begining of C++, and still have a long way
to go. But to be honest I don't think that I will be making a living of
c++ coding :)

Mar 1 '06 #48
Oh well guys, after I gave up on my machine, I sent my code to
classmate to compile it for me, and she sent me the output.
It was as we the teacher asked us to do. so I think that my compiler is
getting old. I am using Dev-C++
hmmmm I need to change it

Olga

Mar 2 '06 #49

oLgAa25 wrote in message
<11**********************@e56g2000cwe.googlegroups .com>...
Oh well guys, after I gave up on my machine, I sent my code to
classmate to compile it for me, and she sent me the output.
It was as we the teacher asked us to do. so I think that my compiler is
getting old. I am using Dev-C++
hmmmm I need to change it
Olga


<OT>
No, Dev-C++ is not a compiler. It is an IDE, and it usually comes with the
window$ port of GCC (GNU Compiler Collection) called MinGW.
You should be using Dev-C++ with a version 4.9.9.2 or newer (4.9.9.1 came
with GCC 3.3.1 [1]). If yours is older, then you should update. Click
'Tools -->check for updates/packages', or go to the Bloodshed site if your
Dev-C++ is old(and does not have that option).
Dev-C++ IDE: http://www.bloodshed.net/

[1] put "std::cout<<__VERSION__<<std::endl;" in main() to see your GCC
version.
</OT>
--
Bob R
POVrookie
Mar 2 '06 #50

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: andrewcw | last post by:
OK I am half way there - I can manipulate the stream without the byte issue like this - but is this the way to push the new values back into the stream & write out the stream without resorting to...
5
by: andrewcw | last post by:
I have an object to serialize. TextWriter writer = new StreamWriter("test.xml"); serializer.Serialize(writer,obj); writer.Close(); but below does not, why ?? I have a file that I will have...
3
by: MJB | last post by:
I'm getting an IStream back from function xmlHttp.responsestream. I would like to convert this to a System.IO.Stream in order to work with it in my application. Has anyone encountered this and...
4
by: TT (Tom Tempelaere) | last post by:
Hey there, I need a string stream, but I can't find one in .NET. I thought StringWriter would derive from Stream, alas it doesn't do so. Which leads me to my next question: What is the purpose...
4
by: fastback66 | last post by:
Evidently per the C++ standard, it is not possible to measure the distance between two stream iterators, because two non-end-of-stream iterators are equal when they are constructed from the same...
8
by: Marc Gravell | last post by:
I want to write a method that will accept a stream as a parameter, and which will write xml to the stream (based in reality on database results) using the XmlTextWriter class. However, this insists...
4
by: Helge Jensen | last post by:
In C# 2.0 System.IO.Stream is declared as: public class Stream: ..., IDisposable { ... public void Dispose(); public void Dispose(bool); IDisposable.Dispose(); } Which must be a...
0
by: mario.lat_ | last post by:
Hallo to all, I have write a little script for connecting to cisco router BUT I have a problem: I have to send to router all the commands and then I have to read the output. If I send a command1...
3
by: sven.suursoho | last post by:
Hello, In main(), the first output API is what I try to achieve. Unfortunately it fails, printing first string as pointer instead of human readable message. Tried to initialize str(""), set new...
4
by: Scott F. Brown | last post by:
Greetings all... I was playing around with compressing streams and came across a behavior that I do not understand. I create a stream (input) from the contents of a textbox. That stream is...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.