我是C++新手。 所以我做了這個程序,應該是一個密碼thingy。 問題是,當我輸入正確的密碼,即「bobby」時,它會直接轉到其他而不是如果。C++ if語句
什麼問題?
我的代碼:
#include <iostream>
using namespace std;
int main()
{
char password[] = "bobby";
char passinput[50];
char num[50];
top:
cout << "Please enter your password: " << endl;
cin >> passinput;
if(passinput==password)
{
cout << "Correct" << endl;
cin >> num;
}
else
{
cout << "Incorrect, try again" << endl;
goto top;
}
}
http://www.cplusplus.com/reference/string/string/compare/ –
'goto' [認爲是有害的(http://en.wikipedia.org/維基/ Considered_harmful)。不要使用它。 – Maroun
@MarounMaroun:「認爲有害」的散文[認爲有害](http://meyerweb.com/eric/comment/chech.html)。但是,是的,你很少想要「goto」,而你當然不想在這裏。 –