2016-09-19 37 views
1

我是一個新手學習代碼,所以這可能很簡單,但我不明白髮生了什麼。C - while條件掛在條件

當我嘗試運行以下代碼時,它似乎掛在while循環條件中,並且在此之後不執行任何代碼。 !=運營商是否不適合此?

我也嘗試了do {} while,一切運行正常,但只要我將運算符設置爲'E',條件似乎仍然掛在那裏,並沒有脫離循環。

#include <stdio.h> 

int main(void) { 
    float n, content; 
    char operator; 

    n = content = 0; 
    operator = 'a'; 

    printf("Begin Calculations:\n"); 

    while (operator != 'E'); 
    { 
     scanf("%f %c", &n, &operator); 

     switch (operator) { 
     case 's': 
     case 'S': 
      content = n; 
      printf("= %f\n", content); 
      break; 

     case '+': 
      content = content + n; 
      printf("= %f\n", content); 
      break; 

     case '-': 
      content = content - n; 
      printf("= %f\n", content); 
      break; 

     case '*': 
      content = content * n; 
      printf("= %f\n", content); 
      break; 

     case '/': 
      content = content/n; 
      printf("= %f\n", content); 
      break; 

     case 'e': 
     case 'E': 
      printf("End of calculations\n"); 
      operator == 'E'; 
      break; 

     default: 
      printf("Invalid input\n"); 
      break; 
     } 
    } 
    return 0; 
} 
+2

'運營商== 'E';'會不會將e'轉換爲E'。請啓用編譯器警告! –

回答

1

您的while循環經典的錯誤:

while (operator != 'E'); 
{ 

將條件解析爲en空語句後的;,因此如果operator'E'不同,則您的while將永久運行。

採用經典的Kernighan和Ritchie括號風格使得發生這種錯誤明顯,不太可能:

while (operator != 'E') { 

還要注意的是,你應該從switch語句而不是測試中退出的環路Ewhile條件,這將允許雙方eE要正確處理(你的說法operator == 'E';是一個無操作,應書面operator = 'E';。同時檢查的scanf返回值,以避免在文件末尾不斷地循環。

這裏是一個改進版本:

#include <stdio.h> 

int main(void) { 
    float n, content; 
    char operator; 
    int active = 1; 

    n = content = 0; 

    printf("Begin Calculations:\n"); 

    while (active && scanf("%f %c", &n, &operator) == 2) { 
     switch (operator) { 
     case 's': 
     case 'S': 
      content = n; 
      printf("= %f\n", content); 
      break; 

     case '+': 
      content = content + n; 
      printf("= %f\n", content); 
      break; 

     case '-': 
      content = content - n; 
      printf("= %f\n", content); 
      break; 

     case '*': 
      content = content * n; 
      printf("= %f\n", content); 
      break; 

     case '/': 
      content = content/n; 
      printf("= %f\n", content); 
      break; 

     case 'e': 
     case 'E': 
      printf("End of calculations\n"); 
      active = 0; 
      break; 

     default: 
      printf("Invalid input\n"); 
      break; 
     } 
    } 
    return 0; 
} 
+0

經典; (分號)的確如此,感謝我現在得到它的深入響應。 –

2

在while語句的末尾刪除分號:

while(operator != 'E') 
{ 

分號結束,而語句體,換句話說,它的行爲就好像你會這樣寫:

while(operator != 'E') 
{ 
} 

造成無限循環。

2
  1. 你需要在while
  2. 你到底需要刪除分號考慮到這兩種情況下

因此改變

while(operator != 'E'); 

while(operator != 'E' && operator != 'e') 
0
while(operator != 'E'); 

正確的語法是while (...) { ... }沒有分號或do { ... } while (...);與。

printf("End of calculations\n"); 
operator == 'E'; 

賦值運算符是單個=。將==更改爲=,它應該沒問題。

0

您需要從while循環中刪除分號,因爲分號意味着語句的結束使你的程序沒有進一步執行

+0

_program未執行進一步_ - 這是不正確的 –

0

幾件事情。 1運算符是一個保留字,你應該改變變量名 2-(分號;),而()後

檢查更新代碼:

 #include <stdio.h> 

     int main(void) 
     { 
      float n, content; 
      char operator1; 

      n = content = 0; 
      operator1 = 'a'; 

      printf("Begin Calculations:\n"); 

      while(operator1 != 'E') 
      { 
      scanf("%c", &operator1); 

      switch(operator1) 
      { 
       case 's': 
       case 'S': 
      content = n; 
      printf("= %f\n", content); 
      break; 

       case '+': 
      content = content + n; 
      printf("= %f\n", content); 
      break; 

       case '-': 
      content = content - n; 
      printf("= %f\n", content); 
      break; 

       case '*': 
      content = content * n; 
      printf("= %f\n", content); 
      break; 

       case '/': 
      content = content/n; 
      printf("= %f\n", content); 
      break; 

       case 'e': 
       case 'E': 
      printf("End of calculations\n"); 
      operator1 = 'E'; 
      break; 

       default: 
      printf("Invalid input\n"); 
      break; 
      } 
      } 
      return 0; 
     } 
+0

感謝操作員提示,我會牢記這一點。 –