#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
#include<string>
#include<iostream>
#include<iomanip>

using namespace std;

int main()
{
	ifstream infile;
	ofstream outfile;

	int num;//number of grades
	int Hi;//highest test grade
	int Low;//lowest test grade
	sting stat;//status of grade, whether higher or lower than avverage

	infile.open ("grades.txt");
	outfile.open ("gradeOutput");

	double number;//the grades as in the infile

	while (getline(infile,number,'\n'))
	{	
		cout<<"The number of test scores : "<<num<<endl;
		double average = (number/num);//average of all the grades
		cout<<"The average of the test scores : "<<average<<endl;
		cout<<"The highest test score was "<<Hi<<" and the lowest test grade was "<<Low<<endl;
		cout<<"Grade "<<setw(5)<<"Status"<<endl;

		if (number<average)
		{
		cout<<number<<setw(5)<<" Below"<<endl;
		}
		else if (number>average)
		{
		cout<<number<<setw(5)<<" Above"<<endl;
	}
	
	return 0;
}

The assignment is as follows:
Write a c++ program USING FUNCTIONS, that will read in an unknown number (not
more than 60) of test grades from a text file named "functest.txt"(make your own). The
program is to calculate and output the following:
1. Number of Test Grades
2. Average of the Test Grades ( 2 decimal places)
3. The Highest and Lowest Test Grades
4. A list of the grades and whether the grade is ABOVE or BELOW the
average.
The grade and status should take the following format:
Grade Status
XX ABOVE
XX BELOW
etc.
5. Output the results of all the calculations
You may assume that no grade will be exactly equal to the average.

A few things:
1. How can you determine the number of grades in the infile?
2. How can you create a function for just reading in the information from the infile?
3. To see which of the grade is higher and lower...i'm sure that a series of if/else statements could work, but it wouldn't be practical is you had several hundred records. How do you find out the highest and lowest grade from the infile?
4. With regards to the output, to get it formatted as is requested (above)...i'm familiar with using something like
if (i%2==0)
print<<endl;
info.....
But in this case how could I use that to manipulate the headers Grade & Status?
My infile is as follows:

69
57
94
67
46

Thank you.

Use >> in lieu of getline (it is not a C++ function)

Ok thx for the feedback, however, I've completed programs with the use of the getline function and it works ok. In other words what makes the difference between the use of (infile>>number) and (getline(infile,number,'\n')) in this particualr program?

>I've completed programs with the use of the getline function and it works ok.
I bet those programs didn't try to pass a double into getline instead of a string:

double number;//the grades as in the infile

while (getline(infile,number,'\n')

There are other problems too:

>int num;//number of grades
>int Hi;//highest test grade
>int Low;//lowest test grade
You use these but never give them a value.

>sting stat;
Sting? Perhaps you meant to type string.

else if (number>average)
	{
	cout<<number<<setw(5)<<" Above"<<endl;
}

You forgot to close the block on that else if statement.

#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
#include<string>
#include<iostream>
#include<iomanip>

using namespace std;

int readInfo(); 

int main()
{
  int returnVal;
  returnVal = readInfo();
  return returnVal;
}

int readInfo()
{
    ifstream infile;
    ofstream outfile;

    int num = 0;//number of grades
    int Hi = 0;//highest test grade
    int Low = 0;//lowest test grade
    string stat;//status of grade, whether higher or lower than average

    infile.open ("grades.txt");
    outfile.open ("gradeOutput");

    double number;//the grades as in the infile

	int i = 0;
	int storedGrades[60];
    while (infile>>number)
    {    
		storedGrades[i] = number;
        cout<<"The number of test scores : "<<num<<endl;
        double average = (number/num);//average of all the grades
        cout<<"The average of the test scores : "<<average<<endl;
        cout<<"The highest test score was "<<Hi<<" and the lowest test grade was "<<Low<<endl;
        cout<<"Grade "<<setw(5)<<"Status"<<endl;

        if (number<average)
        {
        cout<<number<<setw(5)<<" Below"<<endl;
        }
        else if (number>average)
        {
        cout<<number<<setw(5)<<" Above"<<endl;
    }
	}
    
    return 0;
}

My infile:

48
69
38
79
92

My output:

The number of test scores : 0
The average of the test scores : 1.#INF
The highest test score was 0 and the lowest test grade was 0
Grade Status
48 Below
The number of test scores : 0
The average of the test scores : 1.#INF
The highest test score was 0 and the lowest test grade was 0
Grade Status
69 Below
The number of test scores : 0
The average of the test scores : 1.#INF
The highest test score was 0 and the lowest test grade was 0
Grade Status
38 Below
The number of test scores : 0
The average of the test scores : 1.#INF
The highest test score was 0 and the lowest test grade was 0
Grade Status
79 Below
The number of test scores : 0
The average of the test scores : 1.#INF
The highest test score was 0 and the lowest test grade was 0
Grade Status
92 Below
Press any key to continue . . .

The problems:
1. It's not telling me the number of test scores.
2. The average is not calculating.
3.Because the above 2 aren't working, the status is wrong.
4.How can I get it to show the highest and lowest grade.
5.The grade & status should be in colums form with the headers only appearing once. I guess because of how it is in the loop, the headers will repeat.
Thanks to all for their input.

You never increment i or num each time you go through the loop.

Remember, trying to divide by 0 will cause problems!

Given the requirements of the project I'd read the entire file into a container, then I'd loop through the container to add all the grades and divide by the number of grades to get the average. Then I'd loop through the container a second time to determine how many grades are above or below average and to determine the highest and lowest.

If you don't know the number of grades before reading the file or if the number of grades isn't embedded in the file someplace then you will either need to guess the number of grades and maybe create an array that's too big, write your own code to "expand" the array if it's not big enough as you read through, or use a standard expandable container like vector or list that will expand as required as you read through the file.

Ok lerner, thx much for your input. i'll repost my code with the modifcations.

#include <iomanip>
#include <cmath>
#include <fstream>
#include<string>
#include<iostream>


using namespace std;

int readInfo(); 

int main()
{
  int returnVal;
  returnVal = readInfo();
  return returnVal;
}

int readInfo()
{
    ifstream infile;
    ofstream outfile;

    int num = 0;//number of grades
    int Hi =0;//highest test grade
    int Low=0;//lowest test grade
    string stat;//status of grade, whether higher or lower than average

    infile.open ("gradesA.txt");
    outfile.open ("gradeOutput2");
    double number = 0;
    double average;

    int count = 0;
    int storedGrades[60];
    while (!infile.eof())
    {    
		storedGrades[count] = number;    
		++count;
	}    
	{
        average = (number/num);//average of all the grades
		cout<<number<<endl<<endl;
        if (number<average)
        {
        cout<<number<<setw(5)<<" Below"<<endl;
        }
        else if (number>average)
        {
        cout<<number<<setw(5)<<" Above"<<endl;
        }
        //increment for number of grades?
		num++;
	}
        
        cout<<"The number of test scores : "<<num<<endl;
        cout<<"The average of the test scores : "<<average<<endl;
        cout<<"The highest test score was "<<Hi<<" and the lowest test grade was "<<Low<<endl;
        cout<<"Grade "<<setw(5)<<"Status"<<endl;
       
    infile.close();
    outfile.close();
    return 0;
}

Good day. I know the code isn't complete, but any reason why nothing isn't being printed on the screeen even though it compiles ok? Thx.

I found an assignment that was pretty similar to mine, and did a bit of mofifications accordingly. I'm having errors with it. Their was a suggestion to use vectors in the program, we haven't reached vectors yet in our course, but below is the code along with the syntax error messages. Any additional info/pointers is greatly appreciated.

#include <iomanip>
#include <cmath>
#include <fstream>
#include <vector>
#include<string>
#include<iostream>


using namespace std;

int main()

ifstream infile;
ofstream outfile;

infile.open ("gradesA.txt");
outfile.open ("gradeOutput");



vector<int> testScores;    
while(!infile.eof())
    {
    int grades;        
    infile >> grades;
    testScores.push_back(grades);
    }

int testScoresSize = testScores.size();
int sumofScores = 0;
double totalScores = testScores.size();
for (int i = 0; i < totalScores; i++)
{
        int max = 0;
        min = testScores.at(1);  
    for ( int i = 0; i < int (testScores.size()); i++ )
    {
        for ( int n = 0; n < int (testScores.size()); n++ )
        {
            int ival = testScores.at(i),   
                nval = testScores.at(n);  
            if (ival >= nval && ival >= max)
            {
                max = testScores.at(i); 
            }
            if (ival < nval && ival < min)
            {
                min = testScores.at(i); 

            }
        }
    }
    cout << "AND THE MAX IS:  " << max << endl;
    cout << "AND THE MIN IS:  " << min << endl;

    for each (int testscore in testScores) 
    {
  if 
      (testscore < testScoreAverage) 
  {
     cout << testscore << " Below" << endl;
  }
  if (testscore > testScoreAverage) 
  {
     cout << testscore << " Above" << endl;
  if (testscore == testScoreAverage) 
  {
     cout << testscore << " Average" << endl;
  }
}
    infile.close();
    outfile.close();
    return 0;
}

The syntax error messages:

1>c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(16) : error C3646: 'ifstream' : unknown override specifier
1>c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(16) : error C3646: 'infile' : unknown override specifier
1>c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(19) : error C2143: syntax error : missing ';' before '.'
1>c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(19) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(20) : error C2143: syntax error : missing ';' before '.'
1>c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(20) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(20) : error C2371: 'outfile' : redefinition; different basic types
1>        c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(17) : see declaration of 'outfile'
1>c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(25) : error C2059: syntax error : 'while'
1>c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(26) : error C2143: syntax error : missing ';' before '{'
1>c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(26) : error C2447: '{' : missing function header (old-style formal list?)
1>c:\users\neil\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(35) : error C2059: syntax error : 'for'
1>c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(35) : error C2143: syntax error : missing ')' before ';'
1>c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(35) : error C2143: syntax error : missing ';' before '<'
1>c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(35) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(35) : error C2143: syntax error : missing ';' before '++'
1>c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(35) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(35) : error C2086: 'int i' : redefinition
1>        c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(35) : see declaration of 'i'
1>c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(35) : error C2059: syntax error : ')'
1>c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(36) : error C2143: syntax error : missing ';' before '{'
1>c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(36) : error C2447: '{' : missing function header (old-style formal list?)
1>c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(79) : fatal error C1004: unexpected end-of-file found
1>Build log was saved at "file://c:\Users\Documents\Visual Studio 2008\Projects\rastaschool\rastaschool\Debug\BuildLog.htm"
1>rastaschool - 21 error(s), 0 warning(s)
Member Avatar for iamthwee

I wonder how many times we've mentioned the problem apparent in the following idiom: while(!infile.eof()) I know I have at least twice.

> for each Isn't portable. At least I think it isn't.

Thanks for your input, any reason why my ifstream shows an error. Also with regards to the infile.eof(), our professor said it was necessary for it to be there to read through the entire documents, even though i know that getline could do it. My professor tactually took off 10% of my assignment because i didn't use the while(!infile.eof()). It compiled fine without it and got the results just like anyone else. Any reason why he may harp on that so much? Beside there are otherways, to have the compiler show if their is an error if the infile isn't working. Thx to all for their input.

Member Avatar for iamthwee

>My professor tactually took off 10% of my assignment because i didn't use the while(!infile.eof())...any reason why he may harp on that so much?

From a pragmatic p.o.v he's wrong. Go tell him that.

>Beside there are otherways, to have the compiler show if their is an error if the infile isn't working.
You have been shown this countless number of times. I know I don't like repeating myself as I'm sure many others will agree.

>My professor tactually took off 10% of my assignment
>because i didn't use the while(!infile.eof()).
What an ass. You can't reason with people like that, so just do what he wants and be sure never ever to trust what he teaches you without verifying it first. You can even add comments to the bad code to let us know that you're being forced to do it. If he takes anything off of your grade for comments (as long as they don't break any rules or laws), you can report him to the school.

Professors now-a-days.... there are basic syntax errors such as problem with my infile statement and the 'for' statement. any ideas why i'm having these problems.

>any ideas why i'm having these problems.
Yea, you're cutting and pasting code without knowing what you're doing, running to us when you get any easily fixed syntax error, and generally being lazy. Syntax errors are the easiest of problems to locate and fix. The compiler tells you the line number to start at, and exactly what it expected or didn't expect to find. When you know where to look (such as a line number), even a rudimentary understanding of C++ syntax means you can glance at the code and see if something is missing or shouldn't be there. You have at least that understanding, which suggests that you're just not trying.

:( sorry for my laziness. With regards to the errors, im pretty sure that the 'infile' statement is correct. I think I have all the necessary header files. But it says that 'infile', 'ifstream' & 'cout' are: unknown override specifier or see declaration of 'outfile'. I've never had this problem before. The name of the infile is correct, but the funny thing is that the 'outfile' is created.

>With regards to the errors, im pretty sure that the 'infile' statement is correct.
It is correct. The line number that a compiler gives you is where the error was noticed. It's best to look at the line you're told, and about five lines above it. If you do that, you'll see that your definition of main is missing an opening brace.

It's best to look at the line you're told, and about five lines above it.

Good tip, thx for that one. The '{' for the main function totally slipped me...thx again.

What does the following mean?:

error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::vector<_Ty>' (or there is no acceptable conversion)

That error is regarding my statement:

ifstream infile;
ofstream outfile;

infile.open ("gradesA.txt");
outfile.open ("gradeOutput");

vector <int> testScores;
while(infile >> testScores)
	{
	int grades;        
	infile >> grades;
	testScores.push_back(grades);
	}

Thanks for the input.

It means that the >> operator isn't overloaded for the vector class. In other words, it won't work.

How could I alter it to accomplish what i want? If i just declare it as a regular 'int', i'll still have problems though.

>How could I alter it to accomplish what i want?
I don't know what you want, but replacing the entire loop with this makes sense to me:

int grades;

while ( infile>> grades )
  testScores.push_back ( grades );
#include <iomanip>
#include <cmath>
#include <fstream>
#include <vector>
#include<string>
#include<iostream>

using namespace std;

int main()
{

ifstream infile;
ofstream outfile;

infile.open ("gradesA.txt");
outfile.open ("gradeOutput");

int grades;
while ( infile>> grades )
testScores.push_back ( grades );

int testScoresSize = testScores.size();
int sumofScores = 0;
double totalScores = testScores.size();
for (int i = 0; i < totalScores; i++)
{
        int max = 0;
        min = testScores.at(1);  
    for ( int i = 0; i < int (testScores.size()); i++ )
    {
        for ( int n = 0; n < int (testScores.size()); n++ )
        {
            int ival = testScores.at(i),   
                nval = testScores.at(n);  
            if (ival >= nval && ival >= max)
            {
                max = testScores.at(i); 
            }
            if (ival < nval && ival < min)
            {
                min = testScores.at(i); 

            }
        }
        }
    }
    cout << "AND THE MAX IS:  " << max << endl;
    cout << "AND THE MIN IS:  " << min << endl;

    for (int testscore in testScores) 
    {
  if 
      (testscore < testScoreAverage) 
  {
     cout << testscore << " Below" << endl;
  }
  if (testscore > testScoreAverage) 
  {
     cout << testscore << " Above" << endl;
  }
    }
    }

    infile.close();
    outfile.close();
    return 0;
}

some of the syntax that i get just don't make any sense, for example it will say that one of my statements are missing a ';', but it's actually there already.

Member Avatar for iamthwee

If you worked on indentation you'll realise you have an extra } lying around.

You need to declare vector <int> testScores; before using it.

Probably declare

int max = 0;
int min = 0;

Outside the for loop. Actually declare int min=0 And get rid of for ( int testscore in testScores ) or change it as the syntax is incorrect.

There are most probably logic errors but I'll leave that for you to unravel.

Thanks for catching that extra brace.

#include <iomanip>
#include <cmath>
#include <fstream>
#include <vector>
#include<string>
#include<iostream>

using namespace std;

int main()
{

ifstream infile;
ofstream outfile;

infile.open ("gradesA.txt");
outfile.open ("gradeOutput");

int grades;
vector <int> testScores;
while ( infile>> grades )
testScores.push_back ( grades );

int testScoresSize = testScores.size();
int sumofScores = 0;
double totalScores = testScores.size();
int min=0;
for (int i = 0; i < totalScores; i++)
{
		int max = 0;
        min = testScores.at(1);  
    for ( int i = 0; i < int (testScores.size()); i++ )
    {
        for ( int n = 0; n < int (testScores.size()); n++ )
        {
            int ival = testScores.at(i),   
                nval = testScores.at(n);  
            if (ival >= nval && ival >= max)
            {
                max = testScores.at(i); 
            }
            if (ival < nval && ival < min)
            {
                min = testScores.at(i); 

            }
		}
       
    }
    cout << "AND THE MAX IS:  " << max << endl;
    cout << "AND THE MIN IS:  " << min << endl;
	{
  if 
	  (testscore < testScoreAverage) 
  {
     cout << testscore << " Below" << endl;
  }
  if (testscore > testScoreAverage) 
  {
     cout << testscore << " Above" << endl;
  }
	
}

    infile.close();
    outfile.close();
    return 0;
}

It says that 'testscore' & 'testScoreAverage' are undeclared identifiers.

Member Avatar for iamthwee

Don't you think you should look at a vector tutorial before just jumping into it? Otherwise it is just going to be a guessing game with us providing your answers.

Is there anyway i could bypass the vector?

Member Avatar for iamthwee

The easiest way would be to declare an array with a huge initialiser. Such that you know it won't overflow the buffer.

But there a limitations with that.

ok i guess i'll stay with the vector although our professor will test the code with 60 records, so i guess an array to hold 60...

Be a part of the DaniWeb community

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