ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
I know it is not recommended to use scanf because of overflow issues etc. but, just for learning things from basics, I am using scanf.
But things[or me?] does not seem to be right
What I want to do:
I have to make a binary tree.
I want to enter numbers into the binary tree
The sequence of numbers will be
1 2 5 3 5 6 7
-they are seperated by space and at the end of it \n will follow which says the input is over.
I want scanf to read one number at a time. which means de limiter should be either space or \n and it should process that number and come back to read the next number. If it is \n, the scanf should exit
It should be something like,
Code:
while( scanf("XXXXXXX",& number)) /* XXXX is the unknown format specifier I am expecting */
{
printf("Number read - %d ",number);
node = insert_to_tree(root_pt.root,number);
}
You don't need to test for "number == '\n'", since it will never happen (scanf() cannot scan whitespace into an integer), and because you're already testing for the end of input at the top of your while( ) loop. Recall that scanf() returns the number of fields converted, which will be zero at the end of input.
Aside from that, it doesn't look to horrible to me.
--- rod.
You don't need to test for "number == '\n'", since it will never happen (scanf() cannot scan whitespace into an integer), and because you're already testing for the end of input at the top of your while( ) loop. Recall that scanf() returns the number of fields converted, which will be zero at the end of input.
Aside from that, it doesn't look to horrible to me.
--- rod.
Thanks, but the problem is that without the inner if condition check, i feel that loop is not breaking and exiting the program.
For eg , I used a test code like this -> As per you,it should exit gracefully after I enter my numbers and press an enter
This was compiled and executed with code::blocks
Code:
#include <stdio.h>
int main()
{
int number;
while(scanf("%d",&number))
{
printf("Number : %d\n",number);
}
printf("Exited the while loop\n");
return 0;
}
and the output is
Quote:
1 2 3 4
Number : 1
Number : 2
Number : 3
Number : 4
See the print after the while loop has not come. I want to exit with a press of enter [ I assume that is same as \n]
I want to exit with a press of enter [ I assume that is same as \n]
Yes, you're correct. Adding the character input to test for terminating newline does the trick.
I find scanf() and friends great for reading from data files, and less fun for handling interactive input. The vagaries of human interaction make it easier to deal with input by reading strings or characters and parsing something sensible out of that.
Thanks. I agree to that. I did that code just to show the only way I got to exit the loop.
Change your code to resemble the following:
Code:
#include <stdio.h>
int main()
{
int number;
while(scanf("%d",&number) > 0)
{
printf("Number : %d\n",number);
}
printf("Exited the while loop\n");
return 0;
}
When you want to exit the loop, press ctrl-d, which signals an "EOF" to the input stream reader. Merely entering a newline will not exit the loop.
P.S. Consider placing a printf() before the beginning of the while-loop that prompts the user as to what to enter. Don't assume the user has read the code!
When you want to exit the loop, press ctrl-d, which signals an "EOF" to the input stream reader. Merely entering a newline will not exit the loop.
Actually, any non-whitespace character(s) that cannot be interpreted according to the format string should serve to exit the loop. Ctrl-D is a little-known concept which definitely has its uses, but I don't advocate for using it in this kind of situation. If the user presses Ctrl-D twice (and naive users often see that as a sort of gesture of insistence), the first one will terminate the program, and the second one will terminate the parent shell. On the up-side, using it to terminate one loop will not leave un-handled data in the stream, should there be further input required later in the program.
Quite often I see simple-minded applications prompt for a '.' as signal that input has ended. Use of consistent idiom seems like a good idea to me.
--- rod.
You're right. I should have made myself more clear. In the example program given, the program will terminate by virtue of its logic; it quits once user input has ended (EOF). This is a common paradigm for text-mode applications. The parent shell does the same thing when it receives EOF.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.