As already pointed out in the comments section, the logic in your code will not work with codes that start with a zero.
If you store the code as an integer, then you cannot distinguish the code 01234
from the code 1234
. Therefore, if you want to be able to distinguish these two codes, you must, at least initially, read the number as a string, not as a number:
char buffer[100];
//prompt user for input
printf("Enter a 5 digit area code: ");
//attempt to read one line of input
if ( fgets( buffer, sizeof buffer, stdin ) == NULL )
{
fprintf( stderr, "error reading input!\n" );
exit( EXIT_FAILURE );
}
Note that the code above additionally requires the header file stdlib.h
to be included.
Now, you must count the number of characters entered and verify that there were exactly 5
. Also, you must verify that all entered characters were digits. If the input is not valid, then you must provide an appropriate error message and prompt the user again. This can be best accomplished using an infinite loop, which is repeated until the input is valid, at which point an explicit break
statement is executed, which will break out of the infinite loop.
Converting the individual digits to words can easily be done by creating an array of pointers to strings, so that the 0th element points to the string "zero"
, the 1st element points to "one"
, etc:
const char * const digit_names[] = {
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"
};
Since the ISO C standard guarantees that all numbers are consecutive in the character set (irrespective of which character set you are using), you can simply subtract '0'
(the character code of the digit 0
) from the character code in order to convert the digit from a character code to an actual number between 0
and 9
. Now, you can use that number to index into the array digit_names
to print the corresponding word.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main( void )
{
const char * const digit_names[] = {
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"
};
char buffer[100];
for (;;) //infinite loop, equivalent to while(1)
{
int i;
char *p;
//prompt user for input
printf("Enter a 5 digit area code: ");
//attempt to read one line of input
if ( fgets( buffer, sizeof buffer, stdin ) == NULL )
{
fprintf( stderr, "Unrecoverable error reading input!\n" );
exit( EXIT_FAILURE );
}
//verify that entire line of input was read in
p = strchr( buffer, '\n' );
if ( p == NULL )
{
fprintf( stderr, "Unrecoverable error: Line too long!\n" );
exit( EXIT_FAILURE );
}
//remove newline character
*p = '\0';
//verify that exactly 5 characters were entered and that
//each characters is a digit
for ( i = 0; i < 5; i++ )
{
//verify that we are not yet at end of line
if ( buffer[i] == '\0' )
{
printf( "Too few characters!\n" );
//we cannot use "continue" here, because that would apply to
//the innermost loop, but we want to continue to the next
//iteration of the outer loop
goto continue_outer_loop;
}
//verify that character is digit
if ( !isdigit( (unsigned char)buffer[i] ) )
{
printf( "Only digits allowed!\n" );
//we cannot use "continue" here, because that would apply to
//the innermost loop, but we want to continue to the next
//iteration of the outer loop
goto continue_outer_loop;
}
}
//verify that we are now at end of line
if ( buffer[i] != '\0' )
{
printf( "Too many characters!\n" );
continue;
}
//everything is ok with user input, so we can break loop
break;
continue_outer_loop:
continue;
}
printf( "You entered this valid input: %s\n", buffer );
printf( "In words, that is: " );
for ( int i = 0; i < 5; i++ )
{
//don't print space on first iteration
if ( i != 0 )
putchar( ' ' );
//casting to "unsigned char" is necessary to prevent
//negative character codes
fputs( digit_names[ ((unsigned char)buffer[i]) - '0' ], stdout );
}
printf( "\n" );
}
Here is some sample input and output from the program:
Enter a 5 digit area code: 1234
Too few characters!
Enter a 5 digit area code: 123456
Too many characters!
Enter a 5 digit area code: 12h45
Only digits allowed!
Enter a 5 digit area code: 12345
You entered this valid input: 12345
In words, that is: one two three four five
Also, as you can see, codes starting with 0
work, too:
Enter a 5 digit area code: 1234
Too few characters!
Enter a 5 digit area code: 01234
You entered this valid input: 01234
In words, that is: zero one two three four