c++ - 从文本文件中获取信息并匹配它 C++

标签 c++

我正在尝试创建一个创建新用户的程序。 创建此新用户需要用户名和密码。 如果用户名在文本文件中存在,程序会提示“用户名已存在,需要输入其他用户名” 用户名和密码将存储在一个文本文件中。

假设我的文本文件(userandPassword)已经有以下用户名和密码

根据用户名密码格式化的文本文件

joe abc
jane def

我的代码有问题

如果我第一次输入joe,程序会提示“用户名已存在!”

然后再输入jane,程序会提示“用户名已存在!”

但是如果我在那之后输入 joe,程序将假定用户名 joe 不存在并提示我输入密码。

我的输出(失败尝试)

Enter Desired UserName: joe
User Name existed!
Enter Desired UserName: jane
User Name existed!
Enter Desired UserName: joe
Enter Desired Password: 

期望的输出

Enter Desired UserName: joe
User Name existed!
Enter Desired UserName: jane
User Name existed!
Enter Desired UserName: joe
User Name existed! 
Enter Desired UserName: jane
User Name existed!
Enter Desired UserName: joe
User Name existed! 
Enter Desired UserName: joe
User Name existed! 
Enter Desired UserName: jane
User Name existed!
Enter Desired UserName: bob
Enter Password: <---(password will only be prompted to key in if the username does not exist in the text file, otherwise it's will contiune to show "User Name existed" if username exist in text file)

这是我的代码

主要.cpp

#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{

string line, userName,userNameInFile,password;
ofstream fout;
ifstream readFile("userandPassword.txt");
cout << "Enter Desired UserName: ";
cin >> userName;

while (getline(readFile, line)) {
    stringstream iss(line);
    iss >> userNameInFile;
    while (userNameInFile == userName)  {
        cout << "User Name existed!" << endl;
        cout << "Enter Desired UserName: ";
        cin >> userName;

    }
}

cout << "Enter Desired Password: ";
cin >> password;

fout.open("userandPassword.txt",ios::app);
fout << userName <<  ' ' << password << endl;
// close file.
fout.close();
cout << "\nAccount Created and stored into TextFile!" << endl;
return 0;

我不太确定是什么导致它变成这样。请帮忙。谢谢。


更新的答案*

string line, userName,userNameInFile,password;
ofstream fout;
vector<string> storeUserName;
ifstream readFile("userandPassword.txt");

while (getline(readFile, line)) {
    stringstream iss(line);
    iss >> userNameInFile;
    storeUserName.push_back(userNameInFile);
}

cout << "Enter Desired UserName: ";
do {
for (int i =0; i<storeUserName.size(); i++) {

        if (storeUserName[i] == userName) {
            cout << "Existing UserName Existed!\n";
            cout << "Enter Desired UserName: ";
        }

}
}while (cin >> userName);

最佳答案

首先,您从文件中读取用户 "joe",并检查它是否与用户输入的用户名匹配。它确实如此,然后您要求输入另一个用户名,然后是 “jane”。它匹配,所以内循环中断,外循环继续。此循环从文件中读取下一个用户名,它与用户最后输入的用户名相匹配,因此您要求用户输入一个新用户名。这个新名称与文件中的当前名称不匹配,因此内循环中断,外循环继续,但它位于文件末尾,因此中断,您使用现有用户名创建用户。

如果您在调试器中单步调试代码,这个问题会很容易发现。

要解决它,您可能需要分两步完成。首先将文件读入集合,例如一个std::vector包含带有用户名和密码的结构。然后,您向用户询问用户名,并在集合中查找。

关于c++ - 从文本文件中获取信息并匹配它 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20920855/

相关文章:

c++ - 串口通信 C++ Linux

c++ - std::unordered_map::insert 的更简单形式?

c++ - 如何让 PCRE 与 C++ 一起工作?

C++ : How to calculate modulo of a number raised to large power?

c++ - 我可以推断作为模板参数传递的函数的返回类型吗?

c++ - 改进了不需要保留顺序时的删除-删除习惯用法

c++ - 运行时检查失败 #02,随机数添加到数组

python - 面临导入错误: DLL load failed while implementing Microsoft azure iothub_service_client

c++ - 快速排序算法(递归)

c++ - 推送和弹出的不同互斥量