Help with void function output

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Deathtotock
    New Member
    • Nov 2008
    • 1

    Help with void function output

    I am inputing from a file and I am outputting to a file. well when i run the program it just puts the headers up there and no names or grades. This is the input.
    Code:
    Balto		        85	83	77	91	76
    Mickey	  	80	90	95	93	48
    Minnie		78	81	11	90	73
    Doc		        92	83	30	69	87
    Goofy		23	45	96	38	59
    Duckey		60	85	45	39	67
    Grumpy		27	31	52	74	83
    Sunny		93	94	89	77	97
    Piggy		79	85	28	93	82
    Pluto		        85	72	49	75	63
    However, this is the output im getting:

    Code:
    Student    Test1     Test2   Test3   Test4   Test5   Average Grade
    And thats it.

    Here is my code:

    Code:
     #include <iostream>
     #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);
    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("inGrade.txt");
    outFile.open("FinalGrades.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;
    
    }

    Any help would be greatly appreciated. Thanks.
    Last edited by Ganon11; Nov 11 '08, 10:53 PM. Reason: Please use [CODE] tags in the future.
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    I'm not sure. The code works fine for me and writes all the data to the file. Maybe the program is not able to open the inGrade.txt file. How about putting a system("PAUSE") before the return so you can check:
    Code:
    if (!inFile)
    {
        cout << "Unable to open the file." <<endl;
        system("PAUSE");
        return 1;
    }
    I hope this helps solve the problem.

    Comment

    • Savage
      Recognized Expert Top Contributor
      • Feb 2007
      • 1759

      #3
      Code:
      if(!inFile)
      That's not the way to check if the file is open.This call is same as calling ifstream::fail( ) which checks if there was a fail bit(generally some file access error) or bad bit(checks to see if stream has lost it's integrity).To check if the file is open use:

      Code:
      ifstream::is_open()
      
      if(inFile.is_open==false)
      {
      ....
      }

      Comment

      Working...