Hi guys, need some help in C Programming HW. Here are the instructions:
Here is what I've got so far. Numbers only apply for 1-9 so I tried doing this, but doesn't work:
Here's what the output looks like and I don't know why:
Write a program that reads integers until 0 is entered. After input terminates, the program should report the total number of even integers (excluding the 0) entered, the average value of the even integers, the total numbers of odd integers entered, and the average value of the odd integers. Do this using the switch statement.
Here is what I've got so far. Numbers only apply for 1-9 so I tried doing this, but doesn't work:
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int evencount=0, oddcount=0;
float eventot=0, oddtot=0;
char ch;
printf("Enter some numbers and enter 0 at end to terminate.\n");
while ((ch = getchar ()) != '0')
{
switch(ch)
{
case '1' : oddcount++;
oddtot+=1;
case '2' : evencount++;
eventot+=2;
case '3' : oddcount++;
oddtot+=3;
case '4' : evencount++;
eventot+=4;
case '5' : oddcount++;
oddtot+=5;
case '6' : evencount++;
oddtot+=6;
case '7' : oddcount++;
oddtot+=7;
case '8' : evencount++;
eventot+=8;
case '9' : oddcount++;
oddtot+=9;
}
}
printf("\n\nThe total number of even integers is %d and the average of"
"\nthem is %d", evencount, (eventot/evencount));
printf("\n\nThe total number of odd integers is %d and the average of"
"\nthem is %d", oddcount, (oddtot/oddcount));
printf("\n\n");
system("PAUSE");
return 0;
}
Here's what the output looks like and I don't know why:
Enter some numbers and enter 0 at end to terminate.
2461350
The total number of even integers is 18 and the average of
them is -1908874354
The total number of odd integers is 21 and the average of
them is -204522252
Press any key to continue . . .