can't get right output?

• 10-05-2010
jrg1424
can't get right output?
In my code everything works to a t but when you enter a 0 you get -1.jf? idk whats wrong.
any help would be appreaciated.

Code:

```#include "stdafx.h" #include <conio.h> int _tmain(int argc, _TCHAR* argv[]) {         int x;         int XCount;         int sum;         float average;         sum = 0;         XCount = 1;         printf( "Enter Integer 1:" );         scanf_s("%d", &x);         while ( x != 0 ){                         sum = sum + x;                         XCount = XCount + 1;                         printf("Enter Integer %d:", XCount);                         scanf_s("%d", &x);         }                                         average = ( float ) sum / (XCount - 1 );         printf( "*****************\n" );         printf( "You have entered %d non-zero numbers\n", XCount - 1);         printf( "The sum of these integers is %d\n", sum );         printf( "The average of these integers is %.2f\n", average );         printf( "*****************" );         getch();         return 0; }```
• 10-05-2010
bithub
When you input 0, you have the calculation of:
Code:

`average = (float) 0 / 0;`
What do you expect the output of zero divided by zero to be?
• 10-05-2010
jrg1424
0? haha idk. this is my first program.
• 10-05-2010
whiteflags
0/0 is defined in math as equaling zero. But in programming dividing by zero is never OK. The actual answer you get is -1.#INF or -1.#NAN which means infinity, (or Not a Number) and that is usually correct but you can't use it. Print it unformatted (plain %f) and you will see.
• 10-05-2010
jrg1424
Would this require me to post a separate print statement every time a value of 0 is given?
• 10-05-2010
whiteflags
Quote:

Originally Posted by jrg1424
Would this require me to post a separate print statement every time a value of 0 is given?

That depends on what you mean. C can't do calculus like that so dividing by zero is an error you'd want to prevent. You could use printf to say something like "I can't divide by zero."
• 10-05-2010
jrg1424
Quote:

Originally Posted by whiteflags
That depends on what you mean. C can't do calculus like that so dividing by zero is an error you'd want to prevent. You could use printf to say something like "I can't divide by zero."

yeah, i added an if statement using the XCount and it worked. thanks for the help
• 10-05-2010
jrg1424
Code:

```#include "stdafx.h" #include <conio.h> int _tmain(int argc, _TCHAR* argv[]) {         int x;         int XCount;         int sum;         float average;         sum = 0;         XCount = 1;         printf( "Enter Integer 1:" );         scanf_s("%d", &x);         while ( x != 0 ){                         sum = sum + x;                         XCount = XCount + 1;                         printf("Enter Integer %d:", XCount);                         scanf_s("%d", &x);         }                                 if (XCount != 1 ){         average = ( float ) sum / (XCount - 1 );         printf( "*****************\n" );         printf( "You have entered %d non-zero numbers\n", XCount - 1);         printf( "The sum of these integers is %d\n", sum );         printf( "The average of these integers is %.2f\n", average );         printf( "*****************" ); }       else{         printf( "*****************\n" );         printf( "You have entered %d non-zero numbers\n", XCount - 1);         printf( "The sum of these integers is 0\n");         printf( "The average of these integers is 0\n");         printf( "*****************" ); }         getch();         return 0; }```