2016-07-25 53 views
-5

繼承人的代碼:Im做一個計算器c和錯誤彈出,當我嘗試運行它

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

void main() { 
    int op,a,b; 

    printf("***Welcome to the calculatron***\n\n"); 

    do{ 
     printf("choose an operation\n\n"); 
     print(" 1 : Addition\n 2: Substraction\n 3: Multiplication\n 4: Division"); 
     scanf("%d",&op) 
    }while((op != 1 && (op != 2) && (op != 3) && (op != 4)); 

    printf("Type the first number"); 
    scanf("%d",&op) 
    printf("Type the second number"); 
    scanf("%d",&op); 


    switch(op) 
    case 1: 
     printf("You have chosen the Addition\n"); 
     printf("%d + %d = %d",a,b,a+b); 
     break; 

    case 2: 
     printf("You have chosen the Substraction\n"); 
     printf("%d + %d = %d",a,b,a-b); 
     break; 

    case 3: 
     printf("You have chosen the Multiplication\n"); 
     printf("%d + %d = %d",a,b,a*b); 
     break; 

    case 4: 
     printf("You have chosen the Division\n"); 
     printf("%d + %d = %d",a,b,a/b); 
     break; 
    } 

的錯誤

expected " ; " before " } " token and expected ") " before " ; " token are in lines 13 and 41

我不知道該怎麼辦,請幫忙。

+0

包括如下內容:#include #include

+0

'void main()'是一個無效的簽名,調用UB。 – Olaf

+2

兩個'scanf'調用寫入時沒有終止';'... –

回答

1

應該有scanf函數後一個分號:

printf("Type the first number"); 
    scanf("%d",&op); 

而且你缺少開關箱的開括號:

switch(op) 
{ 
} 
0

你也錯過switch{?仔細檢查編譯器錯誤。並看看你是否錯過了任何的語法。

0

你缺少後開關(OP)的花手鐲{開放

switch(expression) { 
    case constant-expression : 
     statement(s); 
     break; /* optional */ 

    case constant-expression : 
     statement(s); 
     break; /* optional */ 

    default : /* Optional */ 
    statement(s); 
} 
1

有你的程序中的幾個誤區。

對於初學者main函數不帶參數應該用C像

int main(void) 
{ 
    //... 
} 

聲明你忘了把分號語句與scanf例如在這個編碼塊

do{ 
    printf("choose an operation\n\n"); 
    print(" 1 : Addition\n 2: Substraction\n 3: Multiplication\n 4: Division"); 
    scanf("%d",&op) 
    // must be scanf("%d, &op); 
}while((op != 1 && (op != 2) && (op != 3) && (op != 4)); 

的條件同時可以寫得更簡單,就像

}while (op < 1 || op > 4); 

如果假定它不應接受負值,則最好聲明變量op的類型爲unsigned int

在這些語句

printf("Type the first number"); 
scanf("%d",&op) 
printf("Type the second number"); 
scanf("%d",&op); 

你必須爲變量ab而不是變量op輸入值。

printf("Type the first number"); 
scanf("%d",&a) 
printf("Type the second number"); 
scanf("%d",&b); 

switch語句應該有一個複合語句是你必須在其交換機的聲明中括號

switch(op) 
{ 
    //... 
} 

而且它會很好,包括標籤default的情況下,當用戶將進入無效的操作。

在交換機還printf語句無效文字和運營

case 2: 
    printf("You have chosen the Substraction\n"); 
    printf("%d + %d = %d",a,b,a-b); 
    // should be printf("%d - %d = %d",a,b,a-b); 
    break; 

case 3: 
    printf("You have chosen the Multiplication\n"); 
    printf("%d + %d = %d",a,b,a*b); 
    // should be printf("%d * %d = %d",a,b,a * b); 
    break; 

case 4: 
    printf("You have chosen the Division\n"); 
    printf("%d + %d = %d",a,b,a/b); 
    // should be printf("%d/%d = %d",a,b,a/b); 
    break; 

而且最好是操作的輸出結果有型long long int

例如

printf("You have chosen the Multiplication\n"); 
    printf("%d * %d = %lld", a, b, (long long int)a * b); 
    break; 
0

你在發佈之前應該檢查你的代碼。此外,您還需要更認真的編碼時,有許多錯誤在你的榜樣(我只說說這裏的do... while,很多人都已經談到了這些錯誤:

  • 第二printf()
  • 缺少 f
  • 缺少;scanf()
  • 丟失的圓括號您while條件

結果爲:

do { 
    printf("choose an operation\n\n"); 
    printf("1 : Addition\n 2: Substraction\n 3: Multiplication\n 4: Division"); 
    scanf("%d",&op); 
} while ((op != 1) && (op != 2) && (op != 3) && (op != 4)); 

必須更加嚴謹與您的代碼,學習尤其是當你要小心不要拿壞習慣。

+0

謝謝,但我仍然有這個問題: –

+0

預期的標識符或「(」之前的「做」 –

+0

@johndoe你必須首先正確地重寫你的代碼,糾正發現的錯誤,然後更新你的帖子,如果你自己找不到解決方案如果你不嘗試糾正你的代碼沒有任何幫助,你無法進步。 – naccyde

相關問題