-4

I am inputting strings. But my program jumps to the next line while inputting name and address. When the name lines runs, it does not input string and jumps to the father name and same is done with the next lines at address.

What is the issue please tell me . thanks.

#include<iostream>
#include<string.h>
#include<sstream>
using namespace std;

class StaffRegistration{
private:

    ///data members
    string cnic, name, fatherName, dob, qualification, designation, joiningDate, address;
    int day, month, year;
    int salary, contact;

    ///set methods
    void setCNIC(string sCNIC);
    void setName(char sname[]);
    void setFatherName(string sfName);
    void setDOB(char sdob[]); //array because its fixed
    void setQualifaction(string sq);
    void setDesignation(string sdesignation);
    void setJoiningDate(char sjDate[]);
    void setAddress(string sAddress);
    void setSalary(int);
    void setContact(int scontact);

    //get methods
    void getCNIC();
    void getName();
    void getFatherName();
    void getDOB();
    void getQualification();
    void getDesignation();
    void getJoiningDate();
    void getAddress();
    void getSalary();
    void getContact();


public:

    void inputStaffData();
    void displayStaffData();
};

void StaffRegistration::inputStaffData()
{
    cout<<"Please enter National Identity Card Number (CNIC)\n";
    getCNIC();

    cout<<"\nPlease enter name \n";
    getName();

    cout<<"\nPlease enter father name \n";
    getFatherName();

    cout<<"\nPlease enter date of birth (dd,mm,yyyy)\n";
    getDOB();

    cout<<"\nPlease enter qualification\n";
    getQualification();

    cout<<"\nPlease enter designation\n";
    getDesignation();

    cout<<"\nPlease enter salary\n";
    getSalary();

    cout<<"\nPlease enter joining date (dd,mm,yyyy)\n";
    getJoiningDate();

    cout<<"\nPlease enter contact number\n";
    getContact();

    cout<<"\nPlease enter address information\n";
    getAddress();

    cout<<endl<<endl<<"Record added successfully!\n";
}


// definitions of getter functions
void StaffRegistration::getName()
{
    //char name[40];
    string name;
    getline(cin, name);
    cout<<name;
}

void StaffRegistration::getCNIC()
{
    string gCNIC;
    cin>>gCNIC;
    setCNIC(gCNIC);
}

void StaffRegistration::getFatherName()
{
    string fname;
    getline(cin, fname);
    setFatherName(fname);
}

void StaffRegistration::getDOB()
{
    char dob[11];
    cin>>dob;
    setDOB(dob);
}

void StaffRegistration::getQualification()
{
    string q;
    getline(cin, q);
    setQualifaction(q);
}

void StaffRegistration::getDesignation()
{
    string designation;
    getline(cin, designation);
    setDesignation(designation);
}

void StaffRegistration::getSalary()
{
    int salary;
    cin>>salary;
    setSalary(salary);

}

void StaffRegistration::getJoiningDate()
{
    char jdate[11];
    cin>>jdate;
    setJoiningDate(jdate);

}

void StaffRegistration::getContact()
{
    int con; 
    cin>>con;
    setContact(con);
}

void StaffRegistration::getAddress()
{
    string add;
    getline(cin, add);
    setAddress(add);
}

////definitions of setter functions
void StaffRegistration::setName(char sName[])
{
    name=sName;
}

void StaffRegistration::setCNIC(string sCNIC)
{
    cnic=sCNIC;
}

void StaffRegistration::setFatherName(string sfName)
{
    fatherName=sfName;
}

void StaffRegistration::setDOB(char sdob[])
{
    dob=sdob;
}

void StaffRegistration::setQualifaction(string sq)
{
    qualification=sq;
}

void StaffRegistration::setDesignation(string sdesignation)
{
    designation=sdesignation;
}

void StaffRegistration::setSalary(int s)
{
    salary=s;
}

void StaffRegistration::setJoiningDate(char sjDate[])
{
    joiningDate=sjDate;
}

void StaffRegistration::setContact(int scontact)
{
    contact=scontact;
}

void StaffRegistration::setAddress(string sAddress)
{
    address=sAddress;
}



int main()
{
    StaffRegistration a;
    a.inputStaffData();

}
  • 1
    Your program never spontaneously "jumps". Begin there. Almost certainly the cause is that you are failing to handle bad input, or to throw away newlines, or to reset error flags, or indeed to do any error handling whatsoever. – Lightness Races in Orbit Aug 04 '15 at 16:30
  • Sir please tell me how to resolve it. I have to submitt assignment to my teacher and there are just 30 minutes left of dead line. – David Stevenson Aug 04 '15 at 16:45
  • 1
    Probably should have started working on this a little earlier, wouldn't you say? If I spoonfed you a full solution now then your grade would not reflect your skills or knowledge. That doesn't seem fair. – Lightness Races in Orbit Aug 04 '15 at 16:46

1 Answers1

0

use cin.ignore(); before using getline() to empty the buffer.Read more here: When and why do I need to use cin.ignore() in C++?

Community
  • 1
  • 1
Pushkar
  • 760
  • 15
  • 37