首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

迷糊了,大家帮帮忙啊该如何解决

2012-02-12 
迷糊了,大家帮帮忙啊#includestdio.hmain(){charnum1,num2,num3printf( pleaseenterthefirstletteroft

迷糊了,大家帮帮忙啊
#include   <stdio.h>

main()
{char   num1,num2,num3;
printf( "please   enter   the   first   letter   of   the   weeknumber:\n ");
scanf( "%c ",&num1);
  if(num1   == 't '||num1   == 'T '||num1   == 's '||num1   == 'S ')
      {
      printf( "please   enter   the   second   letter   of   the   weeknumber:\n ");
      scanf( "%c ",num2);
      num3=num1+num2;
        switch(num3)
            {
              case   (201):
              case   ( 't '+ 'u '):   printf( "Tuesday ");break;
              case   ( 'T '+ 'h '):
              case   ( 't '+ 'h '):   printf( "Thursday ");break;
            }
      }
      else
          switch(num1)
          {
          case   'M ':
          case   'm ':   printf( "Mondayn\n ");break;
          case   'W ':
          case   'w ':   printf( "Wednesday\n ");break;
          case   'F ':
          case   'f ':printf( "Friday\n ");break;
          }
        return   0;
}
请大家看看这个输入星期字头输出星期的程序存在什么问题,为什么在输入S、s、T、t时存在问题

[解决办法]
问题在于scanf之后缓冲区没有刷新,我在下面改好了
另外num3要定义为int,才能用来比较.
程序在下面,可以运行,没有问题

#include <stdio.h>

int main()
{
char num1,num2;
int num3; //设置为int型,用来比较ASCII码
printf( "please enter the first letter of the weeknumber:\n ");
scanf( "%c ",&num1);

if(num1 == 't '||num1 == 'T '||num1 == 's '||num1 == 'S ')
{
printf( "please enter the second letter of the weeknumber:\n ");

fflush(stdin);//必须加上 这一句,来刷新缓冲区,才能读入下面的num2

scanf( "%c ",&num2);
num3=num1+num2;
switch(num3)
{
case ( 'T '+ 'U '):
case ( 'T '+ 'u '):
case ( 't '+ 'U '):
case ( 't '+ 'u '): printf( "Tuesday ");break;
case ( 'T '+ 'h '):
case ( 'T '+ 'H '):
case ( 't '+ 'H '):
case ( 't '+ 'h '): printf( "Thursday ");break;
}
}
else
switch(num1)
{
case 'M ':
case 'm ': printf( "Mondayn\n ");break;
case 'W ':
case 'w ': printf( "Wednesday\n ");break;
case 'F ':
case 'f ':printf( "Friday\n ");break;
}
return 0;
}

热点排行