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

使用scanf函数时碰到的有关问题,求高手解答

2012-05-16 
使用scanf函数时碰到的问题,求高手解答[size11px]写了一个switch测试的小程序,目的是实现输入一个数字加

使用scanf函数时碰到的问题,求高手解答
[size=11px]写了一个switch测试的小程序,目的是实现输入一个数字加符号,输出十进制,八进制,十六进制的输出,编译通过没问题,但是输出完会有段错误,错误提示是Program received signal SIGSEGV, Segmentation fault.0x00000000 in ?? ()
,那位高手帮忙解答下啊
#include <stdio.h>
int main(int ac,char* av[])
{
  unsigned short x = 0;
  char c='a';
  printf("input a num\n");
  scanf("%d,%c",&x,&c);
  //getchar();
  //c=getchar();
  switch(c)
  {
  case 'D':
  printf("%dD\n",x);
  break;
  case 'O':
  printf("%oO\n",x);
  break;
  case 'X':
  printf("%xX\n",x);
  break;
  default:
  printf("input error!\n");
  }
  return 0;

}
[size=12px][/size][size=14px][/size][/size]

[解决办法]
@linux-008:~/test/test> ./a.out 
input a num
100,X
64X
@linux-008:~/test/test> ./a.out 
input a num
100,o
input error!
@linux-008:~/test/test> ./a.out 
input a num
100,O
144O
@linux-008:~/test/test> ./a.out 
input a num
100,D
100D

gcc 测试,没有什么问题。

[解决办法]
scanf("%d,%c",&x,&c);
==>
scanf("%d%*[^A-D]%c",&x,&c);

[解决办法]
scanf("%d,%c",&x,&c);
估计这句,他输入的格式不对,没加,

热点排行