switch()顺序不符合原意

starcat 2009-06-21 07:48:09
为什么当我不管输入什么case,程序都要先执行1次default语句?
‘4’是输出不及格学生信息。
#include <stdio.h>
#define N 2
#define M 4
void main()
{ int score[N][M];
char choice='1';
void input(int [][],int,int);
void aver_stu(int [][],int,int);
void aver_cour(int [][],int,int);
void orde_aver(int [][],int,int);
void failed(int [][],int,int);
input(score,N,M);
printf("\n ==========the Score Processing System ====================\n");
printf("1.print each student's average\n");
printf("2.print each course's average\n");
printf("3.Order the students by student's average decreasingly \n");
printf("4.print the failed student \n");
printf("0.Exit the system \n");
printf("===============================================\n");
printf("Please choise (0-4): \n");

while(choice!='0')
{
choice=getchar();
switch(choice)
{case '1':
aver_stu(score,N,M); break;
case '2':
aver_cour(score,N,M); break;
case '3':
orde_aver(score,N,M); break;
case '4':
failed(score,N,M); break;
case '0':
exit(0);
default:
printf("Choice Error,Please select again(0-4):");
}
}
}

void input(int s[N][M],int x,int y)
{
int i,j;
for(i=0;i<x;i++)
for(j=0;j<y-1;j++)
{ printf("student%d course%d score:",i+1,j);
scanf("%d",&s[i][j]);
}
}

void aver_stu(int s[N][M],int x,int y)
{
int i,j,sum;
for(i=0;i<x;i++)
{ for(j=0,sum=0;j<y-1;j++)
sum+=s[i][j];
s[i][j]=sum/3;
printf("%d\n",s[i][j]);
}
}

void aver_cour(int s[N][M],int x,int y)
{
int i,j,sum;
for(j=0;j<y-1;j++)
{for(i=0,sum=0;i<x;i++)
sum+=s[i][j];
printf("%d ",sum/N);
}
printf("\n");
}

void orde_aver(int s[N][M],int x,int y)
{
int i,j,k,t,temp1,temp2,r[N][2];
for(i=0;i<x;i++)
{r[i][0]=i+1; r[i][1]=s[i][y-1];}
for(i=0;i<x-1;i++)
for(k=i+1;k<x;k++)
{if(r[i][1]<r[k][1])
{t=k;
temp1=r[i][0];temp2=r[i][1];
r[i][0]=r[t][0];r[i][1]=r[t][1];
r[t][0]=temp1;r[t][1]=temp2;
}
}
for(j=0;j<N;j++)
printf("%d %d %d\n",j+1,r[j][0],r[j][1]);
}

void failed(int s[N][M],int x,int y)
{
int i,j,k;
for(i=0;i<x;i++)
{
for(j=0,k=1;j<y-1;j++)
if(s[i][j]<60)
{if(k-->0)
printf("student%d ",i+1);
printf("course%d score:%d ",j,s[i][j]);
}
printf("\n");
}
}

...全文
46 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
去掉单引号,反正你那也没什么用,用scanf代替就行了

magipan 2009-06-21
  • 打赏
  • 举报
回复
16L正解
tanwan 2009-06-21
  • 打赏
  • 举报
回复
我看一下input函数在看一下switch之后感觉是你input输入了之后肯定要按回车然后进入switch之后回车被你switch里的getchar()函数接收掉了
飞天御剑流 2009-06-21
  • 打赏
  • 举报
回复
楼主的问题是前面input函数用scanf输入后留下的回车导致的,前面留下的回车给后面的getchar()读取了,结果就到了default那里去了,代码修改一下就好了:


printf("Please choise (0-4): \n");

while(choice!='0')
{
while( getchar() != '\n' ); //清除残留的\n
choice=getchar();
switch(choice)
{case '1':
aver_stu(score,N,M); break;
case '2':
aver_cour(score
laneast 2009-06-21
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 arong1234 的回复:]
那应该是后执行一次default吧
引用 7 楼 laneast 的回复:
getchar() 只接收了一个字符, 输入的时候还会有回车, 所以下一次循环的时候接收到了回车, 执行了一次 default

[/Quote]

在 while 之前还有其它的输入, 第一的 default 是因为之前输入留下的回车符的影响
Arnold9009 2009-06-21
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 ameyume 的回复:]
fflush(stdin); // 前面清空一下标准输入
choice=getchar();
这样就可以正确判断char了
[/Quote]

fflush(stdin);----这种用法不是太好,最好用循环来实现
ameyume 2009-06-21
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 lingyin55 的回复:]
switch可以用于整型和字符


引用 6 楼 nyeying 的回复:
switch是不能判断字符的
还有,你的case后没有break
[/Quote]
break有,但函数声明的时候有点问题,二维数组必须指定第二维的大小,修改后如下:
  void input(int [][M],int,int); 
void aver_stu(int [][M],int,int);
void aver_cour(int [][M],int,int);
void orde_aver(int [][M],int,int);
void failed(int [][M],int,int);
lingyin55 2009-06-21
  • 打赏
  • 举报
回复
switch可以用于整型和字符

[Quote=引用 6 楼 nyeying 的回复:]
switch是不能判断字符的
还有,你的case后没有break
[/Quote]
ameyume 2009-06-21
  • 打赏
  • 举报
回复
fflush(stdin); // 前面清空一下标准输入
choice=getchar();
这样就可以正确判断char了
nlylidb 2009-06-21
  • 打赏
  • 举报
回复
这种事十有八九是getchar接到的是个回车
arong1234 2009-06-21
  • 打赏
  • 举报
回复
为什么switch不能判断字符?人家每个分支都有break
[Quote=引用 6 楼 nyeying 的回复:]
switch是不能判断字符的
还有,你的case后没有break
[/Quote]
arong1234 2009-06-21
  • 打赏
  • 举报
回复
那应该是后执行一次default吧
[Quote=引用 7 楼 laneast 的回复:]
getchar() 只接收了一个字符, 输入的时候还会有回车, 所以下一次循环的时候接收到了回车, 执行了一次 default
[/Quote]
laneast 2009-06-21
  • 打赏
  • 举报
回复
getchar() 只接收了一个字符, 输入的时候还会有回车, 所以下一次循环的时候接收到了回车, 执行了一次 default
nyeboy123 2009-06-21
  • 打赏
  • 举报
回复
switch是不能判断字符的
还有,你的case后没有break
arong1234 2009-06-21
  • 打赏
  • 举报
回复
我不赞同,getchar不会把'1'换成1,因此他绝对不可能是case 1,2,3,...的
建议楼主在default分支内加下面一行:

default:
printf("you have input %x", (int)choice);
看看进入default的到底是什么
[Quote=引用 1 楼 Loaden 的回复:]
case '1':
改成:
case 1:
[/Quote]
lingyin55 2009-06-21
  • 打赏
  • 举报
回复
把choice定义为int型
然后case后面不要用字符,直接用数字就行了。
你输入1的时候,取到的字符并不是‘1’
yupenglove 2009-06-21
  • 打赏
  • 举报
回复
我是初级程序员
老邓 2009-06-21
  • 打赏
  • 举报
回复
其他一样,你不是输入数字吗?
老邓 2009-06-21
  • 打赏
  • 举报
回复
case '1':
改成:
case 1:

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