Hiiiiiiiiiiii!!!!!!!!!...........

The program written below runs perfectly upto some extent but the output varies............because the compiler prints the "{Enter the value}" line twice and itself adds a positive number in the counter..................

#include<stdio.h>
#include<conio.h>

int main()
{
	int sum1=0,sum2=0,sum3=0;
	char value,alpha;
	
	
	while(alpha!='D')
	{
		printf("\n Enter the value");
		scanf("%c",&alpha);
		
		
		if(alpha==0)
		{
			sum1=sum1+1;
		}
		
		else if(alpha>0)
		{
			sum2=sum2+1;  // for positive
		}
		else if(alpha<0)
		{
			sum3=sum3+1;
		}
	}
	
	sum2=sum2-1;
	
	printf("\n The zeros are %d", sum1);
	printf("\n the positive are %d", sum2);
	printf("\n the negative are %d",sum3);
	
	getch();
	
	
}

>>because the compiler prints the
Don't blaim the compiler for the code you wrote :) The logic of your program is flawed.

scanf("%c" expects you to enter an alphabetic character, such as 'D'. The switch statement is checking for numeric data, such as 0, 1, 2, ... Since you used scanf( with %c that switch statement must check for the numeric values of the character, such as '0', '1', '2' etc. Note that using < and > doesn't mean much with that because, if you look at any ascii char you will see that there are NO characters < 0 and they are all > 0. Finallly, its ot possible to eter a negative value with %c.

commented: Righto +1

Like Ancient Dragon said, it looks like you declared a switch statement which is looking for numbers ( ==0, less than, and greater than 0) but your input method is asking for a character.

What you should do is change the input to int, and actually let the while loop preform without making it "Not equal to" whichever number or letter.

What about instead of not D, using :

While(x!=EOF)

With that, whenever the user is done, they can press cntrl+Z then enter to end the loop?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.