can't get right output?

This is a discussion on can't get right output? within the C++ Programming forums, part of the General Programming Boards category; In my code everything works to a t but when you enter a 0 you get -1.jf? idk whats wrong. ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    5

    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. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    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?
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    0? haha idk. this is my first program.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,750
    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. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    Would this require me to post a separate print statement every time a value of 0 is given?

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,750
    Quote Originally Posted by jrg1424 View Post
    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. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    Quote Originally Posted by whiteflags View Post
    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. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    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;
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code output...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-03-2009, 03:22 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 06:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21