structs...

Okay, i want to start off by saying this is for a class of mine but i have messed up and waited to the last minute to finish it and really got myself into a mess. I have taken C++ before but never had to use structs. My program worked fine until i tried to move all the input into a seperate void funtion and now the output isnt correct. If anyone can tell me where i have an error in passing the information into the struct i would greatly appreciate it!

So here is the code:

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

struct Student
{
string name;
string address;
string city;
string state;
string id;
float gpa;
}; // Student

void enterRecords(Student a)
{
cout << "Enter student's name: ";
cin >> a.name;
cout << "\nEnter student's address: ";
cin >> a.address;
cout << "\nEnter student's city: ";
cin >> a.city;
cout << "\nEnter student's state: ";
cin >> a.state;
cout << "\nEnter student's id: ";
cin >> a.id;
cout << "\nEnter student's gpa: ";
cin >> a.gpa;
cout << endl;
} // enterRecords

void printRecords(Student a, int i)
{
cout << "Student " << i+1 << ": " << a.name << " " << a.address
<< " " << a.city << " " << a.state << " " << a.id
<< " " << a.gpa << endl;
} // printRecords



int main()
{
Student a[3];
int i = 0;

// entering info into struct
while(i < 3)
{
enterRecords(a[i]);
i++;
} // while

cout << endl << endl;

// printing info from struct to screen
i = 0;
while(i < 3)
{
printRecords(a[i], i);
i++;
} // while

cout << "\nPress ENTER to continue...";
cin.ignore();
cin.get();

return 0;
} // main
You are passing the struct by value into the function. You need to pass by pointer or by reference.
okay great, i got it to work now, thanks for the quick reply!
Topic archived. No new replies allowed.