gpt4 book ai didi

c++ - 这个功能有什么问题?它使我的程序崩溃而没有错误

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

自从我创建函数 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
}

最佳答案

你将越过数组的末尾,因为你的守卫落后了:

if (lab1[i] == (username + " ") && i < 5){

应该是:

if (i < 5 && lab1[i] == (username + " ")){

&& 运算符执行短路评估,您在这里确实需要它,因为每个数组的长度都不同。

将测试放在之后您已经尝试索引数组并不是很有用。

关于c++ - 这个功能有什么问题?它使我的程序崩溃而没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20157643/

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