So, my assignment is to take in a student ID, first and last name, and 5 test scores.

I am then to average the test scores, collect all of that data into an array and output all of the information in a tabular format(which I have yet to do).

I have much of the bulk programming done, but I'm struggling with two specific things. Averaging the data for ALL students and pulling the data out of an array to print it on screen.

Here's my code so far:

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

using namespace std;

const int SCORES = 5;
const int MAX_SIZE = 25;

struct Student
       {
         int studentId;
         string firstName;
         string lastName;
         int testScores[SCORES];
       };
         
bool getStudent(Student& stu);
void printData(Student list[], int count);
double calcAverage(Student& stu);

int main()
{
    Student stu;
    Student list[MAX_SIZE];
    int index = 0;
    
    calcAverage(stu);
    
    bool done = false;
    while(!done && index <= MAX_SIZE)
    {
                done = getStudent(stu);
                if(!done)
                {
                         list[index] = stu;
                         index++;
                }
                 
    }
    
    system("cls");

    printData(list, index);

    system("pause");
    return EXIT_SUCCESS;
}

bool getStudent(Student& stu)
{
     cout << "=======================New Student=======================" << endl;
     cout << "!!When finished adding data use CNTRL-Z to clear & print!!\n" << endl;
     cout << "Enter student ID: ";
     cin >> stu.studentId;
     
     if(cin.eof() || cin.fail())
        return true;
        cin.ignore();
        
     cout << "Enter student's first name: ";
     getline(cin, stu.firstName);
     cout << "Enter student's last name: ";
     getline(cin, stu.lastName);
     cout << "Please enter 5 test scores (seperate with spaces): ";

     for(int i = 0; i < SCORES; i++)
           cin >> stu.testScores[i];
     return false;
}

double calcAverage(Student& stu)
{  
     //avg = (stu.testScores[0] + stu.testScores[1] + stu.testScores[2] 
     //+ stu.testScores[3] + stu.testScores[4]) / 5.0;
}

void printData(Student list[], int count)
{
     for(int i = 0; i < count; i++)
     {
         cout << list[i].studentId << " ";
         cout << list[i].firstName << " ";
         cout << list[i].lastName << " ";
         cout << list[i].testScores << " ";
         //cout << list[i].avg << " ";
         }
}

Notice how I commented out the entire body of the calcAverage function; this is because it only calculates the average of 5 test scores of ONE student, not ALL students individually. The next problem I have lies within "cout << list.testScores << " ";" How would I print this data? Would I just form a for loop to output everything inside testScores[]?

I'm pretty sure the answers will be simple and I am just having a brain block atm.

Thanks so much!

SgtMe commented: Made effort when asking for help :D First person today +1

1st problem u try to call

double calcAverage(Student& stu)

before assigning the value for student

2nd problem
you have to use loop to pass for each and every student some thing like this

for(i=0;i<total_number_student;i++)
{
avg[i]=calcAverage(list[i])//it ll store the average of every student in a array avg
}

ya u have to use nested for loop in side display function to print the data

I hope it will help you
Best Of Luck

Firstly, you need to check your loop in the main function. Look where you write CalcAverage(). This needs to be inside a loop, so that you can run it for all students. Currently, it's just doing the calculations for one.
Secondly, yes you would could use a loop like that to print the data. In this situation, I would use variable i and increment each loop to print out the test scores. This would be best, seeing as it would support variable amounts of pupils.
Also, I would like to thank you on the fact that you have put in a great amount of effort before calling for help. You don't want to know how many times people have just demanded help :|

If you need anything else just ask :)

Whoops! Sorry prvnkmr449! Didn't notice you were writing a reply as well. Ah well :)
Also: even if it does count as a simple error, it's unavoidable. It's the same reason why people will get someone else to proof read a letter.

@SgtMe

Just i suggest that it is not at write place, u have to call this after assigning the value thats all

I am not mention any where that put it out side the loop, who say u have to call function out side the loop, I never say this

Thanks for all the help! I'll get working on it ASAP.

Also, I like to try what I can before asking because if all that I did was demand answers without trying then I would never learn anything!

1st problem u try to call

double calcAverage(Student& stu)

before assigning the value for student

2nd problem
you have to use loop to pass for each and every student some thing like this

for(i=0;i<total_number_student;i++)
{
avg[i]=calcAverage(list[i])//it ll store the average of every student in a array avg
}

ya u have to use nested for loop in side display function to print the data

I hope it will help you
Best Of Luck

Okay, could you tell me exactly where to implement these?

I tried this:

