Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Print data function trouble

$
0
0
My print data function for some reason will output the first name, last name all grades and the avg for the first entry in a file correctly but will not print any other names or grades but will for some reason use the first person average for all averages. A sample output:

Enter filename: students.txt


Name Grades Average

John Smith 85 83 77 91 76 80 90 95 93 48 81.80
0 0 0 0 0 0 0 0 0 0 81.80
0 0 0 0 0 0 0 0 0 0 81.80
0 0 0 0 0 0 0 0 0 0 81.80
0 0 0 0 0 0 0 0 0 0 81.80
0 0 0 0 0 0 0 0 0 0 81.80
0 0 0 0 0 0 0 0 0 0 81.80
0 0 0 0 0 0 0 0 0 0 81.80
0 0 0 0 0 0 0 0 0 0 81.80
0 0 0 0 0 0 0 0 0 0 81.80





Ive thought maybe it has something to do with incrementing or maybe im reading from the file wrong but i think its my printData function on line 88 - 107



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


using namespace std;

const int NUM_GRADES=10; ////Total grades
const int NUM_STUDENTS=10; //Total Number of Students


void gData(string firstName[], string lastName[], int grades [][NUM_GRADES]); //Function to read from input file.
void CompAvg(string firstName[], string lastName[], int grades [][NUM_GRADES], float avgs []); //calculate average of each student.
void printData(string firstName[], string lastName[], int grades [][NUM_GRADES], float avgs []); //Function to display the result.

int main()
{
    //string names [NUM_STUDENTS];
    string firstName[NUM_STUDENTS], lastName[NUM_STUDENTS];
    
    int grades [NUM_STUDENTS][NUM_GRADES];
    float avgs [NUM_STUDENTS];
    
    
    //Calling functions
    gData(firstName, lastName,grades);
    CompAvg(firstName, lastName, grades, avgs);
    printData(firstName, lastName, grades, avgs);

    
    
    
    return 0;
}

void gData(string firstName [],string lastName [], int grades [][NUM_GRADES])//(string names, int grades)
{
    string filename;
    ifstream fin;
    cout<<"Enter filename: "; //User required to enter file name
    cin>>filename;
    fin.open (filename.c_str());
    while(!fin) {
      cerr<<"Error: Please enter a valid file name.\n";
      cout << "Enter filename: ";
      cin >> filename;
      fin.open (filename.c_str());
    }
    
    
    
    
    for (int i=0;i <= NUM_STUDENTS; ++i)
    {
      fin >> firstName[i] >> lastName[i];
      
      for (int j=0;j<=NUM_GRADES; ++j)
      {
        fin >> grades [i][j];
        
      }
    }
    fin.close();
}

  //void CompAvg(string firstName [], string lastName, int grades [][NUM_GRADES], float avgs []);
void CompAvg(string firstName [], string lastName [], int grades [][NUM_GRADES], float avgs [])//(int grades, float average)
{
    float total = 0; //To hold an average temporarily
    ifstream fin;
    for(int i=0;i<NUM_GRADES; ++i)
    {
        fin >> firstName[i] >> lastName[i];
        for(int j=0;j<NUM_GRADES; ++j)
        {
            fin >> grades [i][j];
            total += grades[i][j]; //Adding up all the grades
        }

    avgs [i] = total /NUM_GRADES;
    }
}




void printData(string firstName[], string lastName[], int grades[][NUM_GRADES], float avgs[])
{
    // output table
    cout << "\n\n Name\t\t\t" << "Grades\t\t\t\t\t" << "Average\n\n";
    
    for(int i=0;i<NUM_STUDENTS;++i)
    {
        cout<<firstName[i] << " " << lastName[i];//Display name
       
        for(int j=0;j<NUM_GRADES;++j)
        {
            cout<< " " << grades[i][j] << " "; //Display 10 grades
            
        }
                cout<<fixed<<setprecision(2)<<"\t" << avgs[i] << endl;
     
     // <<firstName[i] << " " << lastName[i] <<"\n"; //Display Grades
    }
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>