If i alter the program, by placing the counters after the braces...then it tells me that my if/else statements aren't ok.

I thought I said outside the while loop.

I've placed the counters after the loop...it pretty much prints four zeros, because that was the initial value being declared for them. How can I fix that?

That depends, did you print them before or after the while loop?

Post your latest code.

My latrest Code:
----------------------------------------------------------------------------------

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

using namespace std;




int main()
{

int stunum;//# of students in the class
string stuname;// Name of the student
int Test1;//score for test 1
int Test2;//score for test 2
int Midterm;//score for midterm
int Final;//score for final
int Term;//score for term project
int NumA = 0;//# of A's
int NumB = 0;//# of B's
int NumC = 0;//# of C's
int NumF = 0;//# of F's

ifstream inFile;
ofstream outFile;

inFile.open ("ClassData.dat");
outFile.open ("ClassOutput");
while (inFile>>stuname>>Test1>>Test2>>Midterm>>Final>>Term)//variables for student info

{
int gradeAvg= Test1 + Test2 + Midterm + Final + Term; 
if(gradeAvg>=90)
{
outFile<<stuname<<setw(5)<<gradeAvg<<setw(5)<<"A"<<endl;
}

else if (gradeAvg>=80)
{
outFile<<stuname<<setw(5)<<gradeAvg<<setw(5)<<"B"<<endl;
}

else if (gradeAvg>=70)
{
outFile<<stuname<<setw(5)<<gradeAvg<<setw(5)<<"C"<<endl;
}

else if (gradeAvg<70)
{
outFile<<stuname<<setw(5)<<gradeAvg<<setw(5)<<"F"<<endl;
}

}
outFile<<NumF++;
outFile<<NumC++;
outFile<<NumB++;
outFile<<NumA++;




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

return 0;

}

----------------------------------------------------------------------------------
This program prints four zeros. Also, if i wanted it to print the number of students in the class, I'd have to include '10' in my inFile correct?

Oh dear....

> NumF++;
Put these at appropriate places inside the if statements.

> cout << NumF;
Put these outside (and after) the while loop has finished.

:-O :$ ....thanks for the help....simple mistake...will be posting an array's question in a few days.

where in my inFile can I put '10', number of students to have the outFile say: There are 10 students in the class'. I tried placed it there, but it messed up my if/else statements.

My inFile is as follows:
10
Gayle 10 8 22 16 22
Brown 7 10 17 11 19
Byles 1 4 21 29 11 
Watts 1 2 18 10 17
Walsh 1 5 21 8 18
Ahmed 3 7 16 14 14
Ellis 8 9 20 30 23
Silny 10 10 25 28 21
Simms 8 3 22 24 25
Kenez 5 4 23 22 11
----------------------------------------------------------------------------------------------------------------------------
My source code is as follows:
#include<fstream>

#include<iomanip>

#include<string>

#include<iostream>

#include<cmath>

using namespace std;







int main()

{


int stunum;//# of students in the class

string stuname;// Name of the student

int Test1;//score for test 1

int Test2;//score for test 2

int Midterm;//score for midterm

int Final;//score for final

int Term;//score for term project

int NumA = 0;//# of A's

int NumB = 0;//# of B's

int NumC = 0;//# of C's

int NumF = 0;//# of F's


ifstream inFile;

ofstream outFile;

inFile.open ("ClassData.dat");

outFile.open ("ClassOutput");

while (inFile>>stunum>>stuname>>Test1>>Test2>>Midterm>>Final>>Term)//variables for student info

{

int gradeAvg= Test1 + Test2 + Midterm + Final + Term; 

if(gradeAvg>=90)

{

outFile<<stuname<<setw(5)<<gradeAvg<<setw(5)<<"A"<<endl;

NumA++;

}

else if (gradeAvg>=80)

{

outFile<<stuname<<setw(5)<<gradeAvg<<setw(5)<<"B"<<endl;

NumB++;

}

else if (gradeAvg>=70)

{

outFile<<stuname<<setw(5)<<gradeAvg<<setw(5)<<"C"<<endl;

NumC++;

}

else if (gradeAvg<70)

{

outFile<<stuname<<setw(5)<<gradeAvg<<setw(5)<<"F"<<endl;

NumF++;

}

}

outFile<<"A's="<<setw(2)<<NumA++<<endl;

outFile<<"B's="<<setw(2)<<NumB++<<endl;

outFile<<"C's="<<setw(2)<<NumC++<<endl;

outFile<<"F's="<<setw(2)<<NumF++<<endl;

outFile<<"The number of students in the class"<<setw(2)<<stunum<<endl;


inFile.close();

outFile.close();

return 0;

}

