problem with program

This is a discussion on problem with program within the C++ Programming forums, part of the General Programming Boards category; I program has a problem when reading from the file given: robert arnold 85 hiedi turberville 90 sally hall 55 ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    76

    problem with program

    I program has a problem when reading from the file given:

    robert arnold 85
    hiedi turberville 90
    sally hall 55

    Program output is:

    robert arnold 85
    hiedi turberville 90
    sally hall 55
    3998044


    The class average is: 999568
    robert arnold 85 /Below class average/
    hiedi turberville 90 /Below class average/
    sally hall 55 /Below class average/
    3998044 /Above class average/


    The highest grade of the class belongs to: 3998044



    As you can see the average is not computing right.

    Here is the code:
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    // declare function prototypes
    void infile_class_average(string firstName[], 
    string lastName[], int grade[], int& averageGrade);
    
    void below_class_average(string firstName[],
     string lastName[],int grade[], int& averageGrade);
    void highest_score(string firstName[],
     string lastName[],int grade[], int& averageGrade); 
     
    ///////////////////// main function ////////////////////////////////////
    int main(int argc, char *argv[])
    {
        // local variables //
        int grade[50], averageGrade;
        string firstName[50], lastName[50];
           
    infile_class_average(firstName, lastName, grade, averageGrade);    
    below_class_average(firstName,lastName, grade, averageGrade);   
    highest_score(firstName, lastName, grade, averageGrade);  
       
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    ////////////////// function definition #1  infile_class_average //////////////////////////////////////
    // get file input and calculate class average
    void infile_class_average(string firstName[]
    , string lastName[], int grade[], int& averageGrade)
    {
        ifstream infile;
        ofstream outfile;
        
        infile.open("C:\\testScores.txt");
        outfile.open("testScoresOut.txt");
        
             // intitialize  integers to zero
    int count = 0, totalGrades = 0;
    int numOfStudents = 0; 
    while(!infile.eof() && numOfStudents < 50)
    {  
    //get student input  first name , last name, and student grade
    numOfStudents++; 
    infile >> firstName[numOfStudents] >> lastName[numOfStudents] >> grade[numOfStudents];
    outfile << firstName[numOfStudents] << " " << lastName[numOfStudents] << " " << grade[numOfStudents] << endl;
    cout << firstName[numOfStudents] << " " << lastName[numOfStudents] << " " << grade[numOfStudents] << endl;
    
    // find the class average.
    count = count + 1;
    totalGrades = totalGrades + grade[numOfStudents];
    
    
    ///////////////////////////////////
    }
    averageGrade = totalGrades / count;
    
    cout << "\n\nThe class average is: " << averageGrade << endl;
     infile.close();
    }
    ////////////////// function definition #2  below class average ////////////////////////////
    // output the number of students that has a grade below the class average.
    void below_class_average(string firstName[], string lastName[],
     int grade[], int& averageGrade)
     {
        ifstream infile;
        ofstream outfile;
        
        infile.open("c:\\testScores.txt");
        outfile.open("testScoresOut.txt");
        
      
         int numOfStudents = 0;
    do    
    {  
    infile >> firstName[numOfStudents] >> lastName[numOfStudents] >> grade[numOfStudents];
    numOfStudents++; 
    
    if(averageGrade > grade[numOfStudents])
     cout << firstName[numOfStudents] << " " << lastName[numOfStudents] << " " << grade[numOfStudents] << " /Below class average/" << endl;
    if(averageGrade < grade[numOfStudents])
     cout << firstName[numOfStudents] << " " << lastName[numOfStudents] << " " << grade[numOfStudents] << " /Above class average/" << endl;
    } while(!infile.eof() && numOfStudents < 50);
    infile.close();
    }
    ////// function definition #3 highest score /////////////////////
    void highest_score(string firstName[],
     string lastName[],int grade[], int& averageGrade)
     {
        ifstream infile;
        ofstream outfile;
        
        infile.open("c:\\testScores.txt");
        outfile.open("testScoresOut.txt");
        
      int largest = 0;
      int numOfStudents = 0;
    do    
    {  
         
    infile >> firstName[numOfStudents] >> lastName[numOfStudents] >> grade[numOfStudents];
    numOfStudents++; 
    
    if(largest < grade[numOfStudents])
    largest =  grade[numOfStudents];
    
     
    } while(!infile.eof() && numOfStudents < 50);
    
    cout << "\n\nThe highest grade of the class belongs to: " << firstName[numOfStudents]
         << " " <<  lastName[numOfStudents] << "  " << largest << endl;
    infile.close();     
    }

    whst I believe is happening is in the first function class average it is looping one too many times.

    if I initialize numOfStudents to 1 it will work but you get the wrong answer of course.

    any suggestions?

  2. #2
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    Without looking too much into that hideously indented code, I imagine that numOfStudents is becoming one too large - for the output at least, maybe elsewhere as well. The extra output is whatever is left in memory one past the location where your array ends.

    If I define an array to be of size 3, then access array[3] (one past what's been declared), I'll either get a segmentation fault or access whatever is still written to the spot in memory array[3] would use, were it actually part of the array. Make sure you never go 'out of bounds' with your arrays.
    There is a difference between tedious and difficult.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    >> while(!infile.eof(

    Don't use eof() to control your loop. Instead, use a while loop and use while (infile >> firstName[numOfStudents]) to control it so that it will not run the loop if it fails to read in the next entry.

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    I appreciate your honesty decrypt when you say hideous. As you can tell, I am a newbie taking my first programmming course in college so any advice you could give to make my code more readable would be very helpfull. Also thanks to both of you for your responses.

  5. #5
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    This article can explain far better than I ever could.

    http://en.wikipedia.org/wiki/Indent_style

    As a side note, try to use spaces instead of tabs, since the tab character can be set differently on other computers, and can make things messy. Your text editor or IDE may have a option to convert tabs to spaces to make it more convenient.

    Good luck with programming!
    There is a difference between tedious and difficult.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    I still can not get this to stop going out of bounds.

    while(infile >> firstName[numOfStudents]) does not work either.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 10:10 AM
  2. Program Termination Problem
    By dacbo in forum C Programming
    Replies: 3
    Last Post: 01-23-2006, 01:34 AM
  3. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  4. Replies: 20
    Last Post: 06-12-2005, 11:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21