Pointer with a struct
So, I need to reference the struct of student info, but Im not allowed to use any parameters on the readStudent function. Any suggestions?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#include <iostream>
#include <string>
using namespace std;
void readStudent( );
struct Student {
string firstName;
string lastName;
string aNumber;
int courses;
double gpa;
};
struct Course {
int courseNumber;
string courseName;
int creditHours;
char grade;
};
int main ( ) {
readStudent();
cout << (*studenta).firstName;
system ("pause");
return 0;
}
void readStudent( ) {
Student student1;
Student *studenta = new Student;
studenta = &student1;
cout << "What is your first name?";
cin >> (*studenta).firstName;
cout << "What is your last name?";
cin >> (*studenta).lastName;
cout << "What is your A number?";
cin >> (*studenta).aNumber;
cout << "How many classes are you taking?";
cin >> (*studenta).courses;
}
|
Are you allowed to return anything from readStudent?
you could try making student1
a global variable.
What are you asking? You need to access struct Student? You're already doing that.
FYI, you have a memory leak.
studenta
has different scope and not on main()
's variable scope
Topic archived. No new replies allowed.