这是我的代码
#include <stdio.h>
#include <math.h>
#include <string.h>
int main()
{
int o;
int menu;
printf("\n\t\t\t\t _________________________________________\n");
printf("\t\t\t\t| CONVERTER |\n");
printf("\t\t\t\t|=========================================|\n");
printf("\t\t\t\t| | |\n");
printf("\t\t\t\t| 1 | Decimal to Binary |\n");
printf("\t\t\t\t|_________|_______________________________|\n");
printf("\t\t\t\t| | |\n");
printf("\t\t\t\t| 2 | Binary to Decimal |\n");
printf("\t\t\t\t|_________|_______________________________|\n");
printf("\t\t\t\t| | |\n");
printf("\t\t\t\t| 3 | Octal to Binary |\n");
printf("\t\t\t\t|_________|_______________________________|\n");
printf("\t\t\t\t| | |\n");
printf("\t\t\t\t| 4 | Octal to Decimal |\n");
printf("\t\t\t\t|_________|_______________________________|\n");
printf("\t\t\t\t| | |\n");
printf("\t\t\t\t| 5 | Decimal to Octal |\n");
printf("\t\t\t\t|_________|_______________________________|\n");
printf("\t\t\t\t| | |\n");
printf("\t\t\t\t| 6 | Hexa to Decimal |\n");
printf("\t\t\t\t|_________|_______________________________|\n");
printf("\t\t\t\t| | |\n");
printf("\t\t\t\t| 0 | Quit |\n");
printf("\t\t\t\t|_________|_______________________________|\n");
printf("\n");
for(o=1;o<=50;o++){
printf("\n\t\t\t\t Please select\n\n\t\t\t\t ");
menu = getchar();
switch (menu)
{
case '1':{
long int decimalNumber,quotient;
int binaryNumber[100],i=1,j;
printf("\n\t\t\t\tEnter any decimal number: ");
scanf("%ld",&decimalNumber);
printf("\n");
quotient = decimalNumber;
while(quotient!=0){
binaryNumber[i++]= quotient % 2;
quotient = quotient / 2;}
printf("\t\t\t\tEquivalent binary value is: ");
for(j = i -1 ;j> 0;j--){
printf("%d",binaryNumber[j]);
}
printf("\n");
// o+= o + 1;
}
break;
case '2':{
long int binary1;
printf("\n\t\t\t\tEnter any Binary Number:\n\n\t\t\t\t");
scanf("%ld",&binary1);
printf("\n\t\t\t\tEquivalent binary value is: ");
int decimal=0;
int i=0;
int rem;
while(binary1!=0)
{
rem = binary1%10;
binary1/=10;
decimal += rem*pow(2,i);
++i;
}
printf("\n\n\t\t\t\t%d\n", decimal);
}
break;
case '3':{
long int octal1;
printf("\n\t\t\t\tEnter any Octal number: ");
scanf("%ld", &octal1);
int decimal=0, binary=0, i=0;
printf("\n\t\t\t\tThe Equivalent Binary Value is: ");
while(octal1!=0)
{
decimal+=(octal1%10)*pow(8,i);
++i;
octal1/=10;
}
i=1;
while(decimal!=0)
{
binary+=(decimal%2)*i;
decimal/=2;
i*=10;
}
printf("%d\n", binary);
}
break;
case '4':{
long int octal2;
printf("\n\t\t\t\tEnter any Octal number: ");
scanf("%ld", &octal2);
int decimal2=0, i=0;
printf("\n\t\t\t\tThe Equivalent Decimal Value is: ");
while(octal2!=0)
{
decimal2+=(octal2%10)*pow(8,i);
++i;
octal2/=10;
}
printf("%d\n", decimal2);
}
break;
case '5':{
long int decimal2,quotient2;
printf("\n\t\t\t\tEnter any decimal number: ");
scanf("%ld",&decimal2);
int octal3[100],w=1,e;
quotient2 = decimal2;
while(quotient2!=0){
octal3[w++]= quotient2%8;
quotient2 = quotient2 / 8;
}
printf("\n\t\t\t\tEquivalent octal value is: ");
for(e=w-1;e>0;e--)
printf("%d", octal3[e]);
printf("\n");
}
break;
case '6':{
unsigned int q;
printf("\n\t\t\t\tEnter any Hexadecimal number: ");
scanf("%x", &q);
printf("\nThe Equivalent decimal value is: ");
printf("%d\n", q);
}
break;
case '0':{
printf("\n\t\t\t\t Are you sure?\n\n\t\t\t\t y or n: ");
char b[9];
scanf("%s", b);
if(strcmp(b,"y")==0){
o=o+50;
printf("\n\n\t\t\t\t Have a nice day!\n\n");}
}
break;
}
}
return 0;
}
获取菜单中某个项目的结果后,&#34;请选择&#34;在让您选择另一个菜单项之前,短语打印2次。
为什么要打印2次?
这是一个转换器程序,作为我的基本编程主题的项目。我们被告知制作一个转换这些数字的程序,转换一个数字后,该程序不应该结束,应该让你选择另一个选项或选择&#34;退出&#34;退出该计划。
答案 0 :(得分:0)
我复制了你的代码并进行了调试。似乎在选择一个选项并输入要转换的数字后,&#34; menu = getchar();&#34;变为10并且不会等待任何用户输入,而您的开关(菜单)中没有处理该值。考虑使用scanf,因为这对我来说很好。请记住,您将菜单声明为int而不是char,所以我想这一定是您遇到麻烦的原因......
使用
scanf("%ld", &menu);
并替换像这样的案例陈述..
case '1': // change to case 1:
case '2': // change to case 2:
等等..因为值将是int而不是char。