我正在尝试制作一个程序,让用户可以从他们想要转换的测量列表中进行选择 到目前为止,我的代码是:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int sv, SourceUnit, du, mb, kb, by;
printf("--------------------------------------------------0x0 Menu:------ ---------------------------------\n");
printf(" Please Select from the following options (enter number exactly when prompted for source units!\n");
printf("1: Kilo -> Mega\n2: Mega -> Kilo\n10. Bits -> Bytes\n ");
printf("--------------------------------------------------------------------------------------------------\n");
printf("Enter source value: ");
scanf("%d", &sv);
printf("Enter source unit: ");
scanf("%s", &SourceUnit);
if (SourceUnit == 1) //convert kilobytes to megabytes
{
mb = (sv / 1024);
printf("%d Kb == %d Mb\n", sv, mb);
}
else if (SourceUnit == 2) //convert megabytes to kilobytes
{
kb = (sv / 1024);
printf("%d Mb == %d Kb\n", sv, kb);
}
else if (SourceUnit == 10) //bits to bytes
{
by = (sv / 8);
printf("%d Bits == %d Bytes\n", sv, by);
}
else
{
printf("Please Choose from the menu options to convert!\n");
}
return(0);
}
它在GCC上编辑得很好。我得到的输出是:
---------------------------------------0x0 Menu:----------------------------
1. Kilo -> Mega
2. Mega -> Kilo
3. Bits -> Bytes
----------------------------------------------------------------------------
Enter Source Value: (Example I will type in 30 for the number to be converted) 30
Enter Source Unit: (Example will be 1 to convert from Kilo to Mega) 1
Please Choose from the menu options to convert!
出于某种原因,当我不想要它时,我的其他声明会出现......导致此错误的原因是什么?
此外,我还有一些问题需要将deka转换为dibi ..具体可以这样做吗? 我知道一个deka是10个小组,而dibi是16个小组,但这是我能读到的所有信息..
答案 0 :(得分:0)
您的scanf格式说明符错误。应该是&#34;%d&#34;。这导致了这个问题。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int sv, SourceUnit, du, mb, kb, by;
printf("--------------------------------------------------0x0 Menu:------ ---------------------------------\n");
printf(" Please Select from the following options (enter number exactly when prompted for source units!\n");
printf("1: Kilo -> Mega\n2: Mega -> Kilo\n10. Bits -> Bytes\n ");
printf("--------------------------------------------------------------------------------------------------\n");
printf("Enter source value: ");
scanf("%d", &sv);
printf("Enter source unit: ");
scanf("%d", &SourceUnit); //This should be %d
if (SourceUnit == 1) //convert kilobytes to megabytes
{
mb = (sv / 1024);
printf("%d Kb == %d Mb\n", sv, mb);
}
else if (SourceUnit == 2) //convert megabytes to kilobytes
{
kb = (sv / 1024);
printf("%d Mb == %d Kb\n", sv, kb);
}
else if (SourceUnit == 10) //bits to bytes
{
by = (sv / 8);
printf("%d Bits == %d Bytes\n", sv, by);
}
else
{
printf("Please Choose from the menu options to convert!\n");
}
return(0);
}