Click here to Skip to main content
15,936,460 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include "stdafx.h"

int main()
	unsigned int  i, k = 0, l = 0, m = 0, j = 1;
	int x;
		printf("\nEnter the number of elements you want to enter (MAX VALUE 100) : ");
		scanf_s("%d,", &x);
		if (x >= 0&&x<=100)
			int a[100];
			for (i = 1; i <= x; i++)
				printf("\nEnter the %d element : ", i);
				scanf_s("%d", &a[i]);
				if (a[i] > 0)
				if (a[i] < 0)
				if (a[i] == 0)
			printf("\nInvalid entry");
	} while (j <= 1);
		printf("\nNo of Positive numbers are : %d\nNo of Negative numbers are : %d\nNo of zeros are : %d\n", k, l, m);

	return 0;

The above program simply counts the number of positives, negatives and zeros in sequence of numbers entered by the user. I have put a small check using do while loop whether the max range given by the user is a valid number or not, But what if the user enters a character or a special symbol(Well I tried it myself it turns out to be an infinite loop)how to perform a check for it in order to avoid the program getting into the infinite loop and also I want to know why it is entering the infinite loop upon entering a character or a symbol.

What I have tried:

I have tried entering a character/symbol as an input it gives an output like the following infinite times.
Enter the number of elements you want to enter : $
Invalid entry
Enter the number of elements you want to enter :
Invalid entry
Enter the number of elements you want to enter :
Invalid entry
Updated 4-Mar-17 19:41pm

1 solution

j is never less than 1, so the loop won't exit. j is only incremented: j++.

Ask yourself, "How does the user tell me they are finished?".

Rather than calculate as you go, it would be better to do that after the user has completed entering their number.

[Edit:] Here is pseudo code to do what you want:
    /* 1. get user input
     * 2. test input:
     *     if {exit loop marker} then break
     *     if {invalid value} then warn
     *     else add value to array
for {start; test max; increment}
    /* if isPositive, inc positive count
     * if isNegative, inc negative count
     * else inc zero count
// report counts back to user

This should be enough of a guide to help you write your code.

TIP: use variable names that represent what the values hold. Make code 1000% more readable.
Share this answer
ArvindSai 5-Mar-17 1:48am    
thank you,
but one last thing.
say that I have entered a character,as it is invalid it prints "Invalid entry" then again starts the execution from beginning (without incrementing the value of j of course)of the loop by printing " Enter the number of elements you want to enter (MAX VALUE 100) : ", then after why it is not pausing the execution for the user's input as the next instruction is scanf_s?
Graeme_Grant 5-Mar-17 1:57am    
I posted an update to my original answer with the logic required. I'll leave the actual coding up to you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900