jeudi 7 avril 2016

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");
}

Aucun commentaire:

Enregistrer un commentaire