void printData(Student list[], int count)
{
     for(int i = 0; i < count; i++)
     {
         cout << list[i].studentId << " ";
         cout << list[i].firstName << " ";
         cout << list[i].lastName << " ";
         
         for(int index = 0; index < MAX_SIZE; index++)
         {
             cout << list[i].testScores[index];
             }
         //cout << list[i].avg << " ";
         }

}

For the display function, but it just spits out a plethora of random numbers in place of where the scores should be. Is my logic wrong? I'm sorry for being so helpless, very tired after work so I'm not at 100% thinking capacity atm. :P

EDIT: Okay, I found that I was setting the parameters to index < MAX_SIZE(25) rather than the number of test scores.

Now all I need to know is how to fix this pesky calcAverage function properly!

EDIT: Solved the setw problem, I'm stupid lol. Refer to above post please!

OKAY! Sorry to quad-post, but I need some more help.

1.) I am unsure how to print the averages along with the rest of the info.

2.) I am unsure how to EXACTLY format the info into an organized table using setw

3.) The program crashes if more than one student information is entered.

Any thoughts?

Thanks in advance for the help!

CODE:

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

using namespace std;

const int SCORES = 5;
const int MAX_SIZE = 25;

struct Student
       {
         int studentId;
         string firstName;
         string lastName;
         int testScores[SCORES];
       };
         
bool getStudent(Student& stu);
void printData(Student list[], int count);
double calcAverage(Student& stu);

int main()
{
    Student stu;
    Student list[MAX_SIZE];
    int index = 0;
    double avg[] = {0.0};
    
    bool done = false;
    while(!done && index <= MAX_SIZE)
    {
                done = getStudent(stu);
                if(!done)
                {
                         list[index] = stu;
                         index++;
                }
                 
    }
    

    calcAverage(stu);
    
    for(int i = 0; i < index; i++)
            avg[i] = calcAverage(list[i]);
     
    system("cls");

    printData(list, index);

    system("pause");
    return EXIT_SUCCESS;
}

bool getStudent(Student& stu)
{
     cout << "=======================New Student=======================" << endl;
     cout << "!!When finished adding data use CNTRL-Z to clear & print!!\n" << endl;
     cout << "Enter student ID: ";
     cin >> stu.studentId;
     
     if(cin.eof() || cin.fail())
        return true;
        cin.ignore();
        
     cout << "Enter student's first name: ";
     getline(cin, stu.firstName);
     cout << "Enter student's last name: ";
     getline(cin, stu.lastName);
     cout << "Please enter 5 test scores (seperate with spaces): ";

     for(int i = 0; i < SCORES; i++)
           cin >> stu.testScores[i];
     return false;
}

double calcAverage(Student& stu)
{
     double avg;
     avg = (stu.testScores[0] + stu.testScores[1] + stu.testScores[2]
     + stu.testScores[3] + stu.testScores[4]) / 5.0;
     return avg;
}

void printData(Student list[], int count)
{
      cout << "ID"
         << setw(7) << setfill(' ') << "Name"
         << setw(15) << setfill(' ') << "Scores"
         << setw(20) << setfill(' ') << "Avg"
         << "\n" << endl;
         
     for(int i = 0; i < count; i++)
     {
         cout << list[i].studentId << " ";
         cout << list[i].lastName << ", ";
         cout << list[i].firstName << " ";
         
         for(int index = 0; index < 5; index++)
         {
             cout << list[i].testScores[index] << " ";
             }
         cout << "\n";
         }

}

OK...I'll have a quick look.

1. If you want to print all the data:
eg.
cout<<"Student: "<<name<<"Class: "<<class<<"Average Score: "<<avg_score<<endl;

2. If you want to make it into a table, you could just use whitespace, or you can use ASCII to draw up a table, like using the (-), (_), (=), (|) and (¦) characters.

3. Not sure about this one. My recent experiences are that my programs crash if I try to use a pointer to an array ID that doesn't exist, ie:

int bob[3]
bob[3] = 234
//True IDs for bob array are: 0, 1, 2

Make sure that you have enough room in the array to enter the student data. Had a quick look over your code and think this may be the problem, but I could only have quck look.

Hope this helps :)

@Syrne
Ur program is not crash after getting information of first student in visual stdio 2005 which compiler are u using?
you have to pass avg[] array as well to print average along with all info so change line 19 and 85 to

void printData(Student list[], int count,double av[]);

and line no 103 to
cout<<"\t"<<av<<endl;

for formate u try by ur self little bit more because formatting is not a great concern

Hope it will help full.

OKAY! Back again lol. (Been working A TON lately)

Anyways, I've figured out how to calculate the averages properly for each student (with your help of course) now all that is left is to:

