2016-11-28 199 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* 
    } 
}