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