0

I had placed the wrong inFile:

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
------------------------------------------------------
My instructor said i could only use a FOR loop

0

I did a bit of fixing, still having the problem of the outFile only having info for Kennez. Any ideas welcomed...thanks and have a good day.

0

You sure do have a talent for going backwards.

All you had to do to my program from 2 weeks ago was change

while ( inFile >> stuname >> Test1 >> Test2 
                 >> Midterm >> Final >> Term )
  {
      /* the code in the loop */
  }

Into

for (int j=0; j<stunum; j++) {
      inFile >> stuname >> Test1 >> Test2 
             >> Midterm >> Final >> Term;
      /* the code in the loop */
  }
0
#include <iostream>
#include <fstream>
#include <string>
using namespace std;


int main () {
string Surname;
int Test1, Test2, MidTerm, Finals, Average,Term;
int c=0;;
ifstream myfile ("e:\example.txt");
ofstream fout ("e:\output.txt");
if (myfile.is_open())
{
while (!myfile.eof())
{
myfile>>Surname>>Test1>>Test2>>MidTerm>>Finals>>Term;
c++;
Average=Test1+Test2+MidTerm+Finals+Term;


if (Average>=90) fout<<c<<"  "<<Surname<<"   "<<Average<<"   A"<<endl;


if (Average>=80 && Average <90) fout<<c<<"  "<<Surname<<"   "<<Average<<"   B"<<endl;


if (Average>=70 && Average<80) fout<<c<<"  "<<Surname<<"   "<<Average<<"   C"<<endl;


if (Average<70) fout<<c<<"  "<<Surname<<"  "<<Average<<"   F"<<endl;



}
myfile.close();
fout.close();
}


else cout << "Unable to open file";


return 0;
}

// this should work

Edited by happygeek: fixed formatting

0
/*If you don't use {} after a control statement (for, while,
  do/while, if, etc) then the body of the statement will be
  just the first line after the statement. To have more than
  one line in the body after the statement enclose all the 
  lines you want in the body in a set of {}s
*/

for (int j=0; j<stunum; j++)

//place a { here

    inFile>>stuname>>Test1>>Test2>>Midterm>>Final>>Term;

    Grade= Test1+Test2+Midterm+Final+Term;
    Grade=Grade/5;

    //this line should be erased as you don't want another
    //student's data read in yet, you just gone one above
    inFile>>stuname>>Test1>>Test2>>Midterm>>Final>>Term;
    {
       //either the { above or below this line shouldn't 
       //be here with the code as is.  Actually, neither 
       //should be here if  you take out the line just above 
       //the above {.

        {    
            //this outfile stuff looks like the header to a  
            //file so move it outside the loop used to read in 
            //the student records as you only want it once
            //for the entire file, not once for each 
            //student in the file, presumably.

            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;
    
            //here's another unneeded {
            {   
                
                //the next two lines are unnecessary
                //since you already did the calculation 
                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++;
                }  

                //You really like this line don't you!
                //Erase this one too!!              
                inFile>>stuname>>Test1>>Test2>>Midterm>>Final>>Term;

            //Now you'll have to realign your code and remove all 
            //the extra }s once you get rid of all the unnecessary
            //{s.  
            }
            
            //all of this should be outside the loop to determine
            //the number o f A, B, Cs, etc, assuming you only want
            //to print the answer once per reading of the file
            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;
        }
0

Ptarila....thx for the help-but i have to use a FOR loop, i'm currently making modifications to my code and will see if i get it to work. thx a lot

0

I need a second pair of eyes please:

my 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
	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;
	float Avg1;
	float Avg2;
	float Avg3;
	float Avg4;

	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;
			
			
			Avg1=((Test1+Test2)/2*(.2));//Calculate average test score
			Avg2=(Midterm*(.25));//Calculate average midterm score
			Avg3=(Final*(.3));//calculate the final
			Avg4=(Term*(.25));
			Grade= Avg1+Avg2+Avg3+Avg4;

			{

				if(Grade>=90)
				{
				outFile<<setw(15)<<" A ";
				NumA++;
				}
				else if (Grade>=80)
				{
				outFile<<setw(15)<<" B ";
				NumB++;
				}
				else if (Grade>=70)
				{
				outFile<<setw(15)<<" C ";
				NumC++;
				}
				else if (Grade<70)
				{
				outFile<<setw(15)<<" F ";
				NumF++;
				}
			}
			
			outFile<<setw(10)<<"Name"<<setw(10)<<"Test1"<<setw(10)<<"Test2"<<setw(10)<<"Midterm"<<setw(10)<<"Final"<<setw(14)<<"Project"<<setw(14)<<"Total Grade"<<setw(14)<<"Letter Grade"<<endl;
			outFile<<"********************************************************************************************"<<endl;
			outFile<<setw(10)<<stuname<<setw(10)<<Test1<<setw(10)<<Test2<<setw(10)<<Midterm<<setw(10)<<Final<<setw(14)<<Term<<setw(14)<<Grade;
			}
			

				
				
			
			outFile<<endl<<endl;
			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 outFile:
The # of students in the class:10
F Name Test1 Test2 Midterm Final Project Total Grade Letter Grade
********************************************************************************************
Gayle 75 82 62 61 59 64.25 F Name Test1 Test2 Midterm Final Project Total Grade Letter Grade
********************************************************************************************
Brown 79 100 17 19 89 50.1 B Name Test1 Test2 Midterm Final Project Total Grade Letter Grade
********************************************************************************************
Byles 100 94 81 79 81 83.6 F Name Test1 Test2 Midterm Final Project Total Grade Letter Grade
********************************************************************************************
Watts 71 22 48 100 17 55.55 C Name Test1 Test2 Midterm Final Project Total Grade Letter Grade
********************************************************************************************
Walsh 41 85 71 88 81 77 C Name Test1 Test2 Midterm Final Project Total Grade Letter Grade
********************************************************************************************
Ahmed 93 47 76 64 94 75.7 B Name Test1 Test2 Midterm Final Project Total Grade Letter Grade
********************************************************************************************
Ellis 87 99 100 70 83 85.35 B Name Test1 Test2 Midterm Final Project Total Grade Letter Grade
********************************************************************************************
Silny 80 76 85 78 81 80.5 C Name Test1 Test2 Midterm Final Project Total Grade Letter Grade
********************************************************************************************
Simms 88 93 42 84 65 70.05 C Name Test1 Test2 Midterm Final Project Total Grade Letter Grade
********************************************************************************************
Kenez 75 48 77 69 82 72.75

The # of A's: 0
The # of B's: 3
The # of C's: 4
The # of F's: 3


Thks for all the help...

0

never mind- i got it figured out..thx a lot, i didn't see it until now. I see now that its logical it is because each time the program goes through the loop it's gonna print out the headers because it's part of the loop....thx for the tip...now i have a question not related to C++....can you delete a post after it's solved? Also, how long are threads/post stayed online? If you don't log into your account every so often, will it expire after a while? Thnx.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.