Need Help with my program

When i print out the info the user inputs it gives me garbage.
when i print out the name it comes out blank and the grades are bunch of random numbers as well as the average.
Maybe I'm calling the functions wrong.


#include<iostream>
#include<string>
using namespace std;

//Abstract data type
struct Student
{
string name;
int id;
double grades[4];
};

//Prototype
void studentInfo(Student);
double getAverage(Student &);
void printInfo(Student, double);

int main()
{

//Declare variables
Student person;
double average;
Student* personPtr = &person;
//Program introduction
cout << "Hello. This program is design for you to input student information" << endl;
cout << "You will have to enter the student name, id and four test grades." << endl;
cout << "It will drop the lowest grade and give you th average of the remaining grades" << endl;
cout << "At the end of the program it will display all the information you inputed." << endl;
cout << "Lets begin." << endl;
//Calling functions
studentInfo(person);
cout << "------------------------------------" << endl;
average = getAverage(person);
printInfo(*personPtr, average);
cout << "------------------------------------" << endl;

//--------------------------
system("pause");
return 0;
}

//Aquires the student infomation
void studentInfo(Student person)
{
cout << "Enter the students name:";
getline(cin, person.name);
cout << "Enter " << person.name << " ID number:";
cin >> person.id;
cin.ignore();
for (int i = 0; i < 4; i++)
{
cout << "Enter a grade for test #" << i + 1 << ": ";
cin >> person.grades[i];
}
}

//Gets the average of the top 3 grades
double getAverage(Student &person)
{
double average;
double lowestGrade = person.grades[0];
for (int i = 0; i < 4; i++)
{
if (lowestGrade > person.grades[i])
lowestGrade = person.grades[i];
}
average = ((person.grades[0] + person.grades[1] + person.grades[2] + person.grades[3]) - lowestGrade) / 3;
return average;
}

//Print all the info aquired
void printInfo(Student person, double average)
{
cout << "Name.............................................. " << person.name << endl;
cout << "ID number......................................... " << person.id << endl;
for (int i = 0; i < 4; i++)
{
cout << "Test grade #" << i + 1 << "....................................... " << person.grades << endl;
}
cout << "Average of the three highest grades............... " << average << endl;
}
Last edited on
http://www.cplusplus.com/doc/tutorial/functions/ (see «Arguments passed by value and by reference»)
studentInfo(person); is useless, it has no effect.
So then how do i call it?
I tried renaming it on the function, but it still doesn't print it out.
To expand on ne555's response,
 
void studentInfo(Student person)

You're passing person by value. Any changes made by that function are lost when that function exits because you're operating on a local copy of it. Not the caller's instance of it. You want to pass person by reference here so that the caller's instance of the struct is updated.
 
void studentInfo(Student & person)

Topic archived. No new replies allowed.