473,386 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

code hangs

mdh
They say one learns more from one's errors...and I am certainly keeping
that up. The following is code from K&R explaining external
variables...but that is not directly relevant here. When I wrote it, I
initially wrote the code immediately below. But this hangs.

(c is an integer, which may or may not contain a digit. s is a
character array and getch() is a function which gets the next
character)

if ( isdigit(c) )
while ( s[++i] = c = isdigit(getch()));
When I went back and finally saw what I perceived to be a small
difference, the code worked as intended.

if ( isdigit(c) )
while (isdigit ( s[++i] = c = getch()));

I have not included all the code, as I do not believe it is relevant.
Is this what is happening in the first incorrect code...?
When getch() returns a non-digit, the code stops just as it is supposed
to do, thus prevents assignment of c and s[] so the next line of code
is never reached, or is there something else which I have missed as
usual? :-)

Jun 18 '06 #1
4 1583
mdh wrote:
They say one learns more from one's errors...and I am certainly keeping
that up. The following is code from K&R explaining external
variables...but that is not directly relevant here. When I wrote it, I
initially wrote the code immediately below. But this hangs.

(c is an integer, which may or may not contain a digit. s is a
character array and getch() is a function which gets the next
character)

if ( isdigit(c) )
while ( s[++i] = c = isdigit(getch()));


coz s[++i] = c = isdigit(getch()) is an assignment expression instead
of logic expression, and it's always TRUE except the assigned value is
zero. it's exact the same as you ask why it's different between the
following two lines:

while (x = y) { ... }
while (x==y) { ... }

HTH
Xicheng

Jun 18 '06 #2
mdh

Xicheng Jia wrote:
coz s[++i] = c = isdigit(getch()) is an assignment expression instead
of logic expression, and it's always TRUE except the assigned value is
zero. it's exact the same as you ask why it's different between the
following two lines:

while (x = y) { ... }
while (x==y) { ... }

HTH


Sure does...nice insight for me.
Thanks

Jun 18 '06 #3
On 2006-06-18, mdh <md**@comcast.net> wrote:
They say one learns more from one's errors...and I am certainly keeping
that up. The following is code from K&R explaining external
variables...but that is not directly relevant here. When I wrote it, I
initially wrote the code immediately below. But this hangs.

(c is an integer, which may or may not contain a digit. s is a
character array and getch() is a function which gets the next
character)

if ( isdigit(c) )
while ( s[++i] = c = isdigit(getch()));

You place either a 0 or a !0 into s[++i]. I can't imagine that that is
what you intented.
When I went back and finally saw what I perceived to be a small
difference, the code worked as intended.

Hardly a "small difference".
if ( isdigit(c) )
while (isdigit ( s[++i] = c = getch()));

Now you have assigned the return value of getch() to both c and s[++i],
which is a char. Now, instead of placing a bunch of 0's and 1's into a
string of chars, you've placed character digits.
I have not included all the code, as I do not believe it is relevant.
Is this what is happening in the first incorrect code...?

I can't imagine what the rest of the code looks like, but no, it is not
relevant that I can see.

When getch() returns a non-digit, the code stops just as it is supposed
to do, thus prevents assignment of c and s[] so the next line of code
is never reached, or is there something else which I have missed as
usual? :-)


Something else. :-)

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
I know that area of town like the back of my head.
Jun 18 '06 #4

mdh wrote:
When I wrote it, I
initially wrote the code immediately below. But this hangs.
Meaning what exactly ?
(c is an integer, which may or may not contain a digit. s is a
character array and getch() is a function which gets the next
character)

if ( isdigit(c) )
while ( s[++i] = c = isdigit(getch()));
When I went back and finally saw what I perceived to be a small
difference, the code worked as intended.

if ( isdigit(c) )
while (isdigit ( s[++i] = c = getch()));

I have not included all the code, as I do not believe it is relevant.
Is this what is happening in the first incorrect code...?
When getch() returns a non-digit, the code stops just as it is supposed
to do, thus prevents assignment of c and s[] so the next line of code
is never reached, or is there something else which I have missed as
usual? :-)


The part of the code you do include does not give me an idea as to
why it hangs. One thing to consider is what happens if getch() keeps
returning digits ? Eventually you will go beyond the bounds of s[].

Best to come up with a short *complete* piece of code which exhibits
the problem and post it here.

Jun 18 '06 #5

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

Similar topics

8
by: C. Alexander | last post by:
Im running a small server that seems to work fine outside the IDE as an exe, but within the IDE, when I run the program for the first time, i can start/stop the host many times. But I end the...
1
by: Paul | last post by:
following hangs when re-entered run() { PyEval_RestoreThread(context); pName = PyString_FromString(script); Py_DECREF(pName);
0
by: KS | last post by:
For the second time within 14 days it seems I have to rebuild my pc from scratch: VB.NET won't run - it won't even run a empty form - it just hangs ! I can NOT repair VB.NET - it hangs with 1...
3
by: Bob | last post by:
I'm currently updating one of our web sites and have encountered a strange problem. The page that is giving me the problem is written in ASP and hits a SQL 2K DB. When I click submit I have 4...
3
by: Senthuran | last post by:
Hi, The SQL server 2000 Server hangs some times. It is not periodic. It is not specific in any queries, which are taking more time to execute. Because, it is occurring for different types of...
15
by: Bryan | last post by:
I have a multi-threaded C# console application that uses WMI (System.Management namespace) to make RPC calls to several servers (600+ ) and returns ScheduledJobs. The section of my code that...
3
by: JTF | last post by:
Whenever I start an application from ASP.NET, the exe hangs, with Task manager showing no cpu time. Every exe I've tried hangs in the same way (notepad, cmd, ms access). What runs is a .net exe...
3
by: Lars Netzel | last post by:
Hi! A button triggers the code.. I'm trying to learn about the XmlSerializer and when the code comes the try statement it doesn' throw an exception, it goes into the...
0
by: ngrTerry | last post by:
When I change from the code view to the design view of a form in Visual Studio 2005 (VB.NET) The design environment hangs for up to five minutes with a blank screen where the form should be. I...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.