我正在尝试制作一个程序,为用户提供从他们想要转换的测量列表中进行选择的选项。
到目前为止,我的代码是:
#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!
由于某种原因,我的else语句在我不希望出现时出现。.什么导致此错误?
我也遇到一些问题,我需要将deka转换为dibi ..究竟该怎么做?我知道deka是10个一组,而dibi是16个一组,但这就是我可以从中读取的所有信息。
您的scanf的格式说明符错误。应该是“%d”。造成了问题。
#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);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句