1.11M Members

Positive int != EOF & smallest of it

 
0
 

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);
	
}
 
0
 

anyone?:S

 
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);

}
 
0
 

perfect :) .. Thank you so much

Question Answered as of 2 Years Ago by D33wakar
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: