954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Positive int != EOF & smallest of it

how do you promt the program from positive integars terminatd by -1 .. and then ask for smallest of these integars.

I did everything except when I add (!= EOF) .. It shows the same prompt multiple times

should I still keep the code or is there a specific error?

#include <stdio.h>

int

main (void)

{
int n, smallest, i=1;
while (n != EOF)

{
printf ("Enter a list of positive integars: ",i);
scanf ("%d", &n);

if (n<=smallest)
smallest=n;
i++;
}

printf("The smallest of these integars is %d.\n ");

return (0);

}

neveragn
Newbie Poster
16 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

anyone?:S

neveragn
Newbie Poster
16 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 
It shows the same prompt multiple times

That's because the 'printf' statement is inside the loop and you don't need to pass that 'i'.You can do like this:

int main (void)//this is more readable, isn't it?
{
    int n, smallest=0, i=0;
    printf ("Enter a list of positive integars:\n");//printf outside the loop
    while (scanf("%d", &n)!=EOF){//press ctrl+Z to break out of the loop
		if(n==-1)
		    break;
		if (n<=smallest)
		    smallest=n;
		i++;
	}

	printf("The smallest among the %d integars is %d.\n ",i,smallest);

	return (0);

}
diwakar wagle
Posting Whiz in Training
203 posts since Nov 2008
Reputation Points: 48
Solved Threads: 31
 

perfect :) .. Thank you so much

neveragn
Newbie Poster
16 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: