我正在输入字符串。但是我的程序在输入名称和地址时跳转到下一行。当名称行运行时,它不输入字符串并跳转到父名称,而地址处的下一行也是如此。
有什么问题请告诉我。感谢。
#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();
}
答案 0 :(得分:0)
在使用cin.ignore();
清空缓冲区之前使用getline()
。点击此处:When and why do I need to use cin.ignore() in C++?