Thread: Take user input between 1 and 10 and print out the number as a word, using a function

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    8

    Take user input between 1 and 10 and print out the number as a word, using a function

    Hi, I'm new to C, but not programming in general, but am struggling to get my head around functions. I'm working my way through some C exercises to get some practice and have hit a wall. I need to use a function to take a number, such as 1, and print out One.

    Here's what I've got so far:

    Code:
    #include <stdio.h>
    
    int name(int n);
    char numberOne[] = "One";
        
    
    int main(void)
    {
        int n;
        
        scanf("%d", &n);
        
        if (n>=0 && n<10) name(n);
        else printf("Error: number (%d) out of range\n", n);
        
    }
    
    
    int name(int n){
        if(n == 1){
            printf("%s", numberOne);
            return 0;
        }else{
            printf("Got here");
            return 0;
        }
    }
    However, when I run the code and enter "1" the only thing that gets printed to the screen is "(lldb)". I've tried changing it to doing it without variables and just printing "One" but that gave the exact same result. I can't work out why this is, any help would be greatly appreciated.

    Thanks!
    Last edited by JaAnTr; 10-02-2014 at 04:18 AM.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Do you get the same result if you replace the line

    Code:
    printf("%s", numberOne);
    with

    Code:
    printf("%s", "One");
    ??

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    8
    Yes, it still just prints (lldb).

    Edit: Just tried compiling it in terminal and running it from there and it now works fine. It doesn't work in XCode though, that was my issue. Any idea why that is?
    Last edited by JaAnTr; 10-02-2014 at 04:43 AM.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    (lldb) is the prompt for the debugger, maybe it's happening because your program crashed somewhere. You could try running the program step by step from within XCode and see where this happens? I don't use it but some other threads commented on this with XCode so maybe there is an implementation problem:

    How to fix (lldb) error in Xcode? - mac resolved | Ask MetaFilter
    (lldb) Run time Error on Xcode - Stack Overflow

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Works on my Xcode.
    Maybe copy this, then start a new project and paste the code into that.

  6. #6
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Judging from the OP, I hope it's not your intention, JaAnTr, to use if-else statements to do your printing.

    Instead, I would use an array of strings. For example,
    Code:
    str_array[0] = "zero";
    str_array[1] = "one";
    str_array[2] = "two";
    
    /* ... */
    
    str_array[10] = "ten";

  7. #7
    Registered User
    Join Date
    Oct 2014
    Posts
    8
    Sorry, like I said I'm quite new to C. What is wrong with the way I've done it and how do I do it your way, I don't fully understand.

    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 09-23-2013, 02:59 AM
  2. Replies: 2
    Last Post: 09-22-2013, 12:53 PM
  3. Replies: 3
    Last Post: 10-12-2010, 01:40 PM
  4. Open file and print user selected number of lines
    By steals10304 in forum C Programming
    Replies: 14
    Last Post: 09-01-2009, 01:13 PM
  5. check if user input matches a word
    By fakebloo in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2004, 07:12 PM