954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

c++ functions for grades

#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<69
57
94
67
46


Thank you.

zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
 

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

ithelp
Nearly a Posting Maven
Banned
2,230 posts since May 2006
Reputation Points: 769
Solved Threads: 128
 

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?

zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
 

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

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
#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.

zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
 

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.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

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

zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
 
#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.

zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
 

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.

<code>#include &lt;iomanip&gt;
#include &lt;cmath&gt;
#include &lt;fstream&gt;
#include &lt;vector&gt;
#include&lt;string&gt;
#include&lt;iostream&gt;


using namespace std;

int main()

ifstream infile;
ofstream outfile;

infile.open (&quot;gradesA.txt&quot;);
outfile.open (&quot;gradeOutput&quot;);



vector&lt;int&gt; testScores;    
while(!infile.eof())
	{
	int grades;        
	infile &gt;&gt; grades;
	testScores.push_back(grades);
	}

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

            }
        }
    }
    cout &lt;&lt; &quot;AND THE MAX IS:  &quot; &lt;&lt; max &lt;&lt; endl;
    cout &lt;&lt; &quot;AND THE MIN IS:  &quot; &lt;&lt; min &lt;&lt; endl;

	for each (int testscore in testScores) 
	{
  if 
	  (testscore &lt; testScoreAverage) 
  {
     cout &lt;&lt; testscore &lt;&lt; &quot; Below&quot; &lt;&lt; endl;
  }
  if (testscore &gt; testScoreAverage) 
  {
     cout &lt;&lt; testscore &lt;&lt; &quot; Above&quot; &lt;&lt; endl;
  if (testscore == testScoreAverage) 
  {
     cout &lt;&lt; testscore &lt;&lt; &quot; Average&quot; &lt;&lt; endl;
  }
}
    infile.close();
    outfile.close();
    return 0;</code> 
}


The syntax error messages:

<code>1&gt;c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(16) : error C3646: 'ifstream' : unknown override specifier
1&gt;c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(16) : error C3646: 'infile' : unknown override specifier
1&gt;c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(19) : error C2143: syntax error : missing ';' before '.'
1&gt;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&gt;c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(20) : error C2143: syntax error : missing ';' before '.'
1&gt;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&gt;c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(20) : error C2371: 'outfile' : redefinition; different basic types
1&gt;        c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(17) : see declaration of 'outfile'
1&gt;c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(25) : error C2059: syntax error : 'while'
1&gt;c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(26) : error C2143: syntax error : missing ';' before '{'
1&gt;c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(26) : error C2447: '{' : missing function header (old-style formal list?)
1&gt;c:\users\neil\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(35) : error C2059: syntax error : 'for'
1&gt;c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(35) : error C2143: syntax error : missing ')' before ';'
1&gt;c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(35) : error C2143: syntax error : missing ';' before '&lt;'
1&gt;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&gt;c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(35) : error C2143: syntax error : missing ';' before '++'
1&gt;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&gt;c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(35) : error C2086: 'int i' : redefinition
1&gt;        c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(35) : see declaration of 'i'
1&gt;c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(35) : error C2059: syntax error : ')'
1&gt;c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(36) : error C2143: syntax error : missing ';' before '{'
1&gt;c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(36) : error C2447: '{' : missing function header (old-style formal list?)
1&gt;c:\users\documents\visual studio 2008\projects\rastaschool\rastaschool\rastaschool.cpp(79) : fatal error C1004: unexpected end-of-file found
1&gt;Build log was saved at &quot;file://c:\Users\Documents\Visual Studio 2008\Projects\rastaschool\rastaschool\Debug\BuildLog.htm&quot;
1&gt;rastaschool - 21 error(s), 0 warning(s)</code>
zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
 

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.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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.

zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
 

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

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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.

zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
 

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

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

:( 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.

zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
 

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

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
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.

zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
 

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.

zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
 

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

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You