We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,913 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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);
	
}
2
Contributors
3
Replies
2 Hours
Discussion Span
1 Year Ago
Last Updated
4
Views
Question
Answered
neveragn
Newbie Poster
16 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

anyone?:S

neveragn
Newbie Poster
16 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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);

}
D33wakar
Posting Whiz in Training
206 posts since Nov 2008
Reputation Points: 48
Solved Threads: 31
Skill Endorsements: 1

perfect :) .. Thank you so much

neveragn
Newbie Poster
16 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by D33wakar

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0651 seconds using 2.67MB