2013-04-07 107 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