Thread: C Programming Help - 'return' & digits in a number

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    4

    Exclamation C Programming Help - 'return' & digits in a number

    Hello all,

    I need some help, so any way you can help me would be greatly appreciated. I've just started learning C language and using user defined functions, but I'm still not quite sure as to what "return ;" does or how to use it. At the bottom of my programs I've been instructed to put "return 0;" and when using user defined functions I need to return a number, but don't know how to return it, where it goes when it is returned, or how to retrieve it in the main. Any information about the return command would be helpful. Thanks.

    Also, I need to create a user defined function that will keep track of how many digits are in the input that a user puts in. For example: I ask the user to input his salary and collect the input with a scanf function. If the user inputs 30000, I need some way for the program to keep track that 5 digits were inputted. Please help me if you know a way to do this or a command that will help me. Thank you.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by jamestgarner
    I need some help, so any way you can help me would be greatly appreciated. I've just started learning C language and using user defined functions, but I'm still not quite sure as to what "return ;" does or how to use it. At the bottom of my programs I've been instructed to put "return 0;" and when using user defined functions I need to return a number, but don't know how to return it, where it goes when it is returned, or how to retrieve it in the main. Any information about the return command would be helpful.
    Generally like this.
    Code:
    int foo( /* parameters */ )
    {
       int value = 0;
       /* calculate value */
       return value;
    }
    Quote Originally Posted by jamestgarner
    Also, I need to create a user defined function that will keep track of how many digits are in the input that a user puts in. For example: I ask the user to input his salary and collect the input with a scanf function. If the user inputs 30000, I need some way for the program to keep track that 5 digits were inputted. Please help me if you know a way to do this or a command that will help me.
    It depends on what your input is. If it is text, you could use strlen. If it is a number, that is a basic exercise using division. I'm lazy, so I might call a standard function to find the log base 10.

    I generally tell folks to avoid scanf, and the FAQ shows better methods.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    4
    yeah i have to be able to determine how many digits a user's input is, and i can call my own function and use log base 10, but i don't understand log very much and so idk how to set something like that up. how would you do that?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    4
    oh and about the function above. i understand you can return that value, but where does that value return to? and how can i call it in main?

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Given the function prototype (same as first line of function, exept with ';'):
    int numDigets(int number);

    you call it like this:
    Code:
    int main()
    {
         int var=numDigets(30000);
         //var equals 5. 
         return 0;
    }

    You can also do artithmatic expressions on the return value.
    And you can pass a variable instead of 30000 as needed.
    Last edited by King Mir; 10-09-2006 at 07:25 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Of course if you have no control over how many digits are input, it's hard to use scanf() to store it in a known numeric type such as int or long, as you may overflow the available space. Another reason to avoid scanf() functions.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by jamestgarner
    i can call my own function and use log base 10, but i don't understand log very much and so idk how to set something like that up. how would you do that?
    Something like this?
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int digits(int number)
    {
       return log10(number) + 1;
    }
    
    int main(void)
    {
       int i;
       for ( i = 10; i <= 10000; i *= 10 )
       {
          printf("digits(%d) = %d\n", i, digits(i));
       }
       return 0;
    }
    
    /*my output
    digits(10) = 2
    digits(100) = 3
    digits(1000) = 4
    digits(10000) = 5
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    So what happens if the number in question is 0?

    Wanna guess what digits(0) will return?

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Want to read the man page and find out?
    http://www.hmug.org/man/3/log10.php
    Jeez. Pointless criticism.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    No, he's correct. You need to make a special case for zero.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Perhaps even any value less than 1.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Code:
    if(number == 0)
    {
        return 1;
    }
    else if(number < 0)
    {
        number = -number;
    }
    //then do the log10
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  13. #13
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Want to read the man page and find out?
    http://www.hmug.org/man/3/log10.php
    Jeez. Pointless criticism.
    Did you even read your own link?

    log(+-0) , log2(+-0) , and log10(+-0) return -infinity and raise the
    "divide-by-zero" floating-point exception.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  4. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM