Thread: C++ Set and Get help

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    1

    C++ Set and Get help

    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.



    Code:
    #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();
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Did you bother to read the error messages you got from the compiler?
    Code:
    Compiling: H:\temp.cpp
    H:\temp.cpp: In function `int main()':
    H:\temp.cpp:74: error: no matching function for call to `students::setName()'
    H:\temp.cpp:17: note: candidates are: std::string students::setName(std::string)
    H:\temp.cpp:76: error: no matching function for call to `students::setID()'
    H:\temp.cpp:28: note: candidates are: void students::setID(int)
    H:\temp.cpp:78: error: no matching function for call to `students::setAge()'
    H:\temp.cpp:46: note: candidates are: void students::setAge(int)
    Not only does it tell you which lines have errors, but points out what the functions are supposed to look like.

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    And please format your code. That is very ugly.

Popular pages Recent additions subscribe to a feed