2016-11-28 201 views
-4

我是C新手,我需要爲項目構建一個帶有循環的菜單。 我有2個問題。C菜單 - 否則如果

1)我想添加一個字符到else,如果在主菜單按「2」後詢問某個東西,問題將會是「您是否要參加活動?」用戶可以輸入聊天「Y」或「N」後,程序會感謝用戶並中斷,其他輸入會將用戶發送到主菜單

2)我想統計已經在主菜單與節目製作時,我按下「3」,我沒有任何想法我如何將count選項與「否則,如果」

int choice; 
while (1) 

{ 
    printf("Main Menu\n"); 
    printf("\n"); 

    printf("Please enter num:"); 
    scanf("%d", &choice); 

    printf("You entered %d\n", choice); 
    if (choice == 4) 
    { 
     break; 
    } 

    else if (choice == 1) 
     printf("Returned to main menu\n"); 

    else if (choice == 2) 
     printf("Are you going to the event?\n"); 

    else if (choice == 3) 
     printf("number of loops that made are \n"); 

    else if (choice == 4) 
     printf("Bye!\n"); 

    else 
     printf("Wrong Input!\n"); 
} 

printf("Bye!\n"); 

return 0; 

}

+0

1 - 我認爲我需要添加循環,但是這句話是對別人,如果循環了。 – opelka

回答

0

爲您計數循環的問題簡單地創建一個int變量,並在每個循環的結尾添加1。 e.g

int count=0; 
while(...) 
{ 
    *code* 
    count++; 
} 

對於其他問題,您可以使用

else if(choice==2) 
{ 
    char going; 
    printf("Are you going to the event? Y/N"); 
    scanf("%c",&going); 
    if(going=='N'||going=='n') 
     *code* 
     system("PAUSE"); 
     return 0; 
    else if(going=='Y'||going=='y') 
    { 
     *code* 
    } 
}