dimanche 28 novembre 2021

I want to input the names as a string but I'm not able to do it correctly after the first input in C++

I want to input a string from the user and in doing so by the following code, I'm only able to input the first string correctly, other strings are showing some garabage value, please help.

#include<iostream>
#include<string.h>
using namespace std;
//Creating Arrays in OOPS
class Employee
{
    private:
        string name;
        int id;
        float salary;
    public:
        void setName(void)
        {
            cout<<"The name of your employee is "<<endl;
            getline(cin,name);
        }
        void setId(void)
        {
            cout<<"Enter the id of the employee"<<endl;
            cin>>id;
        }
        void getData(void)
        {
            cout<<"The name of your employee is "<<name<<endl;
            cout<<"The id of the employee is "<<id<<endl;
        }
};

int main()
{
    Employee svt[7];
    
        for(int i=0; i<7;i++)
        {
            svt[i].setName();
            svt[i].setId();
            svt[i].getData();
        }
    
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire