Like Tree13Likes

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. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    17

    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. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    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
    ARod609 likes this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    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.
    ARod609 likes this.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    17
    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. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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)
      {
    
         // your code goes here
    
    
         return 0; }
    Last edited by CommonTater; 06-25-2011 at 07:50 PM.
    ARod609 likes this.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by smokeyangel View Post
    It runs ok for me. What's the problem?
    You actually got it to run without a main() function?
    Weird!
    ARod609 likes this.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Quote Originally Posted by CommonTater View Post
    You actually got it to run without a main() function?
    Weird!
    Hehe. No. MSVC isn't that insane. I put main() in.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by smokeyangel View Post
    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...
    ARod609 likes this.

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Quote Originally Posted by CommonTater View Post
    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. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    17
    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. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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.
    ARod609 likes this.

  12. #12
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    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)
    	{
              .....
              .....
    ARod609 and CommonTater like this.

  13. #13
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    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
    ARod609 likes this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  14. #14
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    520
    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]);
    ARod609 likes this.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonpuz View Post
    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.
    AndrewHunter and ARod609 like this.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help whit programing test In C language
    By alionas in forum C Programming
    Replies: 4
    Last Post: 11-20-2010, 10:37 AM
  2. Correct programing language for games?
    By brack in forum Game Programming
    Replies: 4
    Last Post: 09-03-2010, 06:21 AM
  3. The C Programing Language soln needed
    By xabhi in forum C Programming
    Replies: 5
    Last Post: 08-25-2010, 04:06 PM
  4. Help to solve My easy C programing questions!
    By hotwebs in forum C Programming
    Replies: 13
    Last Post: 12-25-2009, 03:55 AM
  5. chalanging programing to solve
    By j4k50n in forum C Programming
    Replies: 8
    Last Post: 04-20-2007, 04:02 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21