gpt4 book ai didi

c++ - 未输入 If-block,不知道为什么

转载 作者:行者123 更新时间:2023-11-28 03:15:06 25 4
gpt4 key购买 nike

这个函数应该读取文件的每一行并将其与用户输入的字符串进行比较并检查它们是否匹配。它基本上可以防止文件中出现重复信息。无论如何,该程序没有进入我的“if (loginsFile.is_open())”语句,我不明白为什么。

fstream loginsFile;
loginsFile.open("C:/logins.txt", ios::in | ios::out | ios::trunc | ios :: app | ios:: ate);

string username;
string password;
string info;
bool exists = true;
CheckingAccount cA;
SavingsAccount sA;

do {
cout << "Enter Username: ";
cin >> username;
cout << "Enter Password: ";
cin >> password;
cout << endl;
info = username + " " + password;
if (loginsFile.is_open()){
while (loginsFile.good()){
string line;
getline(loginsFile, line);
cout << "line is " << line.substr(line.find_last_of(" ")) << "\n" << "info is " << line.substr(line.find_last_of(" "));
if (line.substr(line.find_last_of(" ")) == info.substr(0, info.find_last_of(" "))){
exists = false;
cout << "Username already exists!" << endl << "Program is not case sensitive!";
} //end if
} //end while
} //end if
} while (exists == true); //end do while

loginsFile << info << endl;
loginsFile.close();
logins[info] = make_pair(cA, sA);
cout << info.substr(0, info.find(' ')) << " Has Been Successfully Registered!" << "\n" << "\n";
return logins;

最佳答案

您的 if 语句未被输入,因为文件未打开。该文件未打开,因为您不能将“std::ios::trunc”(截断文件删除所有内容)和“std::ios::app”(追加到文件末尾)相结合,因为它们相互矛盾。

关于c++ - 未输入 If-block,不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17139240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com