c++ - 程序忽略用户名,只允许我输入密码?
问题描述
class acc_cr{
protected:
string username;
string password;
int i = 0;
public:
void create(){
fstream fout;
//new username:
cout<<"Enter Your New Username:\n";
while(i == 0){
getline(cin,username);
stringstream check(username);
string intermediate;
// Tokenizing w.r.t. space ' '
//Extracting the username up to first space
getline(check, intermediate,' ');
//Checking if anything is there after first space
if(getline(check, intermediate,' ')){
cout<<"Invalid Username-Contains Spaces!\nTry Another Username:\n";
}
//checking if this username already exists:
else if(fstream(username)){
cout<<"Username Already Exists!\nTry Another Username:\n";
}
else{
fout.open(username,ios::out);//creating new file to store contents
break;//breaking loop
}
}
//new password:
cout<<"Enter Your New Password:\n";
while(i == 0){
getline(cin,password);
//Extracting
stringstream pass(password);
string a_ps;
getline(pass,a_ps,' ');
//checking whether password contains spaces:
if(getline(pass,a_ps,' ')){
cout<<"Invalid Password - Constains Spaces\nTry Another Password\n";
}
else{
break;//breaking loop
}
}
//inserting data into csv file:
fout<<username<<","
<<password<<"\n";
}
};
此代码的目的是要求输入用户名和密码并创建一个 csv 文件,其中将存储这些详细信息,并且 csv 文件的名称与您在代码中看到的用户名相同....但是这里的问题是它不询问用户名,而只询问密码......输出是这样的:
Enter your username!
Enter Your Password!
|(blinker)//this is where i am only abled to type
它只允许我输入密码而不是用户名......我不知道为什么......
解决方案
使用提取运算符 >> 输入名称时,输入过程在第一个空格处停止。未处理的字符保留在输入缓冲区中(缓冲区是临时存储键盘输入(或其他序列,如文件)的内存块。)。这包括在数据之后从键盘输入的换行符“n”。如果您随后执行 getline() 操作,它将处理所有内容,直到第一个字符再次行。新行本身被删除。所以你会得到前一个条目的剩余部分,这是一个空行。这就是为什么我建议您最好使用cin,只需通过cin修改getline您的问题就可以解决
class acc_cr{
protected:
string username;
string password;
int i = 0;
public:
void create(){
fstream fout;
//new username:
cout<<"Enter Your New Username:\n";
while(i == 0){
cin>>username;
stringstream check(username);
string intermediate;
// Tokenizing w.r.t. space ' '
//Extracting the username up to first space
getline(check, intermediate,' ');
//Checking if anything is there after first space
if(getline(check, intermediate,' ')){
cout<<"Invalid Username-Contains Spaces!\nTry Another Username:\n";
}
//checking if this username already exists:
else if(fstream(username)){
cout<<"Username Already Exists!\nTry Another Username:\n";
}
else{
fout.open(username,ios::out);//creating new file to store contents
break;//breaking loop
}
}
//new password:
cout<<"Enter Your New Password:\n";
while(i == 0){
cin>>password;
//Extracting
stringstream pass(password);
string a_ps;
getline(pass,a_ps,' ');
//checking whether password contains spaces:
if(getline(pass,a_ps,' ')){
cout<<"Invalid Password - Constains Spaces\nTry Another Password\n";
}
else{
break;//breaking loop
}
}
//inserting data into csv file:
fout<<username<<","
<<password<<"\n";
}
};
推荐阅读
- c++ - 如何使用boost序列化进行二进制输出?
- cpu-architecture - gem5模拟器中的多核模拟:它是如何工作的
- android - Travis CI 显示 Android 平台 28 错误,但未在 yml 文件中的任何位置提及
- asp.net-core - 锚标记助手不重用当前路由值
- java - lambda 的 ModelMapper java 8 映射
- dart - 如何在 Flutter 中使用 RawMaterialButton 浏览页面浏览页面?
- android - Gradle 构建任务:app:dataBindingGenBaseClassesShopDevDebug 失败
- asp.net-core - VSTS (Azure DevOps) 构建突然失败
- python - 熊猫:按特定日期分组
- c# - 无限循环如何检查MSMQ中是否有相同的消息