account admin; ifstream ifile("D:\\admin.txt"); string id; string pas; while (getline(ifile, id)) { admin.id = id; admin.password = pas; } ifile.close(); string tmi; cout << admin.id; string tmp; system("cls"); cout << "ID:" << ends; cin >> tmi; if (tmi == admin.id) { cout << "Password:" << ends; cin >> tmp; if (tmp == admin.password) { cout << " ___________________________________________________________________________________" << endl; cout << "| |" << endl; cout << "| Welcom,admin. | " << endl; cout << "| |" << endl; cout << "|___________________________________________________________________________________|" << endl; system("cls"); } } 这段代码哪里有问题
时间: 2024-02-16 17:08:00 浏览: 61
cpp.rar_site:www.pudn.com
这段代码的问题在于:
1. 在读取 `admin.txt` 文件时,只读取了 `id`,没有读取 `password`,因此 `admin.password` 变量未被正确赋值。
2. 在读取 `admin.txt` 文件时,没有将 `pas` 变量与文件中的密码进行匹配,导致 `admin.password` 变量仍然未被正确赋值。
3. 在读取完 `ifile` 后,没有将 `ifile` 关闭。
4. 在输出 `admin.id` 时,未在字符串后添加换行符。
以下是修改后的代码:
```
account admin;
ifstream ifile("D:\\admin.txt");
string id;
string pas;
if (getline(ifile, id)) {
if (getline(ifile, pas)) {
admin.id = id;
admin.password = pas;
}
}
ifile.close();
string tmi;
cout << admin.id << endl;
string tmp;
system("cls");
cout << "ID:" << ends;
cin >> tmi;
if (tmi == admin.id) {
cout << "Password:" << ends;
cin >> tmp;
if (tmp == admin.password) {
cout << " ___________________________________________________________________________________" << endl;
cout << "| |" << endl;
cout << "| Welcom,admin. | " << endl;
cout << "| |" << endl;
cout << "|___________________________________________________________________________________|" << endl;
system("cls");
}
}
```
阅读全文