Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Unable to get input from user

+1 vote
asked Jan 30, 2020 by Abdul Haseeb
When i run this program. Program run and work properly. But when user gives input to name and write name (i.e Abdul Haseeb) then user jump to Salary not go to on the Address or Ask for Address. Please check this and guide me how i can handle this. I use many trick available on internet but no one working.

#include<iostream>

#include<string>

using namespace std;

class employee{

private:

int salary;

char name[20], address[50];

public:

int empno;

void input(void);

void output(void);

};

void employee::input(void){

cout << "\nEmployee Number:";

cin >> empno;

cout << "\nEnter Name:";

cin.getline(name,sizeof(name));

cout << "\nEnter Address:";

cin.getline(address, sizeof(address));

cout << "\nEnter Salary";

cin >> salary;

}

void employee::output(void){

cout << "Name is: " << name;

cout << "Address is: " << address;

cout << "Salary is: " << salary;

}

int main(){

employee emp;

emp.input();

emp.output();

}

1 Answer

0 votes
answered Mar 5, 2020 by James Hunter (220 points)
Here you go ...

As far as I know (and I definitely don't know everything) getline() only accepts an input stream as the first parameter and a string to store the entered value as the second parameter.

I changed your definition of name and address for char[xx] to strings, the parameters you passed to getline(), a little tidying up on the output text, and added a cin.ignore(); after you input the integer.

I am not entirely sure why this is needed, but I have run into it before. Would love to get feedback from someone that knows more. The way I understand it is ...

You entered an integer, so the \n remains in the stream and then gets picked up by the next request for input.  I normally input everything as strings and then convert/check/reject input as needed before I use it.

stringstream has some interesting functions for this purpose. Also, boost has some conversion functions that are very handy.

Hope this helps ...

#include <iostream>
#include <string>

using namespace std;

class employee
{
    private:

        int salary;

        string name, address;

    public:

        int empno;

        void input(void);

        void output(void);
};

void employee::input(void)
{
    cout << "\n Employee Number: ";
    cin >> empno;
    cin.ignore();

    cout << "\n      Enter Name: ";
    getline(cin, name);

    cout << "\n   Enter Address: ";
    getline(cin, address);

    cout << "\n    Enter Salary: ";
    cin >> salary;
}

void employee::output(void)
{

    cout << "\n\n    Name is: " << name;

    cout << "\n Address is: " << address;

    cout << "\n  Salary is: " << salary;

}

int main()
{

    employee emp;

    emp.input();

    emp.output();

}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...