# Thread: can't get right output?

1. ## 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;

}```

2. 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?

3. 0? haha idk. this is my first program.

4. 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.

5. Would this require me to post a separate print statement every time a value of 0 is given?

6. 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."

7. 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

8. 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;

}```