甲苯的分子式:编程的问题

来源:百度文库 编辑:查人人中国名人网 时间:2024/04/30 07:21:36
帮忙看看哪错了?
悬赏分:5 - 离问题结束还有 14 天 11 小时
我编了个程序,关于8 10 16进制换算的,如下:
main()
{
long x;
char i,a,b,c;
i=020;
printf ("10%ca\n",i);
printf (" 8%cb\n",i);
printf ("16%cc\n",i);
if (getchar ()=='a')
{
printf ("To 8,Press m\n");
printf ("To 16,Press n\n");
if(getchar ()=='m')
{
printf ("Please give me your number\n");
scanf ("%d",&x);
printf ("%o",x);
}
else if(getchar ()=='n')
{
printf ("Please give me your number\n");
scanf ("%d",&x);
printf ("%x",x);
}
else
printf ("error\n");
}
else if (getchar ()=='b')
{
printf ("To 10,Press m\n");
printf ("To 16,Press n\n");
if (getchar ()=='m')
{
printf ("Please give me your number\n");
scanf ("%o",&x);
printf ("%d",x);
}
else if (getchar ()=='n')
{
printf ("Please give me your number\n");
scanf ("%o",&x);
printf ("%x",x);
}
else
printf ("error\n");
}
else if (getchar ()=='c')
{
printf ("To 10,Press m\n");
printf ("To 16,Press n\n");
if (getchar ()=='m')
{
printf ("Please give me your number\n");
scanf ("%x",&x);
printf ("%d",x);
}
if (getchar ()=='n')
{
printf ("Please give me your number\n");
scanf ("%x",&x);
printf ("%o",x);
}
else
printf ("error\n");
}
else
printf ("error\n");

}
帮忙看一下哪错了?
怎么不好用?
谢了

程序在turboC以上版本必须包含头文件
C语言是stdio.h
C++有 iostream(没有.h) cstdio(没有.h) 并且声明using namespace std;
例如: #include <iostream>
using namespace std;
getchar()也能直接嵌套在if语句中,但不能做左值,
在C++可以用cin.get()

1、if,else的条件判断里面不能直接写getchar(),这样的话,每个getchar()都要求输入,不是楼主想像的只输入一个字符来做分支。应该用char select;
select =getchar();if(select=='a').....else if(select=='b').....这样的方式。

2、回车实际上是两个字符,因此有多个getchar()时,后面的getchar()会得到前面输入时的回车的第2个字符,所以每次要求输入字符前,最好做一下清空缓冲区,用fflush(stdin)。