2013-04-07 108 views
-1

我希望在用戶選擇「VIEW」或「BID」後再次打開菜單..我將如何那樣做沒有無限循環?我將菜單設置爲自己的功能,然後在我的「主」功能中調用它。選擇一個菜單選項後,再次打印菜單..選擇選項後,「do while」循環無限

int menu() { 
    char sel[6]; 

    printf("Welcome to the Silent Auction!\n"); 
    printf("Please make a selection from the following:\n"); 
    printf("View Auction [VIEW]\n"); 
    printf("Bid on Auction [BID]\n"); 
    printf("Close Auction [CLOSE]\n"); 

return; 
    } 


    menu(); 
    char sel[6]; 
    scanf("%s", &sel); 

    do { 

      if (strcmp("VIEW", sel) == 0) { 
      ... 
      } 
     if (strcmp("BID", sel) == 0) { 
      printf("Which auction would you like to bid on?\n"); 
      scanf("%d", &choice); 
      if ... 
    }  else { 
       ... 
     } printf("How much would you like to bid?\n"); 
      scanf("%f", &user_bid); 
      if ... 
      else 
       cur_bid[choice] += user_bid; 
      } 
     if (strcmp("CLOSE", sel) == 0) { 
      for... 
     } 

     } while (sel != "CLOSE"); 




    return 0; 
    } 

回答

0

從你的代碼中,有2點要考慮。一個,menu函數不需要返回int,但可以是void,即void menu() {。由於您沒有閱讀該功能中的選擇,char sel[6]是多餘的。

下,實現自己的目標,while語句之前,你可以調用到menu下一個電話如下圖所示

int  close_flag = 0; 

printf("Enter your choice, VIEW/BID/CLOSE\n"); 
scanf("%6s", sel); 

printf("Entered Choice: %s\n", sel); 

do { 
    if(!strcmp(sel, "VIEW")) 
    { 
     printf("ENTERED VIEW\n"); 
    } 
    if(!strcmp(sel, "BID")) 
    { 
     printf("BIDDING\n"); 
    } 
    if(!strcmp(sel, "CLOSE")) 
    { 
     printf(">>>>CLOSING \n"); 
     close_flag = 1; 
    } 
    if(!close_flag) 
    { 
     printf("Enter your choice, VIEW/BID/CLOSE\n"); 
     scanf("%6s", sel); 
     printf("Entered Choice: %s\n", sel); 
    } 
} while(!close_flag); 

我已經修改了while條件聘請一個標誌,終止循環。此外,一個進一步的建議是的sel字符數限制爲字符顯示在scanf("%6s", sel);

+0

當用戶輸入「VIEW」的作品,但如果他們選擇另一option..say「BID」或「關閉「,我的if語句不工作。它只是終止。 – user2251238 2013-04-07 03:01:21

+0

確定適用於VIEW和BID。但是,當我選擇「關閉」程序不通過我的if語句爲此..它終止 – user2251238 2013-04-07 03:14:19

+0

@ user2251238 ..請檢查我更新的答案在哪裏我使用關閉標誌。 – Ganesh 2013-04-07 03:22:20