2016-05-12 4 views
2

elseステートメントにエラーがあり、その前に何かが来るはずです。 私は本当にあなたのelseステートメントが間違っている誰かが私を助けることができれば、それは'else'の前にエラーが予想されるというエラーコードを修正しました

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 


int random1_6() 
{ 
    return ((rand() % 6) + 1); 
} 

int main(void) 
{ 
    int a, b, c, d, equal, sum; 
    srand(time(NULL)); 
    a = random1_6(); 
    b=random1_6(); 
    sum = a + b; 
    printf("\n The player rolled: %d + %d = %d. \n the players point is %d. \n", a, b, sum, sum); 
    if (sum ==7); 
    { 
     printf("The player wins. \n"); 
    } 
    else (sum !=7); 
    { 
     c = random1_6(); 
     d=random1_6(); 
     equal = c + d;  
     printf("\n The player rolled: %d + %d = %d", c, d, equal); 
} 
+1

のremove '(!合計= 7)'です。 – claudios

+0

if(sum == 7)の後にカンマ(;)も削除する必要があります。 – Carlo

答えて

-1

素晴らしいことだいただきまし 間違っ把握傾けます。 ここはif else synのテキストです。

if (some-condition) { 
} else{ 
} 

それとも、単に削除

if (some-condition) { 
} else if (some-condition) { 
} 

を使用することができます(合計を!= 7)あなたのコードは動作します。

-1

不要なセミコロンを削除します。

if (sum == 7) { 
printf("The player wins. \n"); 
} else { 
c = random1_6(); 
d = random1_6(); 
equal = c + d; 
printf("\n The player rolled: %d + %d = %d", c, d, equal); 
} 
0

(1)あなたはif;を配置する必要はありません。あなたが書く場合:conditionを確認し、あるものは何でも結果は常にblockを実行しますよう

if(condition); { block; } 

Cは、それを解釈します。 elseに到達した場合、sum != 7は常に真であるため、チェックする必要はありません。elseに到達した場合、sum != 7は常にtrueです。あなたがそれを必要とする場合はどのような場合には

は、正しい構文は次のとおりです。if

} 
else if(sum != 7) 
{ 

;

なし(4)また、宣言、main関数から何かを返すのを忘れてintの値を返します。

(5)最終コード}が不足していた可能性があります。間違ったコピー&貼り付けですか?ここで

は、上記の修正とあなたのコードです:

int random1_6() 
{ 
    // enter code here 
    return ((rand() % 6) + 1); 
} 

int main(void) 
{ 
    int a, b, c, d, equal, sum; 

    srand(time(NULL)); 

    a = random1_6(); 
    b = random1_6(); 

    sum = a + b; 

    printf("\n The player rolled: %d + %d = %d. \n the players point is %d. \n", a, b, sum, sum); 

    if(sum == 7) // (1) Remove ";", it's wrong! 
    { 
     printf("The player wins. \n"); 
    } 
    else // (2) Remove "(sum !=7)", it's useless. (3) Remove ";" , it's wrong! 
    // else if(sum != 7) // This is the correct sintax for an else if, just in case :-) 
    { 
     c = random1_6(); 
     d = random1_6(); 
     equal = c + d;  
     printf("\n The player rolled: %d + %d = %d", c, d, equal); 
    } 

    return 0; // (4) The function is declared returning an int, so you must return something. 
} // (5) This bracket was missing :-) 

ホープすべてがあなたのelse文で明らか

関連する問題