### Thread: Simple C program, trouble with arrays, pointers, strings

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2013
Posts
5
Rep Power
0

#### Simple C program, trouble with arrays, pointers, strings

First of all, I'm very new in C programming. Currently I need to write a simple program which converts a decimal number to the color code for resistances or capacitors in electrical circuits. For example: I input the number "4700", and it should print on the screen the color code which is yellow (4) violet (7) red (exp. 2). This is what I have so far:
Code:
```    int nDecimalcounter = 0;
double dInput;
char szColor[10][7] = {"black", "brown", "red", "orange", "yellow",
"green", "blue", "violet", "grey", "white"};

printf("Give decimal value: ");
scanf("%lf", &dInput);

while (dInput > 99) {

dInput = dInput / 10;
nDecimalcounter = nDecimalcounter + 1;
}```
So if I for example enter the number 4700, I'm left with an int value for decimalcounter, and a (double with the first 2 characters in it.
So here are my 2 problems:
1. I need to separate the 2 characters in the double, I need to end up with (double) 4 and (double) 7.
2. Once I have 3 separate numbers, I don't know how to assign the correct colors to each separate number, and print these on the screen (instead of the numbers 4, 7, and 2). I'm thinking I need to use pointers or something like that but it confuses me.

Can anyone help me with this? (even the smallest suggestions are welcome)
2. If you are so new to C programming, why not switch to Python? You'll be new to it too, but it will appear sometimes easier and more handy for your purposes (I do not mean C is bad).

1. I need to separate the 2 characters in the double, I need to end up with (double) 4 and (double) 7.
Number to digits

Strictly speaking it is almost the same what you've done with decimalCounter already...

Other idea is to use sprintf or ftoa to convert value to string and fetch characters from here.

2. Once I have 3 separate numbers, I don't know how to assign the correct colors to each separate number
Why? Array is just the thing invented for fetching something by index. In your case it is something like:

Code:
```digit = 4;
printf("%s", szColor[digit]);```

even the smallest suggestions are welcome
Most sincere (and important) suggestion is - try to learn the language in bit more systematic way. For example lay your hands on book by Kernigan and Ritchie. Also I dare recommend to practice on simpler problems (like these ) before approaching more complex.
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2013
Posts
5
Rep Power
0