有人可以检查我的代码是否坏?当我执行它时,我的命令提示符崩溃。我的C++程序不断崩溃ist我的代码不好?
我将问题范围缩小到本节:
string result = "";
result = pwpasst (username, password, "admin", "123");
result = pwpasst (username, password, "root", "456");
result = pwpasst (username, password, "peter", "789");
如果我注释掉2出的3个功能似乎工作。 + 我不知道我的代码是坏的还是我的命令提示符有问题。
的其余代码:
#include <iostream>
using namespace std;
string pwpasst (string username, string password, string un, string pw){
if (username == un && password == pw)
{
return "You are logged in!";
}
};
int main()
{
string username;
string password;
cout << "Enter your username: ";
cin >> username;
cout << "Enter your password: ";
cin >> password;
string result = "";
result = pwpasst (username, password, "admin", "123");
result = pwpasst (username, password, "root", "456");
result = pwpasst (username, password, "peter", "789");
if (result != "You are logged in!"){
cout << "Wrong password or username!";
} else { cout << result;}
}
不是所有通过'pwpasst'的路径都返回一个值。 –
如果'(username == un && password == pw)'评估为false,则调用undefined-行为。 – George
作为一种预防措施,你应该编译你的代码,总是带有诸如'-Wall'这样的选项,在这种情况下会给你一个警告,例如'warning:控制到达非空函数结束' –