-------------------------------------------------------------------------------------------------------------------------------------------------

I can't see what's the problem....the output file comes out like this:

Gayle   78    C
A's= 0
B's= 0
C's= 1
F's= 0

-------------------------------------------------------------------------------------------------------------------------------------------------

However, when i remove the variable 'stunum' from my source code-it works perfectly. I presume it's to do with my inFile.

Thanks for your assistance.

#1: Please learn to format your code so we can read it.
#2: You added a line to the input file. Where did you add the read for that line?

> outFile<<"A's="<<setw(2)<<NumA++<<endl;
There's no need for the ++ on this line (think about it).

You are correct, just figured it out. Thx, but by any chance did you see why i am not able to read the number of students in the class from the inFile and place it into the outFile? Thanx.

the read for that new line I added to the inFile, while (inFile>>stunum>>stuname>>Test1>>Test2>>Midterm>>Final>>Term)-with 'stunum' being the variable used for the number of students.

with regards to the formatting, i tought i had placed my source-code in the Code Tags...

Member Avatar for iamthwee

with regards to the formatting, i tought i had placed my source-code in the Code Tags...

What he means is sensible formatting. Viz

#include <fstream>
#include <iomanip>
#include <string>
#include <iostream>
#include <cmath>
 
using namespace std;
 
int main()
{
  int stunum;
  string stuname;
  int Test1;
  int Test2;
  int Midterm;
  int Final;
  int Term;
  int NumA = 0;
  int NumB = 0;
  int NumC = 0;
  int NumF = 0; 
 
  ifstream inFile;
  ofstream outFile;
  inFile.open ( "ClassData.dat" );
  outFile.open ( "ClassOutput" );
 
  while ( inFile >> stunum >> stuname >> Test1 >> Test2 
                     >> Midterm >> Final >> Term )
  {
     int gradeAvg = Test1 + Test2 + Midterm + Final + Term;
     if ( gradeAvg >= 90 )
     {
        outFile << stuname << setw ( 5 ) << gradeAvg 
                  << setw ( 5 ) << "A" << endl;
        NumA++;
     }
     else if ( gradeAvg >= 80 )
     {
        outFile << stuname << setw ( 5 ) << gradeAvg 
                   << setw ( 5 ) << "B" << endl;
        NumB++;
     }
     else if ( gradeAvg >= 70 )
     {
        outFile << stuname << setw ( 5 ) << gradeAvg 
                  << setw ( 5 ) << "C" << endl;
        NumC++;
     }
     else if ( gradeAvg < 70 )
     {
        outFile << stuname << setw ( 5 ) << gradeAvg 
                  << setw ( 5 ) << "F" << endl;
        NumF++;
     }
  }
  outFile << "A's=" << setw ( 2 ) << NumA++ << endl;
  outFile << "B's=" << setw ( 2 ) << NumB++ << endl;
  outFile << "C's=" << setw ( 2 ) << NumC++ << endl;
  outFile << "F's=" << setw ( 2 ) << NumF++ << endl;
  outFile << "The number of students in the class" << setw ( 2 ) 
            << stunum << endl; 
 
  inFile.close();
  outFile.close();
  return 0;
}

Maybe

inFile >> stunum; // Only one of these, at the start
while (inFile>>stuname>>Test1>>Test2>>Midterm>>Final>>Term)

> i tought i had placed my source-code in the Code Tags
That's what "preview post" is for, to make sure you did it right.
It looks like the code tags where there, but your most recent post was devoid of any indentation. Picky I know, but coders like to see both in posted code, since it is the most efficient for us to read and figure out what is going on.

Member Avatar for iamthwee

Also he should use spaces instead of tabs to indent his code.

inFile >> stunum; // Only one of these, at the start
while (inFile>>stuname>>Test1>>Test2>>Midterm>>Final>>Term)

