# Thread: I took rstanley's advice. Bought a book on C. Now I'm stuck a int vs char.

1. ## I took rstanley's advice. Bought a book on C. Now I'm stuck a int vs char.

I decided to take #rstanley's advice and purchased the book he recommended. C Programming a modern approach which doesn't give the answers to all the questions. So I'm forced to struggle which I don't mind but, I've hit a wall and if there is a google search that will give me the answer, I can't find it.

This program is a work in progress. In short:
If I use:
Code:
```number=3;     // it works
number=arr[j];    // it doesn't work. even though arr[] contains int values. Probably the answer is obvious to you but, I'm completely beaten on this.```
This is commented in the code below.

Code:
```// trying to complete chap 10 #7 on page 239 of Book: C Programming, A Modern Approach 2nd Edition by K.N. KING
// above builds on chapt 8 ex 6 on page 177

#include <stdbool.h>        // required to declare with bool
#define MAX_DIGITS 10

int arr[MAX_DIGITS];
int k=0;
const int segments[10][7]={
{1,1,1,1,1,1,0},    // 0
{0,1,1,0,0,0,0},    // 1
{1,1,0,1,1,0,1},    // 2
{1,1,1,1,0,1,0},    // 3
{0,1,1,0,0,1,1},    // 4
{1,0,1,1,0,1,1},    // 5
{1,0,1,1,1,1,1},    // 6
{1,1,1,0,0,0,0},    // 7
{1,1,1,1,1,1,1},    // 8
{1,1,1,1,0,1,1}};   // 9
int digits[4][MAX_DIGITS];  // Directions aren't clear. p240 says multiply MAX_DIGITS * 4.  Not using yet anyway.

void clear_digits_array(void);                  // store blank characters into all elements of the digits array
void process_digit(int digit, int position);    // stores the 7 segment representation of digit into a specified position in the digits array
void print_digits_array(void);                  // displays the rows of the digits array, each on a single line

int main(){
int ch[1];
clear_digits_array();

printf("Enter a 7 digit phone number (E.g. 123-4567): ");
while(k<MAX_DIGITS){
scanf("%c", &ch[0]);
if(ch[0]=='0' || ch[0]=='1' || ch[0]=='2' || ch[0]=='3' || ch[0]=='4' || ch[0]=='5' || ch[0]=='6' || ch[0]=='7' || ch[0]=='8' || ch[0]=='9'){
process_digit(ch[0], k);   // pass the int and the counter to function
}else if(ch[0]=='\n'){
break;
}
++k;
}
print_digits_array();
}

void clear_digits_array(void){
for(int j=0;j<MAX_DIGITS;j++){
arr[j]='\0';
}
}

void process_digit(int digit, int position){
arr[position]=digit;
printf("arr[%d] = %c\n", position, arr[position]);
}

void print_digits_array(void){
int number, numLess, num;
for(int i=0;i<3;i++){
for (int j=0;j<k+1;j++){
number=arr[j];                                  // HERE IS THE PROBLEM: change arr[j] to 3 and it works. it seems that the variable 'number' is NOT AN INT!
//num = atoi(number);                           // doesn't work
numLess=number-1;
printf("segments[%c][0] = %d\n",numLess, segments[numLess][0]); // BUG IS HERE: numLess prints as int but, isn't int.
if(i==0){
if(segments[number][0]==1){printf("  _");}
}
if(i==1){
if(segments[number][5]==1){printf(" |");}
if(segments[number][6]==1){printf("_");}
if(segments[number][1]==1){printf("|");}
}

if(i==2){
if(segments[number][4]==1){printf(" |");}
if(segments[number][3]==1){printf("_");}
if(segments[number][2]==1){printf("|");}
}
}
}
}```
With
Code:
`number=3;`
It works (see in red):
Enter a 7 digit phone number (E.g. 123-4567): 678
arr[0] = 6
arr[1] = 7
arr[2] = 8
segments[☻][0] = 1
_segments[☻][0] = 1
_segments[☻][0] = 1
//rest not shown...

With
Code:
`number=arr[j];`
It doesn't work (see in red):
Enter a 7 digit phone number (E.g. 123-4567): 678
arr[0] = 6
arr[1] = 7
arr[2] = 8
segments[5][0] = 0
segments[6][0] = 0
segments[7][0] = 0
//rest not shown...

I declared arr[] as an int. I assume that it only contains int values but, when I grab a value out of the array, it seems to assume that it is a char or something. What am I not understanding.

I know there are lots of bugs especially in the function print_digits_array() but, this is a WIP and I need to figure out the other bugs on my own. I'm sure it's hard to look at this code and not wince.

Any insight is appreciated.

Sincerely,

Michael

2. > arr[position]=digit;
This needs to be
arr[position]=digit - '0';

The numeric characters '0' to '9' have ASCII values from 48 to 56.
But you want the actual numeric values 0 to 9 to index your arrays.

3. Thank you! I never would have figured this out. I'm confused on how you knew to add that code.... Yes, you are correct regarding:
Originally Posted by Salem
>
you want the actual numeric values 0 to 9 to index your arrays.
If you don't mind my asking, what is happening when I insert
Code:
` - '0'`
into arr[position]=digit - '0';?

BEFORE: when I print arr[] values with %c I get what I want.

AFTER: with the addition of your code, I can print out the values with %d.

To me this is sort of a crazy problem because I declared the array as an int.
Code:
`int arr[MAX_DIGITS];`
Did I need to add your code because when I use scanf, I read them in with %c? Using scanf with %d didn't work:

Code:
`scanf("%c", &ch[0]);`
Sorry to beat a dead horse. Dealing with the format of unseen data has been very hard for me to learn. Silly miss understandings like this will chew up my entire day. Not to mention the number of times I stand up, circle the floor and rub my hands on my face with almost overwhelming anxiety.

4. > Did I need to add your code because when I use scanf, I read them in with %c? Using scanf with %d didn't work:
%c reads characters and stores them as you typed them.
If you need to convert the result in some way, then you have to write the code.

%d on the other hand does the "1234" to 1234 conversion for you automatically, storing the result in an integer.

> If you don't mind my asking, what is happening when I insert
The array contains characters in the range '0' to '9'.

In ASCII, the numeric characters are consecutive. It's actually required by the C standard.
So just subtracting '0' turns '0' into 0, '1' into 1, '2' into 2 etc.

This is what %d will be doing behind the scenes.

5. awesome. thank you for the solution.