# Reading Separate Digits as Variables

This is a discussion on Reading Separate Digits as Variables within the C Programming forums, part of the General Programming Boards category; Hello, I am currently taking an intro course to C programming, and have a few questions about an assignment I'm ...

1. ## Reading Separate Digits as Variables

Hello, I am currently taking an intro course to C programming, and have a few questions about an assignment I'm working on. I've Googled for this, and searched these forums, but have not been able to find anything to help thus far.

The program I have to write has to accept a five digit integer as input, print out each digit with three spaces between them, and then print the largest and smallest digit. After searching, I was able to read the digits and print them like this:

Code:
```int c1;
int c2;
int c3;
int c4;
int c5;

printf( "Input a five-digit integer:\n" );
scanf( "%c%c%c%c%c", &c1, &c2, &c3, &c4, &c5 );

printf( "\nThe separated digits are:\n" );
printf( "%c   %c   %c   %c   %c\n", c1, c2, c3, c4, c5 );```
This works fine, except that I next need to use Boolean logic to find the greatest and least digit. I played with a few different ways to get the smallest and largest digits, but none of them seemed to work (ie, they'd report the wrong number as being the largest). Finally, I tried to debug things by just using one if statement to compare the first two integers, and assign the value of the greater one to the variable greatest (which has its own int greatest;, which is not shown in the listing above).

Code:
```if ( c1 > c2 ) {
greatest = c1;
}
else greatest = c2;

printf( "Greatest: %c\n", greatest )```
The code above did not give any compile errors (with gcc on a Linux system, if it matters). However, no matter if c1 > c2 is true or not, the variable greatest always gets assigned the value of c1. So, if c2 is greater, greatest still gets the value of c1.

After looking at it, I realized that it might have something to do with using %c rather than %d; ie, it was looking for ASCII rather than integers. (I think; correct me if I'm wrong.) So, I'm thinking this could be why the conditional wasn't working. I'm reading up on the atoi function. I attempted it as follows, first creating a new set of variables set up as character strings, for instance:

Code:
```char c1;   /*I used one for each of the c variables respectively.*/
int d1;     /*I used this for the integer values.*/

d1 = atoi( c1);```
I then used the d variables (d1, d2, etc.) in the conditionals, and compiled. However, I got compilation errors:

Code:
`digits.c:41: warning: passing arg 1 of `atoi' makes pointer from integer without  a cast`
I had one of those for each of the five atoi functions I used.

Any thoughts on this? I have a few more days for this program, and so will continue to mess with it. Thanks for any insight!

2. Originally Posted by linuxpyro
Any thoughts on this? I have a few more days for this program, and so will continue to mess with it. Thanks for any insight!
atoi, the worst of the text-to-number conversion functions, expects a null-terminated string -- which a single character can never be. Read as a string and (attempt to) convert each character to a digit using cn - '0'.

And perhaps post a small but complete snippet that demonstrates the issue.

3. Use loop for input five integers, and store them in integer array.
Then, again loop with printf to display them with spaces. In the same loop you can find min and max.

4. And logic like this may make more sense:
Code:
```            int digit = text[i] - '0';
if ( digit > hi )
{
hi = digit;
}
if ( digit < lo )
{
lo = digit;
}```

5. Originally Posted by Dave_Sinkula
atoi, the worst of the text-to-number conversion functions, expects a null-terminated string -- which a single character can never be. Read as a string and (attempt to) convert each character to a digit using cn - '0'.

And perhaps post a small but complete snippet that demonstrates the issue.
Could you just show me an example of this? I tried the following, but got errors when compiling:

Code:
`int d1 = cn[c1] - '0';`
I'm sorry; once again I am new to C, and I couldn't find anything on that by searching.

6. Do you actually have a variable called cn? It helps if you actually post what your error is.

Quzah.

7. This is the error I was getting:

Code:
```digitsold.c:36: error: `cn' undeclared (first use in this function)
digitsold.c:36: error: (Each undeclared identifier is reported only once
digitsold.c:36: error: for each function it appears in.)```
I thought cn was a function; I was trying to convert the character to an integer.

Anyway, I talked to my professor, and after mentioning just reading each digit, he said the he would rather I used division. So, I have the program done and working fine. Thanks for the help thus far. I still wouldn't mind setting up the program my way for practice, though, and will post here when I get it worked out.

8. Originally Posted by linuxpyro
Could you just show me an example of this? I tried the following, but got errors when compiling:
Code:
```#include <stdio.h>
#include <ctype.h>
#include <limits.h>

int main(void)
{
char source[] = "hello world";
char destination[sizeof source / sizeof source];
int hi = INT_MIN, lo = INT_MAX;
char text[10];
fputs("Input a five-digit integer: ", stdout);
fflush(stdout);
if ( fgets(text, sizeof text, stdin) != NULL )
{
int i;
for ( i = 0; text[i] != '\0'; ++i )
{
if ( isdigit(text[i]) )
{
int digit = text[i] - '0';
if ( digit > hi )
{
hi = digit;
}
if ( digit < lo )
{
lo = digit;
}
}
}
}
printf("hi = %d, lo = %d\n", hi, lo);
return 0;
}

/* my output
Input a five-digit integer: 15243
hi = 5, lo = 1
*/```