Thread: Just wanted to share this

  1. #1
    Registered User SCRIPT_KITTEH's Avatar
    Join Date
    Apr 2013
    Posts
    74

    Talking Just wanted to share this

    Hi folks,

    Just wanted to share a program I made. It was the answer to one of the end chapter exercises in the C programming book I'm using, asking the reader to create a program that adds all the digits of an integer. It's nothing special, and I'm not sure what practical use it could have, but I'm proud of it because in the past this type of thing would have frustrated me and I would have given up, but now the challenge of figuring things out excites me. Oh and I just thought "fuse" would be a fun word for the context :P

    Code:
    /* Program to calculate the sum of the digits in an integer */
    
    
    #include <stdio.h>
    
    
    int main ()
    
    
    {
        int number, right_digit, sum = 0;
        
        printf ( "\nWhat number do you wanna fuse? ");
        scanf ( "%i", &number );
        printf ("\n");
        
        
        while ( number != 0) {
            right_digit = number % 10;
            sum += right_digit;
            number = number / 10;
        }
        
        printf ( "The sum of the digits is %i.", sum );
        printf ("\n\n");
        
        return 0;
    }

  2. #2
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    It might be a good idea to check the return value of scanf before trying to process the input. Since number is not initialized, the user could enter a non-digit yet the loop may run giving a result that may not be what one would expect.

    Code:
    if(scanf("%i", &number) == 1)
    {
        // process the digits
    }
    The downside is something like 123abc would still be a "valid" number, but abc123 wouldn't be processed.

  3. #3
    Registered User SCRIPT_KITTEH's Avatar
    Join Date
    Apr 2013
    Posts
    74
    Quote Originally Posted by ronin View Post
    It might be a good idea to check the return value of scanf before trying to process the input. Since number is not initialized, the user could enter a non-digit yet the loop may run giving a result that may not be what one would expect.

    Code:
    if(scanf("%i", &number) == 1)
    {
        // process the digits
    }
    The downside is something like 123abc would still be a "valid" number, but abc123 wouldn't be processed.
    Good idea. How does " == 1 " test whether it is a digit or not? The chapter about if statements is the one right after the chapter about loops, so I don't know much about them yet

    Is " == 1 " like a boolean or something?

    Thanks for the input.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Check the documentation for scanf. What does its return value mean?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    The if statement had nothing to do with the digits, but whether or not the input received from the user was valid for the data type being used. scanf returns an integer that represents how many items were properly converted and stored.

    In your program, had a user entered something like abc instead of an integer, scanf would have gone into an error state, but the loop to compute the sum of the digits could be executed because its control was number != 0.

    Since number hadn't been initialized, its value could have been anything including 0 if you were lucky. The short and skinny is that it's up to you to ensure that your data is valid - you cannot rely solely on the user.

  6. #6
    Registered User SCRIPT_KITTEH's Avatar
    Join Date
    Apr 2013
    Posts
    74
    Quote Originally Posted by laserlight View Post
    Check the documentation for scanf. What does its return value mean?
    Hmm... I tried to Google "scanf" and "documentation for scanf" but I feel none the wiser

    I tried to look at these links:
    scanf format string - Wikipedia, the free encyclopedia
    C Printf and Scanf Reference

    But I don't really understand... .-.

    In my mind right now the most in depth understanding I have of "scanf" is something I can put in a program that allows the user to assign a value to a variable while the program's running, but that's it...

    Sorry, I'm still pretty noob at all of this.

  7. #7
    Registered User SCRIPT_KITTEH's Avatar
    Join Date
    Apr 2013
    Posts
    74
    Quote Originally Posted by ronin View Post
    The if statement had nothing to do with the digits, but whether or not the input received from the user was valid for the data type being used.
    Right, I just don't understand how it works though from looking at the code.

    Quote Originally Posted by ronin View Post
    scanf returns an integer that represents how many items were properly converted and stored.
    I don't understand the use of "return" in this sentence errr... the whole sentence at all really.

    Quote Originally Posted by ronin View Post
    Since number hadn't been initialized, its value could have been anything including 0 if you were lucky.
    initialized? :\ Yeah, I understand the theory of what you're saying about user input, & I appreciate the advice, I just don't understand how the code works to put it into practice...
    Last edited by SCRIPT_KITTEH; 05-06-2013 at 04:39 PM.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In C, a function can return one value when it ends, to the function that called it, or it can return nothing (void). scanf() is a function that returns an integer - the number of items it has successfully read and stored into the variable.
    Code:
    int numberStored, myIntVariable;
    numberStored = scanf("%d", &myIntVariable);
    
    printf("Number of values read and stored by scanf(): %d\n",numberStored);
    In the above lines of code, if numberStored is 1, then one value has been successfully converted from the input, and stored that value into the myIntVariable, variable. If numberStored was zero however, then no value was read and stored at all by scanf().

    And of course, the numberStored variable would be the return from scanf().

    There is a C tutorial button at the top of this page, with a bunch of good info in it. You really need to read and work through it. Take your time. C isn't something you can learn by just skimming over it - settle in, and go slowly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. funny thing i wanted to share
    By Logan1033 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 08-21-2006, 02:46 AM
  2. Share with us!
    By ILoveVectors in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-24-2005, 05:15 PM