[SOLVED] while loop not printing message for each iteration in c language
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.