# Trying to teach myself C programing language, and I am not sure how to solve this...

This is a discussion on Trying to teach myself C programing language, and I am not sure how to solve this... within the C Programming forums, part of the General Programming Boards category; Ask the user to enter a digit between 0 and 9. Have the program print out the digit in words, ...

1. ## Trying to teach myself C programing language, and I am not sure how to solve this...

Ask the user to enter a digit between 0 and 9. Have the program print
out the digit in words, for example:

Enter a digit between 0 and 9: 4
You entered the number four

Assume that the user will enter only a single digit. The user may
accidentally enter a single character, and this should generate an error
message.

Code:
#include <stdio.h>
#include <ctype.h>

{
int num1;

printf("Insert a number from 0 to 9: ");
scanf("%d",&num1);
if(num1< 0 || num1>9)
printf("error\n");
else{
printf("You entered the number ");
if
( num1==0) printf("zero");
else if
( num1==1) printf("one");
else if
( num1==2) printf("two");
else if
( num1==3) printf("three");
else if
( num1==4) printf("four");
else if
( num1==5) printf("five");
else if
( num1==6) printf("six");
else if
( num1==7) printf("seven");
else if
( num1==8) printf("eight");
else if
( num1==9) printf("nine");

}
return 0;
}
the if and else statement is just really beating me down, I am using X code and I tried to look at the code snippets and type it exactly but I am still having a problem...

2. You should take a look at using the switch statement. It will make things a little easier.

Also use the isdigit function in the ctype library. Getting int input FAQ

3. It runs ok for me. What's the problem? It generates "error" if I put in "k" for example. Maybe you want it to output "error, k is not a number" or something. If that's the case then you should definitely have a look at the FAQ mentioned in AndrewHunter 's post. You'll need to change the scanf format specifier to accept characters not just digits (%c. With %d it will not read anything in except digits).

Be careful to either convert the ascii char values to int or to compare against the char values. This is also described in the same FAQ entry.

4. Well I want it to respond to a #0-9

You entered the number four
and
Error when you type a character

formatting the if and else statement is what I am getting confused with I try using the code snippets in X-code and in my book but X code keeps on giving me errors

5. Part of the problem may be visual. All this stuff about indenting and formatting code is that it makes it a lot easier to "see" what's going on...

Code:
if ( num1==0)
printf("zero");
else if ( num1==1)
printf("one");
else if ( num1==2)
printf("two");
else if ( num1==3)
printf("three");
else if ( num1==4)
printf("four");
else if ( num1==5)
printf("five");
else if ( num1==6)
printf("six");
else if ( num1==7)
printf("seven");
else if ( num1==8)
printf("eight");
else if ( num1==9)
printf("nine");
It's just a text reformat but it should be easier for you to follow...
Code:
if (condition) is true
do this
else
do that
The idea is that each if() statment is actually in the else clause of the one before, so that only one of the many conditions is ever executed...

Perhaps if you simplified your code just a little it would be easier to follow...
Code:
printf("Insert a number from 0 to 9: ");
scanf("%d",&num1);
if(num1< 0 || num1>9)
{ printf("error\n");
exit(0); }

printf("You entered the number ");

...
That first else statement isn't necessary if you exit the program right when the error occurs. If the error does not occur, the program continues...

EDIT: I just noticed that you don't have a "main" function in your code which worked just fine when I added it...

This is the minimum skeleton for a C program...
Code:
int main (void)
{

return 0; }

6. Originally Posted by smokeyangel
It runs ok for me. What's the problem?
You actually got it to run without a main() function?
Weird!

7. Originally Posted by CommonTater
You actually got it to run without a main() function?
Weird!
Hehe. No. MSVC isn't that insane. I put main() in.

8. Originally Posted by smokeyangel
Hehe. No. MSVC isn't that insane. I put main() in.
Well... perhaps you should have mentioned that instead of telling the OP his program worked as posted...

9. Originally Posted by CommonTater
Well... perhaps you should have mentioned that instead of telling the OP his program worked as posted...
Yes, I should have. Sorry about that.

