2013-04-08 37 views
0

我試圖讓一個程序在Windows控制檯中啓動,它會詢問電阻中的「第一個帶的顏色是什麼」,所以當你輸入諸如「red 「它將輸入一個2到浮動中。我遇到的問題是,我不僅無法弄清楚如何將「紅色」變成「2」,而且每當我嘗試在控制檯上輸入文本時,它都會顯示其餘的printf並跳到最後。轉換爲數字的字的輸入C

這是我到目前爲止,我評論了第三和第四個輸入,所以我可以更容易地排除故障。

//Color bands 

#include <stdio.h> 

int 
main(void) 
{ 
    double first; 
    double second; 
    //double third; 
    //double fourth; 
    double total_resist; 
    double black, brown, red = 2, orange, yellow, green, blue, violet, gray, white; 

    black = 0; 
    brown = 1; 
    red = 2; 
    orange = 3; 
    yellow = 4; 
    green = 5; 
    blue = 6; 
    violet = 7; 
    gray = 8; 
    white = 9; 

    printf("Input first color band> "); 
    scanf("%lf", &first); 

    printf("Input second color band> "); 
    scanf("%lf", &second); 

    total_resist = first + second;// + second + third + fourth; 

    printf("\nTotal resistance is %.lf\n", total_resist); 

    return(0); 
} 
+2

'scanf'永遠不會將字符串'red'轉換爲float'2'。你需要自己做。 – Bechir 2013-04-08 10:13:27

+0

你需要做一個查找表。顏色代碼反正是固定的。你沒有考慮到精確度?也許你需要 – 2013-04-08 10:18:20

回答

0

如何:

char color[20]; 

printf("Input first color band> "); 
scanf("%19s", color); 

if (!strcmp(color, "red")) 
    first = 2.0; 
else if (...) 

顯然下一步就是讓一個函數:

double color2number(const char *name) 
+0

作品,非常感謝你 – user2257073 2013-04-08 10:44:55

+1

而創建一個函數後的步驟是使用數組作爲名稱和數字,所以你迭代而不是10個有效的替代品列表。 – 2014-03-03 01:24:00

0

這應該做的工作:

#include <stdio.h> 



int color2nr(char* color){ 
if(color=="red") 
    return red; 
else if(color=="orange") 
    return orange; 
... 
} 

照此所有其他顏色的東西...

main(void) 
{ 
double first; 
double second; 
double third; 
double fourth; 
double total_resist; 
double black, brown, red = 2, orange, yellow, green, blue, violet, gray, white; 

black=0; 
brown=1; 
red = 2; 
orange = 3; 
yellow = 4; 
green = 5; 
blue = 6; 
violet = 7; 
gray = 8; 
white = 9; 

char[10] buf; 
printf("Input first color band> "); 
scanf("%s", buf); 
double first=color2nr(buf); 

printf("Input second color band> "); 
scanf("%s", buf); 
double second=color2nr(buf); 


total_resist = first + second;// + second + third + fourth; 


printf("\nTotal resist is %.lf\n", total_resist); 



return(0); 
} 
0
1) try using **enum** 

例如:
typedef enum {black,brown,red...}colorcodes;

變量聲明爲這樣..

colorcodes FstColor, SndColor; 

2) `scanf("%s\n",FstColorStr)` and not `scanf("%lf",bSndColorStr);` 

3) if(strcmp(FstColorStr,"red")) FstColor = red and so on for all other colors. 

4) tot_resist = FstColor * pow(10,SndColor); //include math.h 
1
#include <string.h> 
#include <stdio.h> 

static int colour_value(char const *colour) 
{ 
    const char *band[] = 
    { 
     "black", "brown", "red", "orange", "yellow", 
     "green", "blue", "violet", "grey", "white" 
    }; 
    enum { NUM_BANDS = sizeof(band)/sizeof(band[0]) }; 
    for (int i = 0; i < NUM_BANDS; i++) 
    { 
     if (strcmp(colour, band[i]) == 0) 
      return i; 
    } 
    return -1; 
} 

int main(void) 
{ 
    double total_resistance = 0.0; 
    const char *ordinals[] = { "zeroth", "first", "second", "third", "fourth" }; 
    char colour[32]; 
    char band[4][8]; 
    int i; 

    for (i = 0; i < 4; i++) 
    { 
     int value; 
     printf("Input %-6s color band> ", ordinals[i + 1]); 
     if (scanf("%s", colour) != 1) 
      break; 
     if ((value = colour_value(colour)) == -1) 
      break; 
     total_resistance = total_resistance * 10.0 + value; 
     strcpy(band[i], colour); 
    } 

    if (i == 4) 
    { 
     printf("%s", "Bands: "); 
     for (i = 0; i < 4; i++) 
      printf(" %s", band[i]); 
     printf("\nTotal resistance is %.lf\n", total_resistance); 
    } 
    else 
     puts("Oops!"); 

    return(0); 
} 

實施例運行:

Input first color band> orange 
Input second color band> green 
Input third color band> violet 
Input fourth color band> brown 
Bands: orange green violet brown 
Total resistance is 3571 


Input first color band> red 
Input second color band> yellow 
Input third color band> black 
Input fourth color band> blue 
Bands: red yellow black blue 
Total resistance is 2406