Search code examples
cfunctiondefined

C User-defined function issue


So my program asks for name and numbers, then tallies up the number of even entries and odd entries, then giving a total of all the even entries and a total for all the odd.

It all works except the calculations, it tells me I have all odd entries and only values them as 1 when adding them up.

I feel like it has something to do with my variables and referencing in my calc function

#include <stdio.h>
int number = 1;
int evencount, oddcount;
int eventotal, oddtotal;



int main () {


char name[256];



printf("Enter your name \n");
scanf("%s", name);

printf("Enter numbers within 1-100 \n");
printf("Enter 0 to quit\n");
calc(number);


printf("%s,the numbers you have entered are broken down as follows: \n", name);

printf("%d odd entries \n", oddcount);

printf("%d even entries\n", evencount);

printf("You entered even numbers with a total value of %d \n", eventotal );
printf("You entered odd numbers with a total value of %d \n", oddtotal);

return 0;

}

int calc (int input) {

while (number != 0) {
    scanf("%d", &number);
    if (input%2 == 1) {
        oddcount++;
        oddtotal += input;
    }
    else {
        evencount++;
        eventotal += input;

    }
};

}

Solution

  • scanf("%d", &number);
    if (input%2 == 1) {
    {
        oddcount++;
        oddtotal += input;
    }
    

    I think you probably want number % 2 rather than input % 2, oddtotal += number rather than oddtotal += input, etc.