2015-07-03 165 views
1

我是一個初學者的學習代碼,我正在複製什麼YouTube編程教學視頻說。但是當我編寫代碼時,會導致一些錯誤。這個c + +代碼有什麼問題

下面的代碼:

#include <iostream> 
using namespace std; 
int main() 
{ 
    int num1 , num2; 

    cout<< " Enter number 1 and number 2 \n"; 
    cin>> num1 >> num2; 

    if (num1 == num2); 
     cout<< "The both numbers are equal \n"; 

    else if (num1> num2) 
     cout<< "Number 1 is greater than number 2 \n"; 
    else (num1< num2) 
     cout<< "Number 2 is greater than number 1 \n"; 

    return 0; 
} 
+5

請張貼您的錯誤消息。 – kRiZ

回答

8

注意;意味着表達結束,所以你應該改變

if (num1 == num2); 

if (num1 == num2) 

而且else不需要條件,所以改變

else (num1< num2) 

else 
1
  1. 你不需要;if條件檢查

  2. 如果你想要做一個健康檢查,你應該使用else if,在這種情況下else是不夠的:

    #include <iostream> 
    using namespace std; 
    int main() 
    { 
        int num1 , num2; 
    
        cout<< " Enter number 1 and number 2 \n"; 
        cin>> num1 >> num2; 
    
        if (num1 == num2) 
         cout<< "The both numbers are equal \n"; 
        else if (num1> num2) 
         cout<< "Number 1 is greater than number 2 \n"; 
        else if (num1< num2) 
         cout<< "Number 2 is greater than number 1 \n"; 
        return 0; 
    } 
    
+0

我貼你的代碼,而不是和那些錯誤出來太.. ------構建開始:項目:測試,配置:調試的Win32 ------ FirstProgram.cpp C:\文件(8):錯誤C2601:'main':本地函數定義不合法 c:\ documents and settings \ general \ my documents \ visual studio 2010 \ projects \ test \ test \ firstprogram.cpp(4):此行包含尚未匹配的'{' ==========構建:0成功,1失敗,0最新,0跳過========== – user5076066

+0

@ user5076066 http://coliru.stacked-crooked.com/a/d238f2da3dc4ddb8 – Steephen

1
; is not placed after `if` condition 

此外,否則不會得到一個條件...它總是檢查它的相應if的否定。 事實上如果if條件不爲else塊保存代碼被執行....改變

else (num1< num2) 
     cout<< "Number 2 is greater than number 1 \n"; 

else 
     cout<< "Number 2 is greater than number 1 \n"; 

您的問題將得到解決。