2017-08-26 37 views
-5

我正在嘗試編寫一個程序,該程序在鍵入load時在控制檯中顯示自定義消息。但我似乎無法讓它工作。 :/無法理解if命令

#include "stdafx.h" 
#include "iostream" 
#include "string" 

using namespace std; 

int main() 
{ 
    int commands(); 
    string text; 
    cout << "Write your registered e-mail to continue...\n"; 
    cin >> text; 

    string input; 
    cout << "\n"; 
    cout << "Welcome " << text << endl; 
    cout << "\n"; 
    cout << "www.steamcommunity.com/id/thetraderdazz> "; 
    cin >> input; 
    if (input = load); 
    cout << "loading..."; 

    system("pause"); 

    return 0; 
} 

這也給了我下面的錯誤:

identifier "load" is undefined.

+6

你似乎有對C++的不少誤解。你應該退後一步,系統地從一本好書中學習語言。 –

+0

同意上面的評論。這裏有一些你的特定問題的幫助https://stackoverflow.com/questions/6222583/how-to-compare-strings – ivo

+0

我同意上面的所有意見,但首先,你應該使用strcmp函數而不是'=' (它的作用,我認爲你的意思是'=='),你必須使'load'成爲一個字符串,所以:''load''。 –

回答

2
if (input = load); 

有三個錯誤,這條線。首先是您使用賦值運算符=而不是比較運算符==。前者指定,後者比較。

第二個錯誤是您在括號後面放置了分號,表示爲空體。你的編譯器應該給你一個警告。

最後,沒有變量load。你的意思是比較字符串文字"load"

修復到

if (input == "load") 
    cout << "loading...\n"; 
1

你可能預期下

if (input == "load") { 
cout << "loading..."; 
}