Print the averages. (I saw what you guys wrote, but it doesn't seem to be working properly)


CODE:

/*=====================================================================
  ========================DPR 226 Bill Gagliardi=======================
  ===================Assignment #2 Using Structures====================
  =====================================================================*/
  
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

// Declaring constant variables for student arrays
const int SCORES = 5;
const int MAX_SIZE = 25;

// Forming a structure to encapsulate all student data
struct Student
       {
         int studentId;
         string firstName;
         string lastName;
         int testScores[SCORES];
       };
       
//Declaration of function prototypes
bool getStudent(Student& stu);
void printData(Student list[], int count, double avg[]);
double calcAverage(Student& stu);

int main()
{
    Student stu;
    Student list[MAX_SIZE];
    int index = 0;
    double avg[SCORES] = {0.0};
    
// Initiate main loop to step through each student
    bool done = false;
    while(!done && index <= MAX_SIZE)
    {
                done = getStudent(stu);
                if(!done)
                {
                         list[index] = stu;
                         index++;
                }
                 
    }
    
    
    for(int i = 0; i < index; i++)
            avg[i] = calcAverage(list[i]);
     
    system("cls");

    printData(list, index, avg);

    system("pause");
    return EXIT_SUCCESS;
}

// Function to ask for student data and, if no more data is necessary, reverts
// to end of program.
bool getStudent(Student& stu)
{
     cout << "=======================New Student=======================" << endl;
     cout << "!!When finished adding data use CNTRL-Z to clear & print!!\n" << endl;
     cout << "Enter student ID: ";
     cin >> stu.studentId;
     
     if(cin.eof() || cin.fail())
        return true;
        cin.ignore();
        
     cout << "Enter student's first name: ";
     getline(cin, stu.firstName);
     cout << "Enter student's last name: ";
     getline(cin, stu.lastName);
     cout << "Please enter 5 test scores (seperate with spaces): ";

     for(int i = 0; i < SCORES; i++)
           cin >> stu.testScores[i];
     return false;
}

// Function to calculate the average between 5 test scores of each student
double calcAverage(Student& stu)
{
     double avg;
     avg = (stu.testScores[0] + stu.testScores[1] + stu.testScores[2]
     + stu.testScores[3] + stu.testScores[4]) / 5.0;
     return avg;
}

// Function to print all student data on screen in a tabular format after user
// finishes inputting data. 
void printData(Student list[], int count, double avg[])
{
      cout << "ID"
         << setw(9) << "Name"
         << setw(22) << "Scores"
         << setw(20) << "Avg"
         << "\n" << endl;
         
     for(int i = 0; i < count; i++)
     {
         cout << list[i].studentId << " ";
         cout << setw(4);
         cout << list[i].lastName << ", ";
         cout << list[i].firstName << " ";
         
         cout << setw(8);
         
         for(int index = 0; index < 5; index++)
         {
             cout << list[i].testScores[index] << " ";
             }
             
             cout << avg[i];

         cout << "\n";
         }

}

This line:

cout << avg[i];

Does not work. When I compile, it runs, but nothing is displayed after the test scores. Any thoughts?

Thanks for all the help! Almost done! :P

Change

const int SCORES = 5;

to

const int SCORES = 25;

Change

const int SCORES = 5;

to

const int SCORES = 25;

I'm sorry, but I don't quite understand what that would be fixing. Could you elaborate further? I only want the user to input a max of 5 test scores.

Thanks for the help!

@Syrne
(I lost my account password of prvnkmr449)
u using same SCORES at line no 22 inside structure for storing test score

int testScores[SCORES];

it create array of size, no problem up to here but what happen at line no 35

double avg[SCORES] = {0.0};

it also create array of size 5
now look at line no 51 and 52

for(int i = 0; i < index; i++)
            avg[i] = calcAverage(list[i]);

here ur loop is start from zero (0) to less than index, maximum value of index =25 and size of ur avg[] array is 5 acceding the outer bound of array.
you have to create avg array of size 25.

I hope you understand.
Best Of Luck

@Syrne
(I lost my account password of prvnkmr449)
u using same SCORES at line no 22 inside structure for storing test score

int testScores[SCORES];

it create array of size, no problem up to here but what happen at line no 35

double avg[SCORES] = {0.0};

it also create array of size 5
now look at line no 51 and 52

for(int i = 0; i < index; i++)
            avg[i] = calcAverage(list[i]);

here ur loop is start from zero (0) to less than index, maximum value of index =25 and size of ur avg[] array is 5 acceding the outer bound of array.
you have to create avg array of size 25.

I hope you understand.
Best Of Luck

Perfect!

Now I understand! It's working fine now! All I need to do is set up the tabular format and I'm good to go! Thanks a lot for all the help!

With the tables are you just going to use whitespace? Just edit the string output of your average function or however its done. Ask for help if you need :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.