jojonac
jojonac

Reputation: 39

C function return error

I'm writing a function that takes a user input up to a 1000 and then adds each number of the input. So if a user inputs 12 the function outputs 3.

When a user inputs 1000 or over the function will print "That number is to high". I can get the function to print the statement, but it also outputs 14...I do not understand why?

I understand there are probably other bugs in my code but right now this is the one that is killing me. Anything that can help would be great.

Here is my code.

#include <stdio.h>

int SummItAll(int value);

int main ()
{
  int userNumber=0, result;

printf("Enter a positive number \n ");
scanf("%d", &userNumber);

result = SummItAll(userNumber);
printf("%d\n", result);

return 0;
}

int SummItAll(int value)
{
int a=value, b, c, d, f, g;
if(a < 100)
{
  b = a/10;
  c = a%10;
  return b+c;
}
else if(a >= 100 && a < 1000)
{
  b = a/100;
  c = a%100;
  d = c/10;
  f = c%10;
  return b+d+f;
}
else
 return printf("That number is to high!\n");
}

Upvotes: 0

Views: 500

Answers (3)

Pan Ruochen
Pan Ruochen

Reputation: 2070

For your use case, it is better that SummItAll accepts a pointer parameter where both the input and output are passed and return a boolean value to indicate the returned result is valid or not.

bool SummItAll(int *value)
{
    if(...) {
      *value = ...
      return true;
    } else if(...) {
      ..
    } else {
       return false;
    }

}

Upvotes: 1

DaoWen
DaoWen

Reputation: 33019

printf returns the number of characters printed, so it's telling you that your message was 14 characters long.

Rather than returning the result of printf, you should probably return an invalid value (e.g., -1) to indicate an error:

Since the sum of 3 positive digits can never be a negative number, you can check the result of SumItAll, and then take different action in the case of an error:

#include <stdio.h>

int SummItAll(int value);

int main ()
{
    int userNumber=0, result;

    printf("Enter a positive number \n ");
    scanf("%d", &userNumber);

    result = SummItAll(userNumber);

    if (result == -1)
    {
        printf("Please enter a POSITIVE number!\n");
    }
    else if (result == -2)
    {
        printf("That number is to high!\n");
    }
    else
    {
        printf("%d\n", result);
    }

    return 0;
}

int SummItAll(int value)
{
    int a=value, b, c, d, f, g;
    if (a < 0)
    {
        return -1; // too low!
    }
    else if(a < 100)
    {
        b = a/10;
        c = a%10;
        return b+c;
    }
    else if(a >= 100 && a < 1000)
    {
        b = a/100;
        c = a%100;
        d = c/10;
        f = c%10;
        return b+d+f;
    }
    else
    {
        return -2; // too high!
    }
}

Note that -1 and -2 would be considered "magic numbers" in the code above, since they have a special meaning that isn't immediately obvious. It would probably be better to define some symbols to use in the code in place of the integer literals -1 and -2 in order to make the meaning more clear. For example:

#define ERR_NUM_IS_NEGATIVE  -1
#define ERR_NUM_IS_TOO_LARGE -2

And then you can use those symbolic names in the code instead of the literals, which should make it more clear.

Upvotes: 1

hetman
hetman

Reputation: 977

The printf() function returns an integer which indicates how many characters it has written out.

Upvotes: 0

Related Questions