由于我创建了函数void logoff_check()
,程序在第一次(正确)输入时崩溃,编译时没有错误。我没有看到任何错误。
我知道我解释不好。如果您需要更多问题的详细信息,请发表评论,我将尽我所能解释。我是编程新手,顺便学习计算机科学。
这是功能:
void logoff_check(string username, string lab1[], string lab2[], string lab3[], string lab4[], bool& logoff)
{
for (int i = 0; i < 6; i++)
{
if (lab1[i] == (username + " ") && i < 5){
lab1[i] == "";
cout << "\n\n\t\t\tLog Off Successful.\n\n";
logoff = true;
break;
}
else if (lab2[i] == (username + " ")){
lab2[i] == "";
cout << "\n\n\t\t\tLog Off Successful.\n\n";
logoff = true;
break;
}
else if (lab3[i] == (username + " ") && i < 4){
lab3[i] == "";
logoff = true;
cout << "\n\n\t\t\tLog Off Successful.\n\n";
break;
}
else if (lab4[i] == (username + " ") && i < 3){
lab4[i] == "";
cout << "\n\n\t\t\tLog Off Successful.\n\n";
logoff = true;
break;
}
}
}
以下是代码的其余部分:
#include <iostream>
#include <cctype>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
void input(string& username, bool& userDigit);
void admin(string[], string[], string[], string[]);
void user(string username, string[], string[], string[], string[]);
void screen(string[], string[], string[], string[]);
void search(string ID, string [], string [], string [], string [], bool& notfound);
void logoff_check(string username, string [], string [], string [], string [], bool& logoff);
int main()
{
string username("");
const string Admin_key("ADMIN");
string lab1[5] = { "", "", "", "", "" };
string lab2[6] = { "", "", "", "", "", "" };
string lab3[4] = { "", "", "", "" };
string lab4[3] = { "", "", "" };
char ans;
bool digitID = true,
end = false,
inputbool = false,
logoff = false;
do
{
cout << endl;
input(username, digitID);
logoff_check(username, lab1, lab2, lab3, lab4, logoff);
if (Admin_key == username && !logoff)
admin(lab1, lab2, lab3, lab4);
else if (digitID && !logoff)
user(username, lab1, lab2, lab3, lab4);
else if (!logoff)
cout << "\nBad input data!\n";
cout << "\n\nTo continue hit Enter or press Q to exit\n";
cin.ignore();
cin.get(ans);
if (ans == 'q' || ans == 'Q') //alternate end because the program never ends with this condition
end = true; //in the do-while loop. I dont know why.
} while (!end);
return 0;
}
void input(string& username, bool& userDigit)
{
//Function Body
}
void admin(string lab1[], string lab2[], string lab3[], string lab4[])
{
//Function Body
}
void user(string username, string lab1[], string lab2[], string lab3[], string lab4[])
{
//Function Body
}
void screen(string lab1[], string lab2[], string lab3[], string lab4[])
{
//Function Body
}
void search(string ID, string lab1[], string lab2[], string lab3[], string lab4[],bool& notfound)
{
//Function Body
}
void logoff_check(string username, string [], string [], string [], string [], bool& logoff)
{
//Function is above
}
答案 0 :(得分:2)
你将超越数组的末尾,因为你的守卫落后了:
if (lab1[i] == (username + " ") && i < 5){
应该是:
if (i < 5 && lab1[i] == (username + " ")){
&&
运算符执行短路评估,您确实需要它,因为每个数组的长度都不同。
在之后放置测试你已经尝试索引数组并不是非常有用。