Hello everyone. I'm coming to you again because you all seem to be much more on the spot answering my questions than my professor.
The long story short, we are being introduced to structs in my c++ class, and for the first assignment we are to do a simple Student GPA struct and then print it out to the screen.
I will copy and paste the email I just sent my professor to save time, but what I wrote above should fill in any gaps from that email.
--------------------------------------------------
I am working on the GPA assignment. I am just doing a basic test run that will fill the struct with info, and then print. These are my two functions:
void testFill(struct std arg)
{
int counter = 0;
float total = 0;
arg.name = "Bob Bobson";
arg.idNumber = 123456;
arg.year = "Freshman";
for(int i = 0; i < 8; i++)
{
arg.gradepoint[i] = i+80;
arg.hours[i] = 3;
total = total+arg.gradepoint[i];
counter++;
}
arg.avg = total / counter;
}
void testPrint(struct std arg)
{
cout << "Name: " << arg.name << endl;
cout << "Id Number: " << arg.idNumber << endl;
cout << "Year: " << arg.year << endl;
for(int i = 0; i < 8; i++)
{
cout << "Grade: " << arg.gradepoint[i] << " ";
cout << "Hours: " << arg.hours[i] << endl;
}
cout << "Average: " << arg.avg << endl;
}
For some reason my fill function does not work, while my print function works fine. If I copy the code from the fill funtion and put it into the main, it works just dandy, and my print funtion using (student) as the arg does what it is suppose to do. If I leave the code in the fill function and just have testFill(student) in the main, it does not fill the array with the information, and the testPrint just prints out the gibberish that is in the struct.
I don't understand why my print function with params (arg) works and my fill function does not.
full code below:
#include
#include
#include
using namespace std;
struct std
{
string name;
int idNumber;
string year;
float gradepoint[8];
int hours[8];
float avg;
};
void testFill(struct std arg);
void testPrint(struct std arg);
int main(int argc, char *argv[])
{
struct std student;
testFill(student);
testPrint(student);
system("PAUSE");
return EXIT_SUCCESS;
}
void testPrint(struct std arg)
{
cout << "Name: " << arg.name << endl;
cout << "Id Number: " << arg.idNumber << endl;
cout << "Year: " << arg.year << endl;
for(int i = 0; i < 8; i++)
{
cout << "Grade: " << arg.gradepoint[i] << " ";
cout << "Hours: " << arg.hours[i] << endl;
}
cout << "Average: " << arg.avg << endl;
}
void testFill(struct std arg){
int counter = 0;
float total = 0;
arg.name = "Bob Bobson";
arg.idNumber = 123456;
arg.year = "Freshman";
for(int i = 0; i < 8; i++)
{
arg.gradepoint[i] = i+80;
arg.hours[i] = 3;
total = total+arg.gradepoint[i];
counter++;
}
arg.avg = total / counter;
}