LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-29-2012, 06:55 PM   #1
UnixCube
Member
 
Registered: Feb 2012
Location: Texas
Posts: 58

Rep: Reputation: 0
while loop not printing message for each iteration in c language


I am programming in the c language. I have a while loop and it is storing two variables because I am using the scanf function as a condition. Each time that I insert the two numbers for input into the while condition, it does not return to me which one is smaller, instead it just continues to allow me to input more numbers. Finally when I press q, it presents to me the last 2 inputs that I made. I'd like to know if anyone has any ideas on this, and thank you for any advice.


Code:
/* lesser.c -- finds the lesser of two nums page 310 */
#include <stdio.h>
int imin(int, int); // function prototype of imin function

int main(void)
{
	int num, num2; // declaration of variables of type int
	int count;
	printf("Enter a pair of integers (q to quit):\n");
	scanf("%d %d", &evil1, &evil2); // decimal conversion specifier to store input inside variable evil1 and evil2
	
	while (scanf("%d\n %d\n", &num, &num2) != 0); // while condition uses scanf to store two integers inside                                                                                                                                            //variables num & num2
	 {
		printf("The lesser of %d and %d is %d.\n",
                       num, num2, imin(num,num2));//decimal conversion specification
		printf("Enter a pair of integers (q to quit):\n");
	 } // end while loop
	printf("Bye.\n");  
	return 0;
} // end main function

int imin(int n, int m)
{
	int min; // declaration of variable min of type int
	
	if (n < m)
		min = n; // assign min the value of n
	else
		min = m;
	return min;

} // end imin function

Last edited by UnixCube; 04-29-2012 at 07:43 PM. Reason: the output did not look as i intended it initially
 
Old 04-29-2012, 07:02 PM   #2
UnixCube
Member
 
Registered: Feb 2012
Location: Texas
Posts: 58

Original Poster
Rep: Reputation: 0
so the output is like this: The problem is that whenever I input two numbers the program won't return to me the value
of which integer is smaller, and it won't prompt me to either insert two more numbers or press q to quit, until I finally press q and then it prompts me with that information, but by then the program terminates itself.

Code:
Enter a pair of integers (q to quit):
12
32
12
32
43
q
The lesser of 43 and 32 is 32.
Enter a pair of integers (q to quit):
Bye.
 
Old 04-29-2012, 07:04 PM   #3
UnixCube
Member
 
Registered: Feb 2012
Location: Texas
Posts: 58

Original Poster
Rep: Reputation: 0
it should be like this:

Code:
Enter a pair of integers (q to quit):
509 333
The lesser of 509 and 333 is 333.
Enter a pair of integers (q to quit):
-9393 6
The lesser of -9393 and 6 is -9393.
Enter a pair of integers (q to quit):
q
Bye.
 
Old 04-29-2012, 07:41 PM   #4
UnixCube
Member
 
Registered: Feb 2012
Location: Texas
Posts: 58

Original Poster
Rep: Reputation: 0
Alright, guys I fixed the problem. Here is the code and the output, in case anyone else has a similar problem that might need solving. Thank You.

Code:
/* lesser.c -- finds the lesser of two nums page 310 */
#include <stdio.h>
int imin(int, int); // function prototype of imin function

int main(void)
{
	int num, num2; // declaration of variables of type int
	
	printf("Enter a pair of integers1 (q to quit):\n");
	
	while (scanf("%d %d", &num, &num2) == 2) // while condition uses scanf to store two integers inside variables evil1 & evil2
	{
		printf("Enter a pair of integers (q to quit):\n");
	
		printf("The lesser of %d and %d is %d.\n",num, num2, imin(num,num2)); // decimal conversion specification
	
	}// end while loop
	printf("Bye.\n"); // 
	return 0;
} // end main function

int imin(int n, int m)
{
	int min; // declaration of variable min of type int
	
	if (n < m)
		min = n; // assign min the value of n
	else
		min = m;
	return min;

} // end imin function
Here is the correct output that I received:

Code:
Enter a pair of integers (q to quit):
12
32
Enter a pair of integers (q to quit):
The lesser of 12 and 32 is 12.
34
12
Enter a pair of integers (q to quit):
The lesser of 34 and 12 is 12.
q
Bye.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] printing a through g using a while loop in c language UnixCube Programming 23 04-09-2012 03:52 PM
[SOLVED] bash for loop iteration question. ncalsmitty1369 Programming 4 02-05-2010 05:50 PM
python: can I see which iteration I'm on in a loop? BrianK Programming 2 08-27-2008 09:01 PM
Loop iteration in Linux scripting. 151803 Linux - Newbie 3 03-19-2007 06:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration