1,105,229 Community Members

Functions Help

Member Avatar
jmay1327
Newbie Poster
9 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I am trying to use functions in the following program. The program is supposed to use a vector of structures to store a students first name, last name, and grades. Also, to get a average for each student and a class average. I do not think I am calling the function with the right parameters, but I am completely stuck. Any help would be greatly appreciated.

#include <iostream>
#include <string>
#include <vector>
#include <conio.h>
#include <algorithm>


using namespace std;

struct Student{
      
       string fName;
       string lName;
       vector<double>exams;
};

void display_Student(const Student& student);
double studentAverage(const Student& , double average);
           
int main(){
    int count;
    double average;
    const int NUM_EXAMS = 5;
    double examScore;
    int numStudents = 0;
    vector<Student> students;
       
       
       cout << "Number of students: " << endl;
       cin >> numStudents;
     for (int i = 0; i < numStudents; i++){
         Student student;
         cout << "Enter the first name for student #" << i+1
             << ": " << endl;
         cin >> student.fName;
                      
         cout << "the first name of the student is: " << student.fName << endl;
          
         cout << "Enter the last name for student #" << i+1
             << ": " << endl;
         cin >> student.lName;
         cout << "the first name of the student is: " << student.fName << endl; 
        
         for (int j = 0; j < NUM_EXAMS ; j++ ){
              cout << "Enter the exam scores student #" <<  i+1 << " exam #" << j + 1
                 << ": " << endl;;    
                cout << " Exam #" << j+1 << ": "; 
                cin >> examScore;
                student.exams.push_back(examScore); 
          }// end of nested for loop
          students.push_back(student);
    }//end of for loop
    
    
      // display the student
      for (int n = 0; n < students.size(); n++){
          display_Student(students[n]);
          }// end of the for loop
          
          studentAverage(students, average);

    _getch();
    return 0;
    
}//end of main

double studentAverage(const Student& students, double average){
       double total = 0;
       for(int i = 0; i < Student.exams[i]; i++){
             total += exams[i];
             }// end of the for loop
       average = total / 5.0;
       cout << "The average for " << student.fName << "is:" << average << endl;
       
}// end of the double studentAverage(const Student& , double average)function

void display_Student(const Student& student){
     cout << student.fName << " " << student.lName;
     for (int n = 0; n < student.exams.size(); n++){
         cout << " " << student.exams[n];
         cout << endl;
     }// end of the for loop     
}// end of the void display_Student(const Student& student) function
Member Avatar
gerard4143
Nearly a Posting Maven
2,385 posts since Jan 2008
Reputation Points: 371 [?]
Q&As Helped to Solve: 409 [?]
Skill Endorsements: 0 [?]
 
0
 

It would help if you indicated which function your having problems with.

Member Avatar
jmay1327
Newbie Poster
9 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

It would help if you indicated which function your having problems with.

Sorry, I am having trouble with the studentAverage function. Also, is it possible to create the student in a function, or is it better to have that in main.

Member Avatar
gerard4143
Nearly a Posting Maven
2,385 posts since Jan 2008
Reputation Points: 371 [?]
Q&As Helped to Solve: 409 [?]
Skill Endorsements: 0 [?]
 
0
 

I would try something like below

double studentAverage(const std::vector<struct Student> & vec, int student_index_postion)
{
  double average = 0.0;
  unsigned long count = 0;
  
  /*loop through exam scores*/
  
  return average/count;
}
Member Avatar
jmay1327
Newbie Poster
9 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I would try something like below

double studentAverage(const std::vector<struct Student> & vec, int student_index_postion)
{
  double average = 0.0;
  unsigned long count = 0;
  
  /*loop through exam scores*/
  
  return average/count;
}

this is what I got, but I'm still getting errors. Also, how would I call the function.

double studentAverage(const vector<struct Student> & vec ,int n){
       double average = 0.0;
       double total = 0.0;
       unsigned long count = 0;
       
       for(int i = 0; i < Student.exams[i]; i++){
             total += Student.exams[i];
             average = total / 5.0;
       }// end of the for loop
      
       
       for (count = 0; count < student; count++){
       cout << "The average for " << student.fName << "is:" << average << endl;
       }// end of the for loop
       return average/count;
       }// end of the for loop
Member Avatar
gerard4143
Nearly a Posting Maven
2,385 posts since Jan 2008
Reputation Points: 371 [?]
Q&As Helped to Solve: 409 [?]
Skill Endorsements: 0 [?]
 
0
 

I have to ask, are you taking the average of each student or are you taking the average of the group of students?

Member Avatar
jmay1327
Newbie Poster
9 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I have to ask, are you taking the average of each student or are you taking the average of the group of students?

Actually, I am trying to get both. A student average and a class average.

Member Avatar
gerard4143
Nearly a Posting Maven
2,385 posts since Jan 2008
Reputation Points: 371 [?]
Q&As Helped to Solve: 409 [?]
Skill Endorsements: 0 [?]
 
0
 

Well here's a gimme

double studentAverage(const std::vector<struct Student> & vec, int student_index_postion)
{
  double average = 0.0;
  unsigned long count = 0;
  
  for (unsigned long i = 0; i < vec[student_index_postion].exams.size(); ++i)
  {
	++count;
	average += vec[student_index_postion].exams[i];
  }
  
  return average/count;
}

This will calculate the average of the student at student_index_postion in the vector.

Member Avatar
jmay1327
Newbie Poster
9 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Well here's a gimme

double studentAverage(const std::vector<struct Student> & vec, int student_index_postion)
{
  double average = 0.0;
  unsigned long count = 0;
  
  for (unsigned long i = 0; i < vec[student_index_postion].exams.size(); ++i)
  {
	++count;
	average += vec[student_index_postion].exams[i];
  }
  
  return average/count;
}

This will calculate the average of the student at student_index_postion in the vector.

Thank you so much for all your help.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: