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;

}

Recommended Answers

All 5 Replies

Try passing your structure by reference....Wow you called your structure std? Don't do that.

#include <iostream>
#include <cstdlib>

using namespace std;

struct str/*don't call your structure std*/
{
     string name;
     int idNumber;
     string year;
     float gradepoint[8];
     int hours[8];
     float avg;
};

void testFill(struct str & arg);/*pass by reference*/
void testPrint(struct str arg);

int main(int argc, char *argv[])
{
	char pause;/*pause execution*/
	struct str student;

	testFill(student);
	testPrint(student);

	std::cin >> pause;
	return EXIT_SUCCESS;
}

void testPrint(struct str 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 str & 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;

}

Convert your fill function to use a reference parameter.

Currently, you're passing your student to testFill() by value. When you do this, an independent copy of the argument is made and placed in the parameter. Thus, when you modify the parameter, you are NOT modifying the argument.

This is what you currently have:

#include <iostream>

void setTen(int myParam) {
  myParam = 10;
}

int main() {
  int someValue = 0;
  std::cout << "Starting value: " << someValue << std::endl;
  setTen(someValue);
  std::cout << "Ending value: " << someValue << std::endl;
  return 0;
}

This is what you need:

void setTen(int &myParam) {
  myParam = 10;
}

int main() {
  int someValue = 0;
  std::cout << "Starting value: " << someValue << std::endl;
  setTen(someValue);
  std::cout << "Ending value: " << someValue << std::endl;
  return 0;
}

Notice the difference between the 2 versions of setTen()...

Thank you for your fast replies. I am heading out the door to the movie theater at the moment, but I will try out the suggestions as soon as I get back.

the std was the what the professor used in his example. He has a sick sense of humor, so I just decided to keep. I see what you mean about the connotations and it easily getting confused with "standard" too, so I will change it.

I see what you mean about the connotations and it easily getting confused with "standard" too, so I will change it.

The problem isn't that it will get confused with "standard". The problem is that it will conflict with the "std" namespace that all Standard C++ objects (such as cin and cout) are declared in.

Thank you so much, you guys are the saving grace I needed during this semester! Works like a charm.

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.