2014-01-08 48 views
1

我正在學習使用C++,並決定創建一個密碼程序,用戶被要求輸入密碼並將用戶輸入與密碼進行比較,並返回錯誤或權限。出於某種原因,這個程序總是返回一個錯誤,我不知道爲什麼。它必須與比較字符串有關,但我不確定。簡單的C++密碼程序

#include <iostream> 
#include <string> 
using namespace std; 

int main(){ 
    string pass = "password"; 
    string input; 
    cout << "What is your password: "; 
    cin >> input; 
    if (input==pass){ 
     cout << "Correct" << endl; 
    }else{ 
     cout << "Wrong" << endl; 
    } 
    return 0; 
} 

我很想從程序員誰以任何方式使用C更深諳++作爲我剛剛從Python和過渡有點岩石轉移到C++一些幫助。

+4

[Works fine](http://coliru.stacked-crooked.com/a/9d443e4db3d55cd3) – chris

+1

實際代碼中的密碼字符串是否包含空格字符? – timrau

+0

這是實際的測試代碼,我在這裏發佈沒做任何更改。並將'string pass ='password''更改爲'string pass =「password \ r」'不起作用。 – LoreleiRS

回答

1

我發現我需要:

#include <string> 

得到插入運算符的定義(cin >> input;)和的std :: string :: ==操作符()(爲if (input==pass))。一旦我這樣做,它在Visual C++中運行良好。

你使用什麼編譯器?