Tried it...it never read '10'(# of students)...

Works for me

#include <fstream>
#include <iomanip>
#include <string>
#include <iostream>
#include <cmath>
 
using namespace std;
 
int main()
{
  int stunum;
  string stuname;
  int Test1;
  int Test2;
  int Midterm;
  int Final;
  int Term;
  int NumA = 0;
  int NumB = 0;
  int NumC = 0;
  int NumF = 0; 
 
  ifstream inFile;
  ofstream outFile;
  inFile.open ( "ClassData.dat" );
  outFile.open ( "ClassOutput" );

  inFile >> stunum;
  while ( inFile >> stuname >> Test1 >> Test2 
                     >> Midterm >> Final >> Term )
  {
     int gradeAvg = Test1 + Test2 + Midterm + Final + Term;
     if ( gradeAvg >= 90 )
     {
        outFile << stuname << setw ( 5 ) << gradeAvg 
                  << setw ( 5 ) << "A" << endl;
        NumA++;
     }
     else if ( gradeAvg >= 80 )
     {
        outFile << stuname << setw ( 5 ) << gradeAvg 
                   << setw ( 5 ) << "B" << endl;
        NumB++;
     }
     else if ( gradeAvg >= 70 )
     {
        outFile << stuname << setw ( 5 ) << gradeAvg 
                  << setw ( 5 ) << "C" << endl;
        NumC++;
     }
     else if ( gradeAvg < 70 )
     {
        outFile << stuname << setw ( 5 ) << gradeAvg 
                  << setw ( 5 ) << "F" << endl;
        NumF++;
     }
  }
  outFile << "A's=" << setw ( 2 ) << NumA++ << endl;
  outFile << "B's=" << setw ( 2 ) << NumB++ << endl;
  outFile << "C's=" << setw ( 2 ) << NumC++ << endl;
  outFile << "F's=" << setw ( 2 ) << NumF++ << endl;
  outFile << "The number of students in the class" << setw ( 2 ) 
            << stunum << endl; 
 
  inFile.close();
  outFile.close();
  return 0;
}

$ cat ClassData.dat
10
Gayle 10 8 22 16 22
Brown 7 10 17 11 19
Byles 1 4 21 29 11
Watts 1 2 18 10 17
Walsh 1 5 21 8 18
Ahmed 3 7 16 14 14
Ellis 8 9 20 30 23
Silny 10 10 25 28 21
Simms 8 3 22 24 25
Kenez 5 4 23 22 11
$ ./a.exe
$ cat ClassOutput
Gayle   78    C
Brown   64    F
Byles   66    F
Watts   48    F
Walsh   53    F
Ahmed   54    F
Ellis   90    A
Silny   94    A
Simms   82    B
Kenez   65    F
A's= 2
B's= 1
C's= 1
F's= 6
The number of students in the class10

I forgot to do an outFile(output for stunum).....silly of me.....thanks for all the assistance....it's really appreciated.......will be posting an array question on monday and will be sure to have the formatting correct....thanks once again.

Out of curiosity...how could I design this same program using appropriate functions? Also....let us say that in addition I wanted to print out from the inFile, each of the student's individual test grade...why would it be a redundant code to have this after each if else statement? Also, how could i use a for loop to read the number of students from my inFile(which is on the first line of the inFile):

else if ( gradeAvg < 70 )
{
outFile << stuname << setw ( 5 ) << gradeAvg
<< setw ( 5 ) << "F" << endl;
NumF++;

If i put that each time for each student...why is it redundant.... Is there a way to have only one long-output statement and then print the letter grade separately? Thanks for the assistance...

Consider this

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

enum {
    Excellent,
    Good,
    Fair,
    Dubya,
};
 
int main()
{
  int stunum;
  string stuname;
  int Test1;
  int Test2;
  int Midterm;
  int Final;
  int Term;
  int stats[4] = { 0 };
  char grades[] = "ABCF";
  int gradeCode;
 
  ifstream inFile;
  ofstream outFile;
  inFile.open ( "ClassData.dat" );
  outFile.open ( "ClassOutput" );

  inFile >> stunum;
  while ( inFile >> stuname >> Test1 >> Test2 
                     >> Midterm >> Final >> Term )
  {
     int gradeAvg = Test1 + Test2 + Midterm + Final + Term;
     if ( gradeAvg >= 90 )
     {
        gradeCode = Excellent;
     }
     else if ( gradeAvg >= 80 )
     {
        gradeCode = Good;
     }
     else if ( gradeAvg >= 70 )
     {
        gradeCode = Fair;
     }
     else if ( gradeAvg < 70 )
     {
        gradeCode = Dubya;
     }

     // A common out for all students
     outFile << stuname << setw ( 5 ) << gradeAvg 
              << setw ( 5 ) << grades[gradeCode] << endl;
     stats[gradeCode]++;
  }

  for ( int i = 0 ; i < 4 ; i++ ) {
     outFile << grades[i] << "'s=" << setw ( 2 ) << stats[i] << endl;
  }
  outFile << "The number of students in the class" << setw ( 2 ) 
            << stunum << endl; 
 
  inFile.close();
  outFile.close();
  return 0;
}

Thanks for the assistance:

If I were to do the following, why does it only show the information for Kenez from my inFile?

10
Gayle 75 82 62 61 59
Brown 79 100 17 19 89
Byles 100 94 81 79 81
Watts 71 22 48 100 17
Walsh 41 85 71 88 81
Ahmed 93 47 76 64 94
Ellis 87 99 100 70 83
Silny 80 76 85 78 81
Simms 88 93 42 84 65
Kenez 75 48 77 69 82
-------------------------------------------------------------------------------

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

using namespace std;

int main()
{
	int stunum;//# of students in the class
	string stuname;// Name of the student
	float Test1;//score for test 1
	float Test2;//score for test 2
	float Midterm;//score for midterm
	float Final;//score for final
	float Term;//score for term project
	float Grade;
	float NumA=0;
	float NumB=0;
	float NumC=0;
	float NumF=0;

	ifstream inFile;
	ofstream outFile;

	inFile.open ("ClassData.dat");
	outFile.open ("ClassOutput");
	inFile>>stunum;//reads in the # of students in the class
	outFile<<"The # of students in the class:"<<stunum<<endl;
	for (int j=0; j<stunum; j++);
	{
	while (inFile>>stuname>>Test1>>Test2>>Midterm>>Final>>Term);
	}
	outFile<<setw(10)<<"Name"<<setw(10)<<"Test1"<<setw(10)<<"Test2"<<setw(10)<<"Midterm"<<setw(10)<<"Final"<<setw(12)<<"Project"<<setw(14)<<"Total Grade"<<setw(16)<<"Letter Grade"<<endl;
	Grade= Test1+Test2+Midterm+Final+Term;
	
	{
	
		{
			outFile<<setw(10)<<stuname<<setw(10)<<Test1<<setw(10)<<Test2<<setw(10)<<Midterm<<setw(10)<<Final<<setw(10)<<Term<<setw(10)<<Grade<<endl;
	
			{	
				float Grade= Test1+Test2+Midterm+Final+Term;
				if(Grade>=90)
				{
				outFile<<"A ";
				NumA++;
				}
				else if (Grade>=80)
				{
				outFile<<"B ";
				NumB++;
				}
				else if (Grade>=70)
				{
				outFile<<"C ";
				NumC++;
				}
				else if (Grade<70)
				{
				outFile<<"F ";
				NumF++;
				outFile<<endl;
				}
				inFile>>stuname>>Test1>>Test2>>Midterm>>Final>>Term;
				
			}

			outFile<<"The # of A's:"<<NumA<<endl;
			outFile<<"The # of B's:"<<NumB<<endl;
			outFile<<"The # of C's:"<<NumC<<endl;
			outFile<<"The # of F's:"<<NumF<<endl;
		}
		
	}


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

	return 0;
}

look at lines 31 and 33 -- delete that semicolon at the end of those two lines.

>>If I were to do the following, why does it only show the information for Kenez from my inFile?
Only displaying the last line of the file because of the problem with the semicolon on line 33.

You really start to be more careful about where you place those semicolons.

> If I were to do the following, why does it only show the information for Kenez from my inFile?

> for (int j=0; j<stunum; j++);
Watch the ; at the end, this means DO NOTHING a number of times.

> while (inFile>>stuname>>Test1>>Test2>>Midterm>>Final>>Term);
Watch the ; at the end, this means read the whole file, but DO NOTHING with any of the data.

The result being that what you end up with eventually, is just the last line of the file.

Thanx....however, after I remove the semi-colon, I receive an error message:

error C2143: syntax error : missing ';'before '}'

That's what i get after compiling the program(it shows up as an error for line 33).

Interesting....our professor didn't point out that was what the semicolons do......i owe youo guys big for that tip!!

Thanx....however, after I remove the semi-colon, I receive an error message:

error C2143: syntax error : missing ';'before '}'

That's what i get after compiling the program(it shows up as an error for line 33).

I agree that's the semicolon causing the problem, see below my new code.... and I have two problems:
Why does it only print the profile for "Kenez" from my inFile? Secondly, in my outFile, it only print's out data for "Kenez" and it places the "Letter Grade", for Kenez in a separate line? Below are my inFile & outFile.

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

using namespace std;

int main()
{
    int stunum;//# of students in the class
    string stuname;// Name of the student
    float Test1;//score for test 1
    float Test2;//score for test 2
    float Midterm;//score for midterm
    float Final;//score for final
    float Term;//score for term project
    float Grade;
    float NumA=0;
    float NumB=0;
    float NumC=0;
    float NumF=0;

    ifstream inFile;
    ofstream outFile;

    inFile.open ("ClassData.dat");
    outFile.open ("ClassOutput");
    inFile>>stunum;//reads in the # of students in the class
    outFile<<"The # of students in the class:"<<stunum<<endl;
    for (int j=0; j<stunum; j++)
    inFile>>stuname>>Test1>>Test2>>Midterm>>Final>>Term;

    Grade= Test1+Test2+Midterm+Final+Term;
    Grade=Grade/5;
    inFile>>stuname>>Test1>>Test2>>Midterm>>Final>>Term;
    {
    
        {    outFile<<setw(10)<<"Name"<<setw(10)<<"Test1"<<setw(10)<<"Test2"<<setw(10)<<"Midterm"<<setw(10)<<"Final"<<setw(12)<<"Project"<<setw(14)<<"Total Grade"<<setw(14)<<"Letter Grade"<<endl;
            outFile<<setw(10)<<stuname<<setw(10)<<Test1<<setw(10)<<Test2<<setw(10)<<Midterm<<setw(10)<<Final<<setw(10)<<Term<<setw(13)<<Grade<<endl;
    
            {    
                float Grade= Test1+Test2+Midterm+Final+Term;
                Grade=Grade/5;

                if(Grade>=90)
                {
                outFile<<"A ";
                NumA++;
                }
                else if (Grade>=80)
                {
                outFile<<"B ";
                NumB++;
                }
                else if (Grade>=70)
                {
                outFile<<"C ";
                NumC++;
                }
                else if (Grade<70)
                {
                outFile<<"F ";
                NumF++;
                }                
                inFile>>stuname>>Test1>>Test2>>Midterm>>Final>>Term;
                
            }

            outFile<<"The # of A's:"<<NumA<<endl;
            outFile<<"The # of B's:"<<NumB<<endl;
            outFile<<"The # of C's:"<<NumC<<endl;
            outFile<<"The # of F's:"<<NumF<<endl;
        }
        
    }


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

    return 0;
}

My inFile:
10
Gayle 86 88 92 96 78
Brown 47 40 57 29 49
Byles 61 49 28 52 71
Holst 71 82 78 70 69
Welsh 71 54 55 80 68
Hamms 93 77 86 74 84
Ellis 88 96 80 87 73
Spear 100 100 85 88 91
Wilks 48 53 62 74 75
Bolop 65 74 73 82 51

My outFile shows like tgis however:
The # of students in the class:10
Name Test1 Test2 Midterm Final Project Total Grade Letter Grade
Kenez 75 48 77 69 82 70.2
C The # of A's:0
The # of B's:0
The # of C's:1
The # of F's:0

Thanks for the assistance:

If I were to do the following, why does it only show the information for Kenez from my inFile?

for (int j=0; j<stunum; j++)
    while (inFile>>stuname>>Test1>>Test2>>Midterm>>Final>>Term);

That line keeps overwriting the values. In the end it is left with only the last values

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.