Help with Objects and Functions

So Im basically finished with this code but I just can't figure out how to adjusts it so it gets the inputs for each of the 3 student before it outputs the results. If that didn't make any sense then heres the assignment this code is for: http://www.rdb3.com/python/exercises/12.2.pdf
Thank you!

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <string>
#include <iostream>
using namespace std;

struct Student
{
    string name;
    string address;
    string city;
    string state;
    string zip;
    string gender;
    string id;
    double gpa;
  
};

Student getStudent();
Student getStudenta();
Student getStudentb();
void showStudent( Student& );

int main()
{
    Student foo;
    Student a;
    Student b;
    
    foo = getStudent ();
    showStudent( foo );
    a = getStudenta ();
    showStudent ( a );
    b = getStudentb ();
    showStudent ( b );

}

Student getStudent()
{
    Student s;
    cout << "Enter for Student 1" << endl;
    cout << " Name: ";
    getline( cin, s.name );
    cout << " Address: ";
    getline( cin, s.address );
    cout << " City: ";
    getline( cin, s.city );
    cout << " State: ";
    getline( cin, s.state );
    cout << " Zip: ";
    getline( cin, s.zip );
    cout << " Gender: ";
    getline( cin, s.gender );
    cout << " ID: ";
    getline( cin, s.id );
    cout << " GPA: ";
    cin >> s.gpa;
    cin.ignore( 1000, 10 );
    return s;
}

Student getStudenta()
{
    Student a;
    cout << "Enter for Student 2" << endl;
    cout << " Name: ";
    getline( cin, a.name );
    cout << " Address: ";
    getline( cin, a.address );
    cout << " City: ";
    getline( cin, a.city );
    cout << " State: ";
    getline( cin, a.state );
    cout << " Zip: ";
    getline( cin, a.zip );
    cout << " Gender: ";
    getline( cin, a.gender );
    cout << " ID: ";
    getline( cin, a.id );
    cout << " GPA: ";
    cin >> a.gpa;
    cin.ignore( 1000, 10 );
    return a;
}

Student getStudentb()
{
    Student b;
    cout << "Enter for Student 3" << endl;
    cout << " Name: ";
    getline( cin, b.name );
    cout << " Address: ";
    getline( cin, b.address );
    cout << " City: ";
    getline( cin, b.city );
    cout << " State: ";
    getline( cin, b.state );
    cout << " Zip: ";
    getline( cin, b.zip );
    cout << " Gender: ";
    getline( cin, b.gender );
    cout << " ID: ";
    getline( cin, b.id );
    cout << " GPA: ";
    cin >> b.gpa;
    cin.ignore( 1000, 10 );
    return b;
}


void showStudent( Student& t)
{
    cout << "Name: " << t.name << endl;
    cout << "Address: " << t.address << endl;
    cout << "City: " << t.city << endl;
    cout << "State: " << t.state << endl;
    cout << "Zip: " << t.zip << endl;
    cout << "Gender: " << t.gender << endl;
    cout << "ID: " << t.id << endl;
    cout << "GPA: " << t.gpa << endl;
    
}
Last edited on
First get all 3 inputs and then output all 3.
This should do:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
    Student foo;
    Student a;
    Student b;
    
    foo = getStudent ();
    a = getStudenta ();
    b = getStudentb ();
    showStudent( foo );
    showStudent ( a );
    showStudent ( b );
}


BTW Why didn't you use an array. With a loop it would be much easier.
Why do you have 3 get functions?

All three functions are identical except for the student number in the prompt.

BTW, it makes sense to make getStudent() and showStudent() member functions of Student.

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <string>
#include <iostream>
using namespace std;

struct Student
{   string name;
    string address;
    string city;
    string state;
    string zip;
    string gender;
    string id;
    double gpa;

    void getStudent (int n);
    void showStudent ();  
};

void Student::getStudent(int n)
{   cout << "Enter for Student " << n << endl;
    cout << " Name: ";
    getline( cin, name );
    cout << " Address: ";
    getline( cin, address );
    cout << " City: ";
    getline( cin, city );
    cout << " State: ";
    getline( cin, state );
    cout << " Zip: ";
    getline( cin, zip );
    cout << " Gender: ";
    getline( cin, gender );
    cout << " ID: ";
    getline( cin, id );
    cout << " GPA: ";
    cin >> gpa;
    cin.ignore( 1000, 10 );
}

void Student::showStudent ()
{   cout << "Name: " << name << endl;
    cout << "Address: " << address << endl;
    cout << "City: " << city << endl;
    cout << "State: " << state << endl;
    cout << "Zip: " << zip << endl;
    cout << "Gender: " << gender << endl;
    cout << "ID: " << id << endl;
    cout << "GPA: " << gpa << endl;    
}

int main()
{   Student students[3];

    for (int i=0; i<3; i++)
        students[i].getStudent(i);
    for (int i=0; i<3; i++)        
        students[i].showStudent();
    return 0;        
}

Last edited on
Topic archived. No new replies allowed.