1
我想做一個程序,讓用戶選擇從他們想要轉換的度量列表中進行選擇。
到目前爲止,我的代碼,我已經是:如果語句問題,不顯示正確的輸出
#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語句顯示出來的時候,我不希望它to..what導致這個錯誤?
此外,我有一些問題,我需要將德卡轉換爲dibi ..如何完全可以做到這一點? 我知道一個deka是一個10的組,而一個dibi是一個16的組,但這是我可以讀取的所有信息。
爲什麼你使用%d作爲第一個int,使用%s作爲第二個? –
gcc output:「temp.c:17:5:warning:格式'%s'需要'char *'類型的參數,但參數2的類型是'int *'」,爲什麼沒有讀取? – Tacet
不錯,它不能在gcc上編譯得很好編譯時始終啓用所有的警告,然後修復這些警告。 (至少''gcc'至少可以使用:'-Wall -Wextra -pedantic'我也使用:'-Wconversion -std = gnu99')這導致了兩條警告:1)未使用的變量:'du' 2) 'scanf(「%s」,&SourceUnit);'格式'%s'需要類型爲'char *'的參數,但參數2的類型爲'int *'不正確的參數類型是需要糾正的主要問題。也許你的意思是:'scanf(「%d」,&SourceUnit);' – user3629249