# Quick question

• 10-19-2011
zach48191
Quick question

Code:

``` for (i=0; i < MAXLEN &&  i!= EOF; i++)     {         printf("Enter numbers between 0-9 \n"); /* Asks the users for a number 0-9 */         number = getchar() - '0';                /* sets the string to a int */                     if (number >= '0' && number <= '9') /* looks for a number '0' - '9' */             {                 --i;             }             else                                /* gives a error message if not the write parameters */             {                 printf("invalid number\n");             }                        getchar();             array[i] = number;                    /* puts the number in the array */     }```
why will it not do the if/else function correctly?!

also I do not understand the point to state getchar() again after the if/else statement??

here is the whole code just for reference

Code:

```#include <stdio.h> #define MAXLEN 5    /* sets the length of the array */ main() {     int array[MAXLEN];  /* places the integeres in the array */     int i, sum, number;     for (i=0; i < MAXLEN &&  i!= EOF; i++)     {         printf("Enter numbers between 0-9 \n"); /* Asks the users for a number 0-9 */         number = getchar() - '0';                /* sets the string to a int */                     if (number >= '0' && number <= '9') /* looks for a number '0' - '9' */             {                 --i;             }             else                                /* gives a error message if not the write parameters */             {                 printf("invalid number\n");             }                        getchar();             array[i] = number;                    /* puts the number in the array */     }             for (i=0; i<MAXLEN && i!= EOF; i++)     {    sum += array[i]; /* will count the sum and store in the array */             printf("%d  ", array[i]); /* prints out the numbers til it gets to count 5 */     }     printf("= %d\n", sum); /* will print the sum */ } /*  expected output:  Enter numbers between 0-9 1 Enter numbers between 0-9 2 Enter numbers between 0-9 5 Enter numbers between 0-9 6 Enter numbers between 0-9 3 1  2  5  6  3  = 17 */```
• 10-19-2011
CommonTater
In the first code sample ... comment out line 4 and try it again.
• 10-19-2011
zach48191
It just made it go to invalid number first and every number after that it would not take and the output was 0 0 0 0 0 = 0

I can get it to work correctly without the else statement, but I want my program to be more idiot proof i guess and also the getchar() after the loop why is that needed?
• 10-19-2011
Salem
Why are questions always quick?

number = getchar() - '0'; /* sets the string to a int */
if (number >= '0' && number <= '9') /* looks for a number '0' - '9' */

You already did the -'0', so you should be testing for 0 to 9, not '0' to '9'
• 10-20-2011
zach48191
what do you mean by 0 to 9 and not '0' to '9'?? I thought that ' ' around a character defines it?
• 10-20-2011
Babkockdood
Quote:

Originally Posted by zach48191
what do you mean by 0 to 9 and not '0' to '9'?? I thought that ' ' around a character defines it?

getchar() - '0' reduces the ASCII value for the characters of 0 - 9 (48 - 57) to the actual values. '0', wrapped in single quotes, is the ASCII value for the character of '0', which is 48. 0 is zero. Since you already converted the ASCII value to the actual value with getchar() - '0', your if statement should be comparing integers, not characters.
• 10-20-2011
zach48191
Thanks for clearing that up makes perfect sense now... I cant believe it still worked even with them. It works great now and I added continue in the IF loop so that it will keep going. I took out the else statement, because it was too much trouble to get it to work correctly and if a user put in a 10 it would only count the 1. Was running into that problem so here is the working code, btw what would be the fix if they entered a 10, I tried to use a

Code:

``` if (number < 0 || number > 9 || number == (double)number);```
but it sadly did not work. Anyways here is my code it works now thought I might post it for others that might have trouble in the future

Code:

```#include <stdio.h> #define MAXLEN 5    /* sets the length of the array */ void getArray (int s[], int maxlen); void sumArray (int s[], int maxlen); main() {     int array[MAXLEN];  /* places the integeres in the array */     int array2[MAXLEN];     getArray (array, MAXLEN);    /* fills the array with the function getArray */     getArray (array2, MAXLEN);     sumArray (array, MAXLEN);    /* sums and prints with the function sumArray */     sumArray (array2, MAXLEN); } void getArray ( int s[], int maxlen ) {     int i, number;     for (i=0; i < MAXLEN &&  i!= EOF; i++)     {         printf("Enter numbers between 0-9 \n"); /* Asks the users for a number 0-9 */         number = getchar() - '0';                /* sets the string to a int */                     if (number < 0 || number > 9) /* looks to check to see if the number is 0 - 9 */             {                 --i;                 continue;             }                         getchar();             s[i] = number;                    /* puts the number in the array */     } } void sumArray ( int s[], int maxlen ) {     int i, sum;     for (i=0; i<MAXLEN && i!= EOF; i++)     {    sum += s[i]; /* will count the sum and store in the array */             printf("%d  ", s[i]); /* prints out the numbers til it gets to count 5 */     }     printf("= %d\n", sum); /* will print the sum */ } /*  expected output:  Enter numbers between 0-9 1 Enter numbers between 0-9 2 Enter numbers between 0-9 3 Enter numbers between 0-9 4 Enter numbers between 0-9 5 Enter numbers between 0-9 6 Enter numbers between 0-9 7 Enter numbers between 0-9 8 Enter numbers between 0-9 9 Enter numbers between 0-9 0 1  2  3  4  5  = 15 6  7  8  9  0  = 45 */```
• 10-21-2011
Salem
Well at the moment, you're just reading single digits in isolation, and ignoring everything else.

So if you type in say
123
or
1 2 3

You read the '1' and then what?
If the next character is a '2', then you've got a compound string which you need to read.

But if it's a space, then the '1' you read is just that, a single digit number.

You should also remove all the
&& i!= EOF
tests from your code, since they don't actually do anything useful.

The only place you could test EOF is like so.
int ch = getchar();
if ( ch == EOF ) break;