int password(const int & key){
static int n = 0;
int k;
cout<<"Please input your password";
cin>>k;
n++;
if(n < 3)
if(k == key) {
return 1;
}
else password(key);
else
if(k != key) return 0;
cout<<"test"; // 以x86为例,没return的都会走到这里,然后寄存器eax就是cout<<"test"的返回值,然后这个eax就当成password的返回值了
}
没必要纠结这种问题,还不如把代码改的没有歧义#include<iostream>
using namespace std;
int password(const int & key);
int main() {
if (password(123456)) {//123456是正确的密码
cout<<"Welcome!"<<endl;
} else {
cout<<"Sorry, you are wrong!"<<endl;
}
return 0; // 加个返回值
}
int password(const int & key) {
static int n = 0;
int k;
if (n++ < 3) {
cout << "Please input your password";
cin >> k;
cout << n << endl;
if(k == key) {
return 1;
} else {
return password(key);
}
}
return 0; // 所有分支都要返回值,别让程序不可控
}
|