LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-12-2011, 08:48 AM   #1
sree_ec
Member
 
Registered: Sep 2010
Location: world
Distribution: Ubuntu 12.04LTS
Posts: 76

Rep: Reputation: 5
scanf problem


Hi ,

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


   }
How can I read this through scanf?


EDIT:

I did
Code:
   while( scanf("%d",&number))
   {

         printf("Number read - %d ",number);
         node = insert_to_tree(root_pt.root,number);
         scanf("%c",&number);
         if(number == '\n')
                return;


   }
which I personally think is a horrible way of doing it. Please advice

Last edited by sree_ec; 05-12-2011 at 08:59 AM.
 
Old 05-12-2011, 09:19 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by sree_ec View Post
Code:
...
         scanf("%c",&number);
...
There's a problem with the statement above; you are requesting that a character be placed into a variable that presumably has been declared as an int.

Recompile your code, and pay attention to the warnings.
 
Old 05-12-2011, 11:32 AM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 05-12-2011, 12:49 PM   #4
sree_ec
Member
 
Registered: Sep 2010
Location: world
Distribution: Ubuntu 12.04LTS
Posts: 76

Original Poster
Rep: Reputation: 5
Quote:
Originally Posted by theNbomr View Post
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]
 
Old 05-12-2011, 12:51 PM   #5
sree_ec
Member
 
Registered: Sep 2010
Location: world
Distribution: Ubuntu 12.04LTS
Posts: 76

Original Poster
Rep: Reputation: 5
Quote:
Originally Posted by dwhitney67 View Post
There's a problem with the statement above; you are requesting that a character be placed into a variable that presumably has been declared as an int.

Recompile your code, and pay attention to the warnings.
Thanks. I agree to that. I did that code just to show the only way I got to exit the loop.
 
Old 05-12-2011, 03:08 PM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by sree_ec View Post
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.

--- rod.

Last edited by theNbomr; 05-12-2011 at 05:56 PM.
 
1 members found this post helpful.
Old 05-12-2011, 05:12 PM   #7
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by sree_ec View Post
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!
 
1 members found this post helpful.
Old 05-13-2011, 12:31 AM   #8
sree_ec
Member
 
Registered: Sep 2010
Location: world
Distribution: Ubuntu 12.04LTS
Posts: 76

Original Poster
Rep: Reputation: 5
Quote:
Originally Posted by dwhitney67 View Post
Change your code to resemble the following:

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.
Yes, this works. Thank you. Although it does not match the \n criteria, I guess it is good enough considering the entire purpose.
 
Old 05-13-2011, 09:24 AM   #9
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by dwhitney67 View Post
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.
 
Old 05-13-2011, 09:49 AM   #10
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by theNbomr View Post
... 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...
A ctrl-d will not terminate the program. Perhaps you are thinking of ctrl-c, which sends a SIGINT to the program?
 
Old 05-13-2011, 10:12 AM   #11
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.

--- rod.
 
  


Reply



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
Using scanf in c on doubles mkrems Programming 2 04-28-2008 07:10 PM
problem with scanf fssengg Programming 3 03-29-2005 04:27 AM
scanf() problem. LUB997 Programming 3 12-19-2004 08:03 PM
scanf blackzone Programming 1 08-04-2004 01:50 AM
scanf help homerz Programming 2 11-11-2003 05:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:40 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