473,800 Members | 2,367 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

scanf hangs when taking multiple inputs in a char array

Adi
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_C HARS];
/*loops until the value entered is valid*/
do
{
fflush(stdin);
scanf("%s",intS tr);
cond=validateSI nt(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_i nternal () from /lib/tls/libc.so.6
#2 0x0017be8e in _IO_new_file_un derflow () from /lib/tls/libc.so.6
#3 0x0017e5cd in _IO_default_ufl ow_internal () from /lib/tls/libc.so.6
#4 0x0017e27d in __uflow () from /lib/tls/libc.so.6
#5 0x00168cb4 in _IO_vfscanf_int ernal () from /lib/tls/libc.so.6
#6 0x0016e67a in scanf () from /lib/tls/libc.so.6

Nov 3 '06 #1
2 4306
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_C HARS];
/*loops until the value entered is valid*/
do
{
fflush(stdin);
Undefined. BOOM. See the FAQ. To discard any "pending input", read
it.
scanf("%s",intS tr);
No check for value being there. Suspect incorrect understanding of %s
format in scanf. Boomlet.
cond=validateSI nt(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/

Nov 3 '06 #2
Adi <ad******@gmail .comwrote:
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_C HARS];
/*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",intS tr);
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=validateSI nt(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_i nternal () from /lib/tls/libc.so.6
#2 0x0017be8e in _IO_new_file_un derflow () from /lib/tls/libc.so.6
#3 0x0017e5cd in _IO_default_ufl ow_internal () from /lib/tls/libc.so.6
#4 0x0017e27d in __uflow () from /lib/tls/libc.so.6
#5 0x00168cb4 in _IO_vfscanf_int ernal () 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 ___ jt@toerring.de
\______________ ____________ http://toerring.de
Nov 3 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3467
by: Steve Zimmerman | last post by:
This post is not intended as an argument to any other post, just some simple scanf experiments that I wanted to share. I found experiments 5 and 6 the most educational. Also, I thought experiment 3 would give some kind of weird error, due to the lack of a space between the two `percent d's in the scanf statement, but it didn't. Experiment 1: ###########################
5
3901
by: Eduardo Olivarez | last post by:
The following code does not work correctly on my machine. Either one of the scanf()'s alone work perfectly. However, when they are combined, the second scanf() call just reads what the first one received as imput, without taking any keyboard input and then the process terminates prematurely. I looked through the glibc documentation trying to figure out what was causing this to no avail, so I started messing around with the code. Strangely...
6
599
by: Pete | last post by:
Following is a fragment from a program I'm writing. I am trying to enter an 8 bit binary representation from keyboard which is then scanfed into &Ptext and then again into &Key1. This is not binary just a representation, when I print each character in the two char arrays, Key1 displays ok but Ptext is missing the first element. I'm stumped can someone please help. The following compiles and runs to show my problem. Thanks in advance. ...
3
14146
by: linguae | last post by:
Hello. In my C program, I have an array of character pointers. I'm trying to input character strings to each index of the character pointer array using scanf(), but when I run the program, I get segmentation faults and core dumps. The problem occurs when the program calls scanf(). I don't know what is wrong with it. Here is my code: #include "stdio.h" #define SIZE 5
17
3505
by: Lefty Bigfoot | last post by:
Hello, I am aware that a lot of people are wary of using scanf, because doing it improperly can be dangerous. I have tried to find a good tutorial on all the ins and outs of scanf() but been unsuccessful. Is there a well-respected (by the c.l.c crowd) book or tutorial that really covers scanf in detail?
33
3177
by: Lalatendu Das | last post by:
Dear friends, I am getting a problem in the code while interacting with a nested Do-while loop It is skipping a scanf () function which it should not. I have written the whole code below. Please help me in finding why such thing is happening and what the remedy to it is. Kindly bear with my English. int main ()
30
3224
by: James Daughtry | last post by:
char array; scanf("%19s", &array); I know this is wrong because it's a type mismatch, where scanf expects a pointer to char and gets a pointer to an array of 20 char. I know that question 6.12 of the C FAQ says that it's wrong for that very reason. What I don't know is where the standard tells me conclusively that it's wrong. What I also don't know is somewhere that this type mismatch will break in practice.
68
4670
by: stasgold | last post by:
Hello. I maybe reinvent the weel ... I'm trying to read positive integer number with the help of scanf, if the input value is not positive number but negaive one zero or char , i have to reread the input until I get the needed pos. number I wrote the code , but it doesn't work the way it should : if i put some char into input, the program goes infinite loop instead of promting me to enter a new value.
20
11025
by: Xavoux | last post by:
Hello all... I can't remind which function to use for safe inputs... gets, fgets, scanf leads to buffer overflow... i compiled that code with gcc version 2.95.2, on windows 2000 char tmp0 = "ABCDEFGHI\0"; char buff; /* Input buffer. */ char tmp1 = "ABCDEFGHI\0";
0
9691
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10505
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10276
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10253
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9090
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7580
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6813
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.