r/C_Homework • u/The-Blank-Soup • Oct 31 '17
Help with C integer sentinels
The question is "Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the loop terminates, it prints out the sum of all the even integers read and the sum of all the odd integers read. Declare any variables that are needed."
I have
int digit, eventotal = 0, oddtotal = 0;
scanf("%d", &digit);
while (digit > 0); { if (digit % 2 == 0) eventotal += digit; else oddtotal += digit;
printf("%d", "%d", eventotal, oddtotal);
}
and it's returning an infinite loop error, I tried a do while but it didn't like that. Please point me in the right direction. Thank you Also sorry for the formatting
View all comments
Show parent comment
1
u/jedwardsol Oct 31 '17
So now you've got to the better formed
This will loop while
digitis greater than 0.The value of
digitdoesn't change in the loop. So if it was greater than 0 at the start it will always be greater than zero.Somewhere in the loop you need to change digit so that eventually it becomes 0.