2016-01-01 28 views
0

我正在創建一個程序,要求用戶選擇他們想要的水品牌以及他們想要送到他們家的瓶子數量。該程序使用一個while循環詢問用戶他們想要的瓶子數量是否正確,如果沒有,循環將允許重新輸入他們想要的數量。在C程序中循環重複以前輸入的數據

該方案的工作,直到每次我必須選擇一個新的水品牌的時候;它堅持以前的水品牌的細節。我無法在程序中看到while循環的問題。代碼如下:

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

int main() 
{ 
    int valuechange = 0; 
    int correct = 0; 
    int choice = 0;  
    int numofbottles; 


    while (choice != 6) 
    { 
     printf("1. Buxton\n"); 
     printf("2. Evian\n"); 
     printf("3. Harrogate\n"); 
     printf("4. Power life\n"); 
     printf("5. Smart water\n"); 
     printf("6. Exit\n\n"); 
     printf("Enter your choice here: "); 
     scanf("%d", &choice); 
     /*each if statement has a while loop for if the user wants to re-enter a value of water bottles*/ 
     if (choice == 1) 
     { 
      while (correct != 1) 
      { 
       printf("Please choose how many bottles of Buxton you want to be delivered to your house:\n\n"); 
       scanf("%d", &numofbottles); 

       printf("you have chosen %d bottles, is this the correct amount?\n(enter 1 for yes or 2 for no):\n\n", numofbottles); 
       scanf("%d", &correct); 
      } 
      printf("Thank you, your order for %d Buxton bottles will be delivered to you\nwithin 3 working days.\n\n", numofbottles); 
     } 


     if (choice == 2) 
     { 
      while (correct != 1) 
      { 
       printf("Please choose how many Evian bottles you want to be delivered to your house:\n\n"); 
       scanf("%d", &numofbottles); 

       printf("you have chosen %d bottles, is this the correct amount?\n(enter 1 for yes or 2 for no):\n\n", numofbottles); 
       scanf("%d", &correct); 
      } 
      printf("Thank you, your order for %d Evian bottles will be delivered to you\nwithin 3 working days.\n\n", numofbottles); 
     } 

     if (choice == 3) 
     { 
      while (correct != 1) 
      { 
       printf("Please choose how many Harrogate bottles you want to be delivered to your house:\n"); 
       scanf("%d", &numofbottles); 

       printf("you have chosen %d bottles, is this the correct amount?\n(enter 1 for yes or 2 for no):\n\n", numofbottles); 
       scanf("%d", &correct); 
      } 
      printf("Thankyou, your order for %d Harrogate bottles will be deliver to you\nwithin 3 working days.\n\n", numofbottles); 
     } 


     if (choice == 4) 
     { 
      while (correct != 1) 
      { 
       printf("Please choose how many Powerlife bottles you want to be delivered to your house:\n\n"); 
       scanf("%d", &numofbottles); 

       printf("you have chosen %d bottles, is this the correct amount?\n(enter 1 for yes of 2 for no):\n\n", numofbottles); 
       scanf("%d", &correct); 
      } 
      printf("Thankyou, your order for %d Powerlife bottles will be deliver to you\nwithin 3 working days.\n\n", numofbottles); 
     } 

     if (choice == 5) 
     { 
      while (correct != 1) 
      { 
       printf("Please choose how many Smart water bottles you want to be delivered to your house:\n\n"); 
       scanf("%d", &numofbottles); 

       printf("you have chosen %d bottles, is this the correct amount?\n(enter 1 for yes or 2 for no):\n\n", numofbottles); 
       scanf("%d", &correct); 
      } 
      printf("Thankyou, your order for %d Smart water bottles will be deliver to you\nwithin 3 working days.\n\n", numofbottles); 
     } 
    } 
    return 0; 
} 
+1

在解決了眼前的問題後,您將需要考慮如何避免重複幾次相同的代碼塊5次(例如包含水的類型名稱的數組)。你也應該每次都檢查'scanf()'的返回值 - 這樣可以減少那些調用的幫助。你的循環應該處理用戶輸入0,或7,或99,或-12345678(或'xyz')的可能性 - 但是檢查scanf()的返回值很重要)或者輸入瓶子數量的這種值,或「正確」的值。 –

回答

2

您在一次迭代後不重置correct。它每次都設置爲0

while (choice != 6) 
{ 
correct = 0; 
... 
/*rest of the code */ 
} 

順便說一句,你有很多重複的代碼。您可以簡單地使用數組來存儲瓶子類型並在循環中使用數組元素。