Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > scanf hangs when taking multiple inputs in a char array

Reply
Thread Tools

scanf hangs when taking multiple inputs in a char array

 
 
Adi
Guest
Posts: n/a
 
      11-03-2006
Hello friend,
Lately, i cam accross a small problem that has causing quite more
pain.
Scanf hangs whenever i try to re- take input into the same char array.
I've pasted below excerpt of my code:
----------------------------------------------------------------------------------------------------------------------------------------
short cond =0;
unsigned char intStr[CLI_MAX_INT16_CHARS];


/*loops until the value entered is valid*/
do
{
fflush(stdin);
scanf("%s",intStr);
cond=validateSInt(intStr);
}while(cond==1);
----------------------------------------------------------------------------------------------------------------------------------------
Given below is the stack trace of my code when i preempted it on SIGINT
(Ctrl+C). As u can see, the stack dump shows that scanf is hanged....

(gdb) where
#0 0x001e016e in __read_nocancel () from /lib/tls/libc.so.6
#1 0x0017cbb8 in _IO_file_read_internal () from /lib/tls/libc.so.6
#2 0x0017be8e in _IO_new_file_underflow () from /lib/tls/libc.so.6
#3 0x0017e5cd in _IO_default_uflow_internal () from /lib/tls/libc.so.6
#4 0x0017e27d in __uflow () from /lib/tls/libc.so.6
#5 0x00168cb4 in _IO_vfscanf_internal () from /lib/tls/libc.so.6
#6 0x0016e67a in scanf () from /lib/tls/libc.so.6

 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      11-03-2006
Adi wrote:

> Hello friend,
> Lately, i cam accross a small problem that has causing quite more
> pain.
> Scanf hangs whenever i try to re- take input into the same char array.
> I've pasted below excerpt of my code:


It's better to paste entire (minimal) examples. (If you don't understand
what's going wrong, you don't know what's important ...)

> ----------------------------------------------------------------------------------------------------------------------------------------
> short cond =0;
> unsigned char intStr[CLI_MAX_INT16_CHARS];
>
>
> /*loops until the value entered is valid*/
> do
> {
> fflush(stdin);


Undefined. BOOM. See the FAQ. To discard any "pending input", read
it.

> scanf("%s",intStr);


No check for value being there. Suspect incorrect understanding of %s
format in scanf. Boomlet.

> cond=validateSInt(intStr);
> }while(cond==1);
> ----------------------------------------------------------------------------------------------------------------------------------------
> Given below is the stack trace of my code when i preempted it on SIGINT
> (Ctrl+C). As u can see, the stack dump shows that scanf is hanged....


No, it shows it was in scanf when you interrupted it.

--
Chris "unhashedup hashed up hashing" Dollin
"Life is full of mysteries. Consider this one of them." Sinclair, /Babylon 5/

 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      11-03-2006
Adi <(E-Mail Removed)> wrote:
> Hello friend,
> Lately, i cam accross a small problem that has causing quite more
> pain.
> Scanf hangs whenever i try to re- take input into the same char array.
> I've pasted below excerpt of my code:
> ---------------------------------------------------------------
> short cond =0;
> unsigned char intStr[CLI_MAX_INT16_CHARS];


> /*loops until the value entered is valid*/
> do
> {
> fflush(stdin);


That's not allowed - you can only fflush() output, but not
input streams. There seems to be some extension under Windows,
allowing you to remove everthing from stdin by calling fflush()
on it, but you should avoid that as non-portable. If you use
it under e.g. UNIX it probably has not effect at all.

> scanf("%s",intStr);


Here's a problem: if the user enters more than (CLI_MAX_INT16_CHARS -1)
non-white-space characters scanf() will write past the end the buffer
you supplied it with.

> cond=validateSInt(intStr);
> }while(cond==1);
> ------------------------------------------------------------------
> Given below is the stack trace of my code when i preempted it on SIGINT
> (Ctrl+C). As u can see, the stack dump shows that scanf is hanged....


> (gdb) where
> #0 0x001e016e in __read_nocancel () from /lib/tls/libc.so.6
> #1 0x0017cbb8 in _IO_file_read_internal () from /lib/tls/libc.so.6
> #2 0x0017be8e in _IO_new_file_underflow () from /lib/tls/libc.so.6
> #3 0x0017e5cd in _IO_default_uflow_internal () from /lib/tls/libc.so.6
> #4 0x0017e27d in __uflow () from /lib/tls/libc.so.6
> #5 0x00168cb4 in _IO_vfscanf_internal () from /lib/tls/libc.so.6
> #6 0x0016e67a in scanf () from /lib/tls/libc.so.6


This doesn't indicate that you're hanging in scanf(). It's just that
only while the program is waiting for user input you have a chance to
hit Ctrl-C and thus it stops somewhere in the innards of scanf().
Did you check if validSInt() ever returns anything else than 1?

Regards, Jens
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
length of 2D Array >> char **myString= (char **) malloc (sizeof (char *)); davidb C++ 0 09-01-2006 03:22 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
Problem- strcat with char and char indexed from char array aldonnelley@gmail.com C++ 3 04-20-2006 07:32 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
Clear hangs up - & hangs up - & hangs up Sue Bilstein NZ Computing 26 03-07-2004 01:33 AM



Advertisments