有人可以檢查我的代碼是否壞?當我執行它時,我的命令提示符崩潰。我的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:控制到達非空函數結束' –