C++ Set and Get problem

hi friends, I am new to c++, just learning.
i want the program to access the private member of the class, and have used set and get, and validate when user input. but the program below is not working.
i cant compile it.
can anyone help me, thanks friends.


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

#include<iostream>
#include<string>


using namespace std;

class students
{
private :
string name;
string ID;
int age;

public :

string setName(string temp)
{

name = temp;

}
string getName()
{
return name;
}

void setID(int temp)
{

if(temp < 1)
{
cout << " You have enter an invalid ID number ";
exit(1);
}
else
ID = temp;

}

string getID()
{
return ID;
}

void setAge(int temp)
{

if (temp < 1)
{
cout<< " invalid age type"<<"program terminatingt";
exit(1);
}
else if (temp > 150)
{
cout <<" you are not human"<< "program terminating";
exit(1);
}
else
age = temp;

}
int getAge()
{
return age;
}


};

int main()
{
students st;
cout <<" Enter your name : ";
cin >>st.setName();
cout<< "Enter your ID :";
cin >>st.setID();
cout<< " Enter your age :";
cin >>st.setAge();
}
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
#include <iostream>
#include <string>
#include <cstdlib> //for exit();

using namespace std;


class students
{
private :
    string name;
    string ID;
    int age;

public :

    void setName(string temp) //return void
    {
        name = temp;
    }

    string getName()
    {
        return name;
    }

    void setID(int temp)
    {

        if(temp < 1)
        {
            cout << " You have enter an invalid ID number ";
            exit(1);
        }
        else
            ID = temp;

    }

    string getID()
    {
        return ID;
    }

    void setAge(int temp)
    {

        if (temp < 1)
        {
            cout<< " invalid age type"<<"program terminatingt";
            exit(1);
        }
        else if (temp > 150)
        {
            cout <<" you are not human"<< "program terminating";
            exit(1);
        }
        else
            age = temp;

    }
    int getAge()
    {
        return age;
    }


};

int main()
{
    //should be straight forward
    students st;
    string tmp;
    int i;
    cout << "Enter your name : ";
    getline(cin, tmp);
    st.setName(tmp);

    cout << "Enter your ID :";
    cin >> i;
    st.setID(i);

    cout<< "Enter your age :";
    cin >> i;
    st.setAge(i);
    return 0;
}
When returning something (get method) you have a return type. When you're setting a value (set method) you don't return anything back.

So just like blackcoder said you needed to have your "setName" method to return void (void means 'returns nothing') instead of returning a string like the "getName" method does.
Thank you very much friend.
Topic archived. No new replies allowed.