C语言 郁闷的小问题

来源:百度知道 编辑:UC知道 时间:2024/06/18 07:23:18
#include "stdio.h"
#include "conio.h"
#include "math.h"
#define NULL 0
#define LEN sizeof(struct stu)

main()
{ int funct;
struct stu *head;
char ch;

clrscr();/*清屏函数*/

do{
printf("请选择你想要执行的操作(1-6)(0为退出):");

scanf("%d",&funct);
putchar('\n');

switch (funct)
{
case 0: return 0;
case 1:break;
case 2:break;
case 3:break;
case 4:break;
case 5:break;
case 6:break;
default: printf("error\n");
}

printf("是否要继续操作(Y/N):"); ch=getchar();
}while(ch!='N');

return 0;
getch();
}

这是一个程序的主函数(可以运行),我把一些与本问题无关的代码删除了。

我是想用此段代码实现功能的选择,用switch语句。
但是当我选择1-6功能时,
printf("是否要继续操作(Y/N):");执行完后 就会紧跟着执行
printf("请选择你想要执行的操作(1-6)(0为退出):");语句。

getchar前面加上fflush,清空输入流。
fflush(stdin);
ch=getchar();

#include "stdio.h"
#include "conio.h"
#include "math.h"
#define NULL 0
#define LEN sizeof(struct stu)

main()
{ int funct;
struct stu *head;
char ch;

do{

printf("请选择你想要执行的操作(1-6)(0为退出):");
scanf("%d",&funct);
putchar('\n');

switch (funct)
{
case 0: return 0;
case 1:break;
case 2:break;
case 3:break;
case 4:break;
case 5:break;
case 6:break;
default: printf("error\n");
}
scanf("%c",&ch);
printf("是否要继续操作(Y/N):");
scanf("%c",&ch);
}while(ch!='N');

return 0;
getch();
}

个人认为你做选择时是输入1-6之间的一个数,在你的所有分支什么也没做就跳出来了,所以就执行下一条语句了。

printf("是否要继续操作(Y/N):"); ch=getchar();
改为:
printf(