【发布时间】:2014-02-02 06:49:18
【问题描述】:
我的程序可以很好地处理 char、超出范围的数字等无效输入,但是当输入 1.2 等浮点值时会出现问题。程序再次打印菜单,并在打印错误消息之前要求用户输入。我试图解决的是不再打印菜单,但仍然很挣扎。例如,
做出选择:1.1 [这里是菜单内容] 做出您的选择:该选择无效。请再试一次。
#include <stdio.h>
#define QUIT 0
int menu();
int main(void)
{
int choice;
char c;
choice = menu();
while(choice != QUIT) //execute so long as choice is not equal to QUIT
{
choice = menu();
}
}
int menu(void)
{
int option;
printf("Text Encoder Service\n\n");
printf("1.\tEnter name of input file (currently 'Secret.txt')\n");
printf("2.\tEnter name of output file (currently not set)\n");
printf("3.\tEnter number of characters data should be shifted (currently +7)\n");
printf("4.\tEncode the text\n\n");
printf("0.\tQuit\n\n");
printf("Make your selection: ");
while( (scanf(" %d", &option) != 1) /* non-numeric input */
|| (option < 0) /* number too small */
|| (option > 4)) /* number too large */
{
fflush(stdin); /* clear bad data from buffer */
printf("That selection isn't valid. Please try again.\n\n");
printf("Your choice? ");
}
return option;
}
【问题讨论】:
标签: c