我是 C++ 的初学者,这里有一些问题!
目前,我有一个名为“users.txt”的文本文件,文本文件中存储了一些数据:
simon
1234
123123
john
4321
34343
weejay
8888
7777
dicson
4444
3333
kendy
5555
9998
首先,用户需要输入用户名和密码才能登录。但是,当我运行该程序时,它会直接说我输入了无效的密码或 ID,尽管我输入正确。我可以知道如何解决这个问题吗??
#include<iostream>
#include<fstream>
#include<stdlib.h>
using namespace std ;
struct users {
string name ;
string pincode ;
int amount ;
};
int main ()
{
cin.sync_with_stdio(0);
cin.tie(0);
string name[10] ;
string pincode [10];
int amount [10];
users obj [10] ;
ifstream infile("users.txt");
for (int i=0; i <10 ; i++)
{
infile >> name [i];
infile >> pincode [i];
infile >> amount [i] ;
}
w:
string username ;
string password;
cout <<"enter username : " << endl ;
cin.ignore();
getline(cin, username);
cout << "enter password : " << endl ;
cin.ignore();
getline(cin, password );
bool islogin = false ;
for (int i=0; i < 10 ; i++)
{
if (password == pincode[i] && username == name[i] )
{
cout << "successfully log in ! " << endl ;
islogin = true ;
break ;
}else
{
islogin = false ;
}
}
if (!islogin)
{
cout << "sorry you have entered invalid pin code or ID " << endl ;
cout << "you want to login again " << endl ;
cout << "1. yes" << endl ;
cout << "2. no" << endl ;
char option ;
cin >> option ;
if (option == '1')
{
system("cls");
fflush(stdin);
goto w ;
}else
{
cout << "thanks for using our system " << endl;
}
}
}
最佳答案
cin.ignore()
简单地忽略从 cin
读取的下一个字符.我想你正在尝试 ignore
解决这个问题Why does std::getline() skip input after a formatted extraction? .
只有在从格式化输入(使用 >>
读取)切换到 getline
时才需要这样做忽略流中剩余的换行符(否则 std::getline
返回空字符串)。正确的调用是:cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
, 这将忽略输入中直到并包括第一个换行符的所有剩余字符。
当您阅读 username
那是你第一次使用 cin
ignore()
call 只是忽略用户名的第一个字符。你的第二个ignore()
忽略密码的第一个字符。
一个完整的例子是:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <vector>
struct users {
std::string name;
std::string pincode;
int amount;
};
int main()
{
std::vector<users> obj;
std::ifstream infile("users.txt");
if (!infile)
{
std::cout << "users.txt not found\n";
return 1;
}
for (int i = 0; i < 10; i++)
{
users user;
infile >> user.name;
infile >> user.pincode;
infile >> user.amount;
if (!infile)
{
break;
}
obj.push_back(user);
}
bool islogin = false;
while (!islogin)
{
std::string username;
std::string password;
std::cout << "enter username :\n";
std::getline(std::cin, username);
std::cout << "enter password :\n";
std::getline(std::cin, password);
for (auto& user:obj)
{
if (password == user.pincode && username == user.name)
{
std::cout << "successfully log in !\n";
islogin = true;
break;
}
else
{
islogin = false;
}
}
if (!islogin)
{
std::cout << "sorry you have entered invalid pin code or ID\n";
std::cout << "you want to login again\n";
std::cout << "1. yes\n";
std::cout << "2. no\n";
char option;
std::cin >> option;
if (option == '1')
{
system("cls");
// ignore the newline after 1 so that the next std::getline succeeds
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
else
{
std::cout << "thanks for using our system\n";
break;
}
}
}
return 0;
}
关于c++ - 为什么我无法登录成功?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57985571/