Struct access private variables

I am writing this program to understand the accesses of private variables because i have forgotten how to access them with getters and setters. I don't want the variables in private to be changed in the struct or even change into arrays but somehow leave it as it is. I know I can access them struct when I used Info but I don't know what to use to access the variable when the user enters it manually. I want it to be shown like this:

How many students are you going to enter:
3

enter student 1 name: bob

enter student 1 class year: 2011

enter student 1 ID: 432

enter student 2 name: billy

enter student 2 class year: 2012

enter student 2 ID: 726

enter student 3 name: Tom

enter student 3 class year: 1997

enter student 3 ID: 821

The results are:
Bob 2011 432
billy 2012 726
Tom 1997 821



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


#include <iostream>

using namespace std;


struct Student{
       
        private:
            int ID;
            string Name;
            int class_info;
       
        public:
            // set ID info
            void setID(int id){
                ID = id;
            }
       
            int getID(){
                return ID;
            }
           
            // set Name info
            void setName(string names){
                Name = names;
            }
       
            string getName(){
                return Name;
            }
           
            // set class info
           
            void setClass(int cls){
                class_info = cls;
            }
       
            int getClass(){
                return class_info;
            }
    };

int main()
{
     
   
    // number of students you want
    int student_amount;
   
    cout << "How many student are you going to enter " << endl;
    cin >> student_amount;
   
    // structure array of students
    struct Student Info[student_amount];
   
    string student_name;
    int student_class;
    int student_ID;
   
   
    for(int i =0; i < student_amount; i++){
       
        cout << "enter the student " << i << " name: " << endl;
        cin >> Info[i].student_name;
        Info.setName(student_name);

        cout << "enter the student " << i << " class year: " << endl;
        cin >> Info[i].student_class;
        Info.setClass(student_class);
       
        cout << "enter the student " << i << " ID: " << endl;
        cin >> Info[i].student_ID;
        Info.setID(student_ID);
    }
   
    cout << "the results are: " << endl;
   
    // print results of the input entered
    for(int i =0; i < student_amount; i++){
        Info.getName();
        Info.getClass();
        Info.getID();
    }
   

    return 0;
}

your getters and setters look fine.

you need a temporary when you use it:
cin >> temp;
Info[i].setsomething(temp);

some other stuff: c++ does not allow variable length arrays (use <vector> for this idea). Compilers support it, but its not technically legal.

you are mixing Info all over, in places acting as if array, other places not (it is, the ones not treating like array are wrong).

line 81 block is wrong, info is array. (should be Info[i].get...)
line 71 is wrong, info is array

like this:
cout << "enter the student " << i << " name: " << endl;
cin >>temp;
Info[index].setName(temp);
Last edited on
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
#include <iostream>
#include <vector>
using namespace std;

struct Student {
private:
	int ID {};
	string Name;
	int class_info {};

public:
	void setID(int id) { ID = id; }
	int getID() const { return ID; }

	void setName(const string& names) { Name = names; }
	string getName() const { return Name; }

	void setClass(int cls) {class_info = cls; }
	int getClass() const { return class_info; }
};

int getInt(const std::string& prm)
{
	int i {};

	while ((std::cout << prm) && (!(std::cin >> i) || std::cin.peek() != '\n')) {
		std::cout << "Not an integer\n";
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	return i;
}

int main()
{
	size_t student_amount {};

	cout << "How many student are you going to enter: ";
	cin >> student_amount;

	vector<Student> Info(student_amount);

	for (size_t i = 0; i < student_amount; ++i) {
		string student_name;

		cout << "enter the student " << i << " name: ";
		getline(cin >> ws, student_name);

		Info[i].setName(student_name);
		Info[i].setClass(getInt("enter the student "s + to_string(i) + " class year: "s));
		Info[i].setID(getInt("enter the student "s + to_string(i) + " id: "s));
	}

	cout << "the results are:\n";

	for (const auto& i : Info)
		cout << i.getName() << "  " << i.getClass() << "  " << i.getID() << '\n';
}

Topic archived. No new replies allowed.