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
#include <iostream>
usingnamespace 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;
}