10. I tried just to simplify it, now I am just going to read up on it obviously something I am doing with the if and else statement is wrong and I have to read up on switch statements too...

input:"w"
"output: the number you have entered zero"
input:"2"
"the number you have entered twoerror"

Code:
#include <stdio.h>
#include <ctype.h>
int main ()
{
int num;
printf("Please enter a numbe from 0-9 ");
scanf ("%d",&num);
if(num)
if
(num==1)
printf("the number you have entered one");
if
(num==2)
printf("the number you have entered two");
if
(num==3)
printf("the number you have entered three");
if
(num==4)
printf("the number you have entered four");
if
(num==5)
printf("the number you have entered five");
if
(num==6)
printf("the number you have entered six");
if
(num==7)
printf("the number you have entered seven");
if
(num==8)
printf("the number you have entered eight");
if
(num==9)
printf("the number you have entered nine");
if
(num==0)
printf("the number you have entered zero");

else
printf ("error");

return 0;

11. Rod... your first attempt at this was working, except for the missing main() function.

Some things to read up on...
1) Source Code formatting. Yes, believe it or not there are standards for this. But mostly, as I said before, it's done to make the code easier to follow, to let you see what's happening and what's inside of what. It may not seem a big deal, but with experience you'll learn that proper formatting will sometimes reveal subtle errors before they become a problem.

2) The correct use of if()-else conditional statements. Try some really simple ones until you get the hang of it. For example, can you tell me what the result will be from each of the following...
Code:
int x = 10

if (x < 10)
puts("it's true");
else
puts("it's false");

if (x == 10)
puts("it's true");
else
puts("it's false");

if (x != 21)
puts("it's true");
else
puts("it's false");
Thing is, I'm betting you understand it better than you think you do... but you expect it to be complex and go about making it harder than it needs to be.

12. Yes, I can see that behaviour now. It wasn't the right thing to do to take out all the "else"s. I think that for this, looking into switch statements is a good plan.

I can see what you were trying to do with the if(num) else "error" block. You've missed out some braces, which is why you're getting the results you're seeing. I'm sure you can figure that out though (and if not, post again...).

I just wanted to point out that
if (num)
isn't going to do what you want it to. It'll be fine if a digit is given, but if a letter is given then the value of num is uninitialised, so it could have any value, probably not zero. Since 0 is an acceptable input, I suggest

Code:
int num = -1;
printf("Please enter a numbe from 0-9 ");
scanf ("%d",&num);
if (num != -1)
{
.....
.....

13. Rod,

Learning C can be a very daunting but highly rewarding adventure, especially if it is your first introduction to programming. Don't get discouraged, we all started out green behind the ears. The tutorials found on this site are a good starting point. Also I stumbled across this tutorial as well.

-Happy coding

14. lets not forget about using arrays....anytime you're working with small numbers (less than a couple thousand..) you can often use an array as a lookup table , rather then hundreds of if statements or switch cases:

Code:
const char * word_lookup[] = {
"zero", "one", "two",
"three","four","five",
"six", "seven", "eight",
"nine"
};

/* get user input...convert it to "num" check for error, etc.. */

if (num > 0 && num < sizeof word_lookup / sizeof word_lookup[0])
printf("You entered '%s'\n",word_lookup[num]);

15. Originally Posted by nonpuz
lets not forget about using arrays....anytime you're working with small numbers (less than a couple thousand..) you can often use an array as a lookup table , rather then hundreds of if statements or switch cases:
You mean something like this? (solution in 20 lines)
Code:
#include <stdio.h>
#include <stdlib.h>

int main (void)
{
int  number = -1;
char *asword[] = {"zero","one","two","three","four","five","six","seven","eight","nine"};

printf("Enter a number from 0 to 9 : ");
scanf("%d",&number);
if  ((number < 0) || (number > 9))
{
printf("Error: Only numbers 0 - 9 are allowed\n");
exit(0);
}

printf("You entered the number %s\n",asword[number]);

return 0;
}
This may be just a bit too advanced for our learning friend...
He really needs to get solid on the raw syntax (if() while() etc.) first.

Page 1 of 2 12 Last