C++ 使用cin和getline会导致错误
在下面的代码中,我创建了两个类,第二个类继承自第一个类。但是,当我调用getdata函数时。它跳过输入,我尝试过使用cin.ingnore()和cin>>ws,但仍然会遇到相同的错误。它正常运行,直到“输入姓氏”,但在此之后,它只打印所有其他内容,而不接受输入C++ 使用cin和getline会导致错误,c++,class,oop,inheritance,C++,Class,Oop,Inheritance,在下面的代码中,我创建了两个类,第二个类继承自第一个类。但是,当我调用getdata函数时。它跳过输入,我尝试过使用cin.ingnore()和cin>>ws,但仍然会遇到相同的错误。它正常运行,直到“输入姓氏”,但在此之后,它只打印所有其他内容,而不接受输入 #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; class RegistrationMod
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
class RegistrationModule{
protected:
string Firstname;
string Lastname;
double cnic;
double contact;
string address;
static double challanno;
public:
RegistrationModule(){
Firstname = "";
Lastname = "";
cnic=0;
address = "";
contact=0;
challanno++;
}
};
double RegistrationModule::challanno=105487;
class monthlyentry : public RegistrationModule{
public:
void getdata(){
cout<<"Enter First Name"<<endl;
getline(cin,Firstname);
cin.ignore();
cout<<"Enter Last Name"<<endl;
getline(cin,Lastname);
cin.ignore();
cout<<"Enter your CNIC: "<<endl;
cin>>cnic;
cin.ignore();
cout<<"Enter your Address: "<<endl;
getline(cin,address);
cout<<"Enter your contact number: "<<endl;
cin>>contact;
cout<<"Your Challan Number is: "<<challanno<<endl;
}
};
int main(){
int size;
monthlyentry a;
a.getdata();
}
#包括
#包括
#包括
使用名称空间std;
类注册模块{
受保护的:
字符串名;
字符串Lastname;
双cnic;
双重接触;
字符串地址;
静态双challanno;
公众:
注册模块(){
Firstname=“”;
Lastname=“”;
cnic=0;
地址=”;
接触=0;
challanno++;
}
};
双重注册模块::challanno=105487;
班级每月进入:公共注册模块{
公众:
void getdata(){
cout您不能只是随意拍打,期望一切正常。默认情况下,ignore()
将删除1个字符。如果没有字符,它将设置位,并且您的流在您从中删除eof标志之前无法再读取任何内容
只有在有行尾剩余时(如格式化提取后)才使用ignore()
void getdata(){
cout<<"Enter First Name"<<endl;
getline(cin,Firstname);
cout<<"Enter Last Name"<<endl;
getline(cin,Lastname);
cout<<"Enter your CNIC: "<<endl;
cin>>cnic;
cin.ignore();
cout<<"Enter your Address: "<<endl;
getline(cin,address);
cout<<"Enter your contact number: "<<endl;
cin>>contact;
cin.ignore(); // might want to clear up before next extractions
cout<<"Your Challan Number is: "<<challanno<<endl;
}
std::getline(std::cin >> std::ws, Firstname);