Reputation: 39
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;
}
};
}
Upvotes: 0
Views: 92
Reputation: 225737
In your calc
function, you're mixing up input
, which is a parameter, and number
, which is a global.
Using a global here for your input variable is bad form, and in fact you don't need to pass anything into this function.
Also, better to use do..while
instead of while
since the loop must run at least once:
You're also using the function before it's declared, so you should have a function prototype. And since calc
doesn't need to return anything, set its return type to void
.
#include <stdio.h>
int evencount, oddcount;
int eventotal, oddtotal;
void calc();
int main()
{
...
calc();
...
}
void calc()
{
int number;
do {
scanf("%d", &number);
if (number%2 == 1) {
oddcount++;
oddtotal += number;
} else {
evencount++;
eventotal += number;
}
} while (number != 0);
}
Upvotes: 1
Reputation: 45135
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.
Upvotes: 3