Home > other >  Error i get using get() and set() method in C
Error i get using get() and set() method in C

Time:01-13

I want to write a code that will accept input but store it as private in C but I keep getting a weird output after compiling it. Below is my code

#include <iostream>
#include <string>

using namespace std;

class MyClass{
    private:
        int salary;
        
    public:
        string fname;
        string lname;
        int age;
        
        void sent(string fname1, string lname1, int age1){
            cout<<"My name is "<<fname1<<" "<<lname1<<" and i am "<<age1<<" years old.";    
        };
        
        void setSalary(int s){
            salary = s;
        }
        
        int getSalary(){
            return salary;
        }
};

int main(){
    
    MyClass myObj;
    
    
    string fname2 = myObj.fname;
    string lname2 = myObj.lname;
    int age2 = myObj.age;
    int salaries;
    myObj.setSalary(salaries);
    
    cout<<"Enter first name: ";
    cin>>fname2;
    
    cout<<"Enter last name: ";
    cin>>lname2;
    
    cout<<"Enter age: ";
    cin>>age2;
    
    cout<<"Salary amount: ";
    cin>>salaries;
    
    myObj.sent(fname2, lname2, age2);
    cout<<"\n";
    
    myObj.getSalary();
    
    return 0;
}

CodePudding user response:

You should call

    cout<<"Salary amount: ";
    cin>>salaries;

before

    myObj.setSalary(salaries);

and also actually print the salary you get:

    cout << myObj.getSalary();
  •  Tags:  
  • Related