473,320 Members | 1,939 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,320 software developers and data experts.

Time to ask?

mdh
I am still at K&R.

I am in the process of writing this exercise and have run into a wall.
Here is the code in full..( as this is usually asked for). My questions
are;

1) Why does it print 4 lines instead of the expected 2?
2) I am using X-code to compile this....not sure if this has anything
to do with it. When I step through the code, I get as far as where I
have written "HANGS HERE" and it goes no further. I am assuming these 2
are related? or not? ...But, I think I need some smarter minds at this
point.

Thanks in advance./

# include <stdio.h>
# include <string.h>
# define MAXNUM 10
# define TESTCHAR 'T'

int getops(char s[]);
int main (){
char s[MAXNUM], type;
while ( (type=getops(s)) != EOF){
switch (type) {

case 'T':
printf("%s\n", s);
break;
}

}

return 0;
}
/*****GETOPS******/

int getch(void);
void ungetch(int c);
int getops(char s[]){

int c, i;
while ( (c = getch()) == ' ' || c =='\t');
for ( i=0; i <= 1; i++) /*****HANGS HERE***/
ungetch('T');
}


/***** GETCH & UNGETCH*****/

# define MAXBUFF 10
char buffer[MAXBUFF];
int buffp=0;
int j=1;
int getch(void){
return (buffp 0) ? buffer[--buffp]: getchar() ;

}

void ungetch(int c){
printf("Printing line %d\n", j++);

}
/******

Output("p" entered):

>>/beginning of output/<<
p
Printing line 1
Printing line 2
Printing line 3
Printing line 4
>>/end of output/<<

**/

Jul 5 '06 #1
73 2810
MQ
int getops(char s[]){

int c, i;
while ( (c = getch()) == ' ' || c =='\t');
for ( i=0; i <= 1; i++) /*****HANGS HERE***/
ungetch('T');
}
First thing I have noticed is that getops does not return anything.
Could this be the problem?

MQ

Jul 5 '06 #2
mdh wrote:
I am still at K&R.

I am in the process of writing this exercise and have run into a wall.
Here is the code in full..( as this is usually asked for). My questions
are;

1) Why does it print 4 lines instead of the expected 2?
2) I am using X-code to compile this....not sure if this has anything
to do with it. When I step through the code, I get as far as where I
have written "HANGS HERE" and it goes no further. I am assuming these 2
are related? or not? ...But, I think I need some smarter minds at this
point.

Thanks in advance./

I have taken the liberty of snipping your code. You don't have a coding
problem. You have an Understanding problem. getch() and ungetch() are
not Standard C functions. They may be implemented as extensions. Borland
and DJGPP come to mind, declared in conio.h and there are surely others.
They are not Standard C.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jul 5 '06 #3
MQ
You don't have a coding
problem. You have an Understanding problem. getch() and ungetch() are
not Standard C functions.
The OP has implemented these functions. Take a look at the bottom of
the listing.

MQ

Jul 5 '06 #4
On 2006-07-05, mdh <md**@comcast.netwrote:
I am still at K&R.
That reminds me to check the shipping status of my copy.
I am in the process of writing this exercise and have run into a wall.
Here is the code in full..( as this is usually asked for).
Absolutely. Thanks very much.
My questions are;

1) Why does it print 4 lines instead of the expected 2?
2) I am using X-code to compile this....not sure if this has anything
to do with it. When I step through the code, I get as far as where I
have written "HANGS HERE" and it goes no further. I am assuming these 2
are related? or not? ...But, I think I need some smarter minds at this
point.
XCode is (I believe) a port of gcc, which might be compiling GNU C. This
shouldn't be an issue for conforming code, but it could be allowing
erroneous code to go by undiagnosed.

I'm assuming that your code below compiles okay, and so I won't
look too closely for typos. You copy-and-pasted the code, right?
(As opposed to retyping)
>
# include <stdio.h>
# include <string.h>
# define MAXNUM 10
# define TESTCHAR 'T'
Formatting! I've corrected the spacing from here on. Remember that
tabs never display correctly, and often don't display at all on
Usenet. If you didn't put any spacing at all, you should consider doing
so.

int getops(char s[]);
int main () {
char s[MAXNUM], type;
while ((type=getops(s)) != EOF){
switch (type) {
case 'T':
printf("%s\n", s);
break;
}
}

return 0;
}

/*****GETOPS******/

int getch(void);
void ungetch(int c);
Keep your prototypes all at the top so they're easy to find.
int getops(char s[]){
int c, i;
while ((c = getch()) == ' ' || c =='\t');
for (i=0; i <= 1; i++) /*****HANGS HERE***/
The loop itself is fine...
ungetch('T');
....so it's obviously hanging in your ungetch function.
}
/***** GETCH & UNGETCH*****/

# define MAXBUFF 10
Defines go at the top as well.
char buffer[MAXBUFF];
int buffp=0;
int j=1;
Avoid globals at all costs.
int getch(void){
return (buffp 0) ? buffer[--buffp]: getchar() ;
This will always return getchar(), because buffp is never above 0.
}

void ungetch(int c){
printf("Printing line %d\n", j++);
Why are you incrementing j? It isn't referenced anywhere else
in this entire code.
}
It clearly isn't freezing here, so I have to assume that it isn't hanging.
>
/******

Output("p" entered):

>>>/beginning of output/<<
p
Printing line 1
Printing line 2
Printing line 3
Printing line 4
>>>/end of output/<<


**/

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>
To email me, use "apoelstra" at the above address.
"You people hate mathematics." -- James Harris
Jul 5 '06 #5
MQ wrote:
You don't have a coding
>problem. You have an Understanding problem. getch() and ungetch() are
not Standard C functions.

The OP has implemented these functions. Take a look at the bottom of
the listing.

MQ
You can't implement getch() in terms of getchar(). Note that getchar()
blocks until newline. This allows editing the line before submitting it
to the program. This is called I/O buffering. The getch() construct
expects exactly one character per keypress in real time. This is rife
with complications, and again, is not Standard C.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jul 5 '06 #6
MQ
It clearly isn't freezing here, so I have to assume that it isn't hanging.
It will go into an infinte loop in the main() function, because type !=
EOF will always be true

Jul 5 '06 #7
MQ

You can't implement getch() in terms of getchar().
That's not my point. My point is that you said getch() and ungetch()
were not implemented, but they clearly are in this case

Jul 5 '06 #8
mdh

Andrew Poelstra wrote:
I'm assuming that your code below compiles okay, and so I won't
look too closely for typos. You copy-and-pasted the code, right?
Correct.

>
Keep your prototypes all at the top so they're easy to find.
OK...
...so it's obviously hanging in your ungetch function.
Yes...I agree....I have had this situation before where it compiles and
runs fine, but when I step through it, it hangs...as here.

Jul 5 '06 #9
mdh
Andrew Poelstra wrote:
Why are you incrementing j?

Just trying to understand why the code was doing what it was doing :-)

Jul 6 '06 #10
MQ
ah, found it

You have a semicolon straight after the while statement in getops().
This will produce the hanging, becuase you will have an infinite loop.
Remove this and it should be fine. Also make sure that getops()
returns something or you will have another infinite loop in main()

MQ

Jul 6 '06 #11
On 5 Jul 2006 15:38:02 -0700, "mdh" <md**@comcast.netwrote:
>I am still at K&R.

I am in the process of writing this exercise and have run into a wall.
Here is the code in full..( as this is usually asked for). My questions
are;

1) Why does it print 4 lines instead of the expected 2?
2) I am using X-code to compile this....not sure if this has anything
to do with it. When I step through the code, I get as far as where I
have written "HANGS HERE" and it goes no further. I am assuming these 2
are related? or not? ...But, I think I need some smarter minds at this
point.

Thanks in advance./
snip
>/*****GETOPS******/

int getch(void);
void ungetch(int c);
int getops(char s[]){

int c, i;
while ( (c = getch()) == ' ' || c =='\t');
Any chance it is actually hanging in the while waiting for getch to
return, which in turn is waiting for getchar to return, which in turn
is waiting for you to enter some data plus hit the ENTER key?
>for ( i=0; i <= 1; i++) /*****HANGS HERE***/
ungetch('T');
}


/***** GETCH & UNGETCH*****/

# define MAXBUFF 10
char buffer[MAXBUFF];
int buffp=0;
int j=1;
int getch(void){
return (buffp 0) ? buffer[--buffp]: getchar() ;

}
snip
Remove del for email
Jul 6 '06 #12
MQ wrote:
>
>You can't implement getch() in terms of getchar().

That's not my point. My point is that you said getch() and ungetch()
were not implemented, but they clearly are in this case
Do me a favor please. Correct me when I'm wrong. Otherwise read for
enlightenment. The declaration and definition of a function named
getch() in a program is NOT an implementation of it. Really.

The implementation is the compiler suite for your particular platform
provided by whoever you got it from. Theoretically, the Implementor
knows more about this than you (the Programmer/User) knows. You don't
implement anything here.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jul 6 '06 #13
On 2006-07-05, mdh wrote:
I am still at K&R.

I am in the process of writing this exercise and have run into a wall.
Here is the code in full..( as this is usually asked for). My questions
are;

1) Why does it print 4 lines instead of the expected 2?
[snip]
Output("p" entered):
Did you type "p" or "p<ENTER>"? If the latter, it prints four
lines because you have entered two characters.
>>>/beginning of output/<<
p
Printing line 1
Printing line 2
Printing line 3
Printing line 4
>>>/end of output/<<


**/
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Jul 6 '06 #14
mdh
MQ wrote:
ah, found it

Hi MQ,
I think that is correct, as it simply eliminates tabs and spaces. But,
I have found something that does not make sense.

The statement:

for ( i=0; i <= 10; i++)
ungetch('T');

does not increment "i" when it returns from ungetch. Not sure if this
has something to do with it.

Jul 6 '06 #15
mdh

Chris F.A. Johnson wrote:
> Did you type "p" or "p<ENTER>"? If the latter, it prints four
lines because you have entered two characters.
>>/beginning of output/<<
Yes I agree...now just trying to figure out the "hanging" but I think I
found a clue...above.

Jul 6 '06 #16
Joe Wright <jo********@comcast.netwrites:
MQ wrote:
>>You can't implement getch() in terms of getchar().
That's not my point. My point is that you said getch() and ungetch()
were not implemented, but they clearly are in this case
Do me a favor please. Correct me when I'm wrong. Otherwise read for
enlightenment. The declaration and definition of a function named
getch() in a program is NOT an implementation of it. Really.

The implementation is the compiler suite for your particular platform
provided by whoever you got it from. Theoretically, the Implementor
knows more about this than you (the Programmer/User) knows. You don't
implement anything here.
Yes, I think you're wrong.

"getch" is just another identifier. There is nothing by that name in
the standard library, and it's in the user namespace. If I declare
and define a function called getch(), that's just as much an
implementation of getch() as a declaration and definition of foobar()
is an implementation of foobar() -- and I can make my getch() function
do whatever I like.

It happens that there are are functions named "getch" in other
libraries, the curses/ncurses library and MS Windows (and they're
incompatible with each other). Because of that, I would probably
avoid using that identifier in my own code -- but there's nothing in
the C standard that forbids it, or even discourages it. If an
implementation prevents me from declaring my own getch() when I
haven't #included any system-specific header that declares it, then
that implementation is non-conforming.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 6 '06 #17
On 2006-07-05, MQ <mi**************@gmail.comwrote:
>
>It clearly isn't freezing here, so I have to assume that it isn't hanging.

It will go into an infinte loop in the main() function, because type !=
EOF will always be true
I could check for that, but you snipped all of the code. Remember that
not everyone can look back at old messages.

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>
To email me, use "apoelstra" at the above address.
"You people hate mathematics." -- James Harris
Jul 6 '06 #18
On 2006-07-06, mdh <md**@comcast.netwrote:
Andrew Poelstra wrote:
>...so it's obviously hanging in your ungetch function.

Yes...I agree....I have had this situation before where it compiles and
runs fine, but when I step through it, it hangs...as here.
Ah, but when I checked the ungetch() function, there was nothing to
indicate a hang. In fact, it was nothing but a printf().

MQ mentioned that it might hang on a test for EOF somewhere, but I
don't have the code with me to confirm or correct that.

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>
To email me, use "apoelstra" at the above address.
"You people hate mathematics." -- James Harris
Jul 6 '06 #19
On 2006-07-06, mdh <md**@comcast.netwrote:
Andrew Poelstra wrote:
>Why are you incrementing j?

Just trying to understand why the code was doing what it was doing :-)
Okay, but bear in mind that without the increment, the function is nothing
but a printf. That function has no side effects, and certainly isn't
hanging the program. Your bug must be somewhere else.

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>
To email me, use "apoelstra" at the above address.
"You people hate mathematics." -- James Harris
Jul 6 '06 #20
On 2006-07-06, MQ <mi**************@gmail.comwrote:
ah, found it

You have a semicolon straight after the while statement in getops().
This will produce the hanging, becuase you will have an infinite loop.
Remove this and it should be fine. Also make sure that getops()
returns something or you will have another infinite loop in main()
1) Quote context.
2) Your last statement isn't necessarily true, because falling off of
a non-void function is undefined, and may very well terminate the loop
in main, overwrite system data, or draw demons from your nose.

Good catch, though. Perhaps I should have looked for typos. :-)

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>
To email me, use "apoelstra" at the above address.
"You people hate mathematics." -- James Harris
Jul 6 '06 #21
On 2006-07-06, mdh <md**@comcast.netwrote:
MQ wrote:
>ah, found it


Hi MQ,
I think that is correct, as it simply eliminates tabs and spaces. But,
I have found something that does not make sense.

The statement:

for ( i=0; i <= 10; i++)
ungetch('T');

does not increment "i" when it returns from ungetch. Not sure if this
has something to do with it.
How do you know that i is not incremented?

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>
To email me, use "apoelstra" at the above address.
"You people hate mathematics." -- James Harris
Jul 6 '06 #22
mdh

Andrew Poelstra wrote:
The statement:

for ( i=0; i <= 10; i++)
ungetch('T');

does not increment "i" when it returns from ungetch. Not sure if this
has something to do with it.

How do you know that i is not incremented?
Well, on stepping through it, the first call to ungetch() does not
hang. When the code returns after ungetch, i is still "0". I would have
expected it to be "1". If I try the next step, it hangs.

Jul 6 '06 #23
On 2006-07-06, mdh <md**@comcast.netwrote:
>
Andrew Poelstra wrote:
The statement:

for ( i=0; i <= 10; i++)
ungetch('T');

does not increment "i" when it returns from ungetch. Not sure if this
has something to do with it.

How do you know that i is not incremented?
Well, on stepping through it, the first call to ungetch() does not
hang. When the code returns after ungetch, i is still "0". I would have
expected it to be "1". If I try the next step, it hangs.
Here's your `for' loop, with your step commented in:

i = 0;
topOfLoop:
if (i <= 10)
{
ungetch ('T');
/* i is still 0. */
i++;
goto topOfLoop;
}

Does your debugger hang or the code? The next step shouldn't
cause any problems.

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>
To email me, use "apoelstra" at the above address.
"You people hate mathematics." -- James Harris
Jul 6 '06 #24
mdh

Andrew Poelstra wrote:
Here's your `for' loop, with your step commented in:

i = 0;
topOfLoop:
if (i <= 10)
{
ungetch ('T');
/* i is still 0. */
i++;
goto topOfLoop;
}

Does your debugger hang or the code?

Yes!!! And the interesting thing is that when the code returns from
ungetch, it does not increment i either.

I think another poster mentioned that a return from a void function is
undefined. I do not know enough exactly to know the implication of
this, but I wonder if this has something to do with it?

Jul 6 '06 #25
On 2006-07-06, mdh <md**@comcast.netwrote:
>
Andrew Poelstra wrote:
>Here's your `for' loop, with your step commented in:

i = 0;
topOfLoop:
if (i <= 10)
{
ungetch ('T');
/* i is still 0. */
i++;
goto topOfLoop;
}

Does your debugger hang or the code?

Yes!!! And the interesting thing is that when the code returns from
ungetch, it does not increment i either.
Which one? It will increment i if you give it the chance.
I think another poster mentioned that a return from a void function is
undefined. I do not know enough exactly to know the implication of
this, but I wonder if this has something to do with it?
It's certainly possible. UB (undefined behavior) can cause anything
imaginable.

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>
To email me, use "apoelstra" at the above address.
"You people hate mathematics." -- James Harris
Jul 6 '06 #26
mdh

Andrew Poelstra wrote:
>Here's your `for' loop, with your step commented in:
>
i = 0; <<<---1
topOfLoop:
if (i <= 10) <<<---2
{
ungetch ('T'); <<<---3
/* i is still 0. */
i++; <<<---4
goto topOfLoop;
}

Does your debugger hang or the code?
Yes!!! And the interesting thing is that when the code returns from
ungetch, it does not increment i either.

Which one? It will increment i if you give it the chance.

Well, as you step through,<<<- 1 ,<<<- 2,<<<- 3 are successful.
Ungetch() executes the first time. The call is returned to <<--4, and
it hangs there. i is never incremented.

Jul 6 '06 #27
On 2006-07-06, mdh <md**@comcast.netwrote:
Andrew Poelstra wrote:
>>Here's your `for' loop, with your step commented in:

i = 0; <<<---1
topOfLoop:
if (i <= 10) <<<---2
{
ungetch ('T'); <<<---3
/* i is still 0. */
i++; <<<---4
goto topOfLoop;
}

Does your debugger hang or the code?

Yes!!! And the interesting thing is that when the code returns from
ungetch, it does not increment i either.

Which one? It will increment i if you give it the chance.

Well, as you step through,<<<- 1 ,<<<- 2,<<<- 3 are successful.
Ungetch() executes the first time. The call is returned to <<--4, and
it hangs there. i is never incremented.
Well, you need to fix all of the problems with your code that various
people have pointed out. That should eliminate the undefined behavior.
From there, post the corrected code if it still doesn't work.

Until you do that, it could be a bug in your debugger just as easily
as it could be your faulty code.

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>
To email me, use "apoelstra" at the above address.
"You people hate mathematics." -- James Harris
Jul 6 '06 #28
"mdh" <md**@comcast.netwrites:
[...]
I think another poster mentioned that a return from a void function is
undefined. I do not know enough exactly to know the implication of
this, but I wonder if this has something to do with it?
No, a return from a void function isn't undefined.

A return statement that returns a value is illegal in a function
declared to return void. I presume that's not your problem; if it
were, your program wouldn't have compiled.

A return statement *without* an expression in a function that returns
a type other than void is legal in C90, but illegal in C99. It's a
bad idea in any case. At best, it's going to return garbage to the
caller, which will presumbably be expecting something sensible.

Falling off the end of a function (i.e., reaching the closing "}"
without executing a return) is ok if the function returns void, and a
bad idea otherwise. For a non-void function, this will also return a
garbage value to the caller; the caller's attempt to use the result
invokes undefined behavior.

I wasn't able to find the code in question, so I don't know whether
any of this applies to your program. If it does, fix it; there's not
much point in doing anything else until you've fixed any known bugs.
(That's not *always* true; there are times when you can be sure that a
given bug isn't the cause of the misbehavior you're seeing. But such
assumptions can easily be wrong.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 6 '06 #29
mdh said:
I am still at K&R.

I am in the process of writing this exercise and have run into a wall.
Here is the code in full..( as this is usually asked for). My questions
are;

1) Why does it print 4 lines instead of the expected 2?
2) I am using X-code to compile this....not sure if this has anything
to do with it. When I step through the code, I get as far as where I
have written "HANGS HERE" and it goes no further. I am assuming these 2
are related? or not? ...But, I think I need some smarter minds at this
point.
It doesn't actually hang on my system, and neither can I see any immediate
reason why it would. It does block for input, but as long as I keep it fed
it continues to execute quite happily.

It just doesn't do anything.

I think you're expecting getops() to do something cool, but at present it
doesn't have any code in it other than "skip white space" and "print two
numbers".

What were you expecting to happen?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 6 '06 #30
MQ
Do me a favor please. Correct me when I'm wrong.
I believe I did

Jul 6 '06 #31
MQ

1) Quote context.
Sorry Andrew, is this because some newreaders cannot view previous
messages?

MQ

Jul 6 '06 #32
mdh

Keith Thompson wrote:
>I wasn't able to find the code in question, so I don't know whether
any of this applies to your program. If it does, fix it; there's not
much point in doing anything else until you've fixed any known bugs.
I think all bugs are now fixed...but i still get the same issue. I have
stripped down the project to this: It now compiles and runs and prints
the expected output.

viz:
Printing line 1
Printing line 2
Printing line 3
Printing line 4
Printing line 5
Printing line 6
Printing line 7
Printing line 8
Printing line 9
Printing line 10
Printing line 11

but it hangs if I am stepping through it (Where indicated). Perhaps
some input from Xcode is warranted? ( I have posted there, but not too
much help yet)

# include <stdio.h>
# define MAXNUM 10
int getops(char s[]);

int main (){

char s[MAXNUM], type;
while ( (type=getops(s)) != EOF){

switch (type) {

case 'T':
printf("%s\n", s);
break;
}

}

return 0;
}
/*****GETOPS******/
void ungetch(int c);
int getops(char s[]){
int i;

/**while ( (c = getch()) == ' ' || c =='\t'); **/

/*for ( i=0; i <= 10; i++)
ungetch('T');*/
i = 0;
topOfLoop:
if (i <= 10)
{
ungetch ('T');
/* i is still 0. */
i++; /** Hangs here after return from ungetch in debugger**/
goto topOfLoop;
}

return EOF;

}

/***** GETCH & UNGETCH*****/
int j=1;
void ungetch(int c){
printf("Printing line %d\n", j++);

}

Jul 6 '06 #33
MQ said:
>
>1) Quote context.

Sorry Andrew, is this because some newreaders cannot view previous
messages?
It's unwise to assume otherwise. It's also unwise to assume that everyone's
newsreader is capable of correctly assembling threads. It's also unwise to
assume everybody gets their newsfeeds in the same order that you do. It's
also unwise to assume that a previous message is retained on all servers
for the same length of time it's retained on yours. For all these reasons
and more, quote sufficient context to remind people of the particular
subject you are talking about.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 6 '06 #34
mdh

Richard Heathfield wrote:
>
What were you expecting to happen?

Richard, I started off trying to do something else. Got side-tracked by
the "debugging" bug. Still getting it.(See my answer to Keith Thompson
above). The reason I am pursuing this is that it has happened before,
where it compiles and runs, but then in the debugger it hangs.

Jul 6 '06 #35
mdh said:
>
Richard Heathfield wrote:
>>
What were you expecting to happen?


Richard, I started off trying to do something else. Got side-tracked by
the "debugging" bug. Still getting it.(See my answer to Keith Thompson
above). The reason I am pursuing this is that it has happened before,
where it compiles and runs, but then in the debugger it hangs.
Your program periodically requires input. How are you supplying this input?

The debugger may /appear/ to hang when your program is expecting input. Find
out how to supply input during the program run.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 6 '06 #36
"MQ" <mi**************@gmail.comwrites:
>Do me a favor please. Correct me when I'm wrong.

I believe I did
Without knowing who wrote "Correct me when I'm wrong", or what it was
about, the above makes very little sense.

Please don't snip attribution lines (the "So-and-so writes:" lines),
and please leave in enough context so your followup makes sense on its
own.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 6 '06 #37
MQ
It's unwise to assume otherwise. It's also unwise to assume that
everyone's

ok thanks, my bad...

Jul 6 '06 #38
MQ

Keith Thompson wrote:
Without knowing who wrote "Correct me when I'm wrong", or what it was
about, the above makes very little sense.

Please don't snip attribution lines (the "So-and-so writes:" lines),
and please leave in enough context so your followup makes sense on its
own.
The poster responsible will know...but thanks, I will in future (and in
present as you can see :)

Jul 6 '06 #39
MQ said:
>
Keith Thompson wrote:
>Without knowing who wrote "Correct me when I'm wrong", or what it was
about, the above makes very little sense.

Please don't snip attribution lines (the "So-and-so writes:" lines),
and please leave in enough context so your followup makes sense on its
own.

The poster responsible will know...
Not necessarily. Between writing his article and reading your reply, the
poster may have dealt with dozens or even hundreds of other articles on a
number of subjects in various newsgroups. Why should he remember one minor
discussion among many, purely on the basis of five or six words?

Watch to see how others quote context. Most people get it right, pretty
much. Emulate them.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 6 '06 #40
"MQ" <mi**************@gmail.comwrites:
Keith Thompson wrote:
>Without knowing who wrote "Correct me when I'm wrong", or what it was
about, the above makes very little sense.

Please don't snip attribution lines (the "So-and-so writes:" lines),
and please leave in enough context so your followup makes sense on its
own.

The poster responsible will know...but thanks, I will in future (and in
present as you can see :)
The poster responsible may or may not know, as Richard just posted.
But in any case, you're posting to a newsgroup, not just to the author
of the article to which you're following up.

And thanks for providing context.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 6 '06 #41
On 2006-07-06, MQ <mi**************@gmail.comwrote:
>
>1) Quote context.

Sorry Andrew, is this because some newreaders cannot view previous
messages?
Yep. For example, my reader discards messages once I've read them and exited
the program. It would take far too much effort to grab past messages, and
in all likelihood my ISP has deleted any old messages from its server.

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>
To email me, use "apoelstra" at the above address.
"You people hate mathematics." -- James Harris
Jul 6 '06 #42
mdh

Richard Heathfield wrote:
mdh said:
>
Your program periodically requires input. How are you supplying this input?
There is a standard input available during debugging. One of the poster
earlier mentioned that ungetch() is a named function in C. It turns out
that Xcode objected to the use of a "modified" version of ungetch(). On
changing the function name to my_ungetch() it no longer hangs during
debugging. Not sure why it does work when compiled normally(using
ungetch only), but in any case, that seems to be the explanation for
the hanging.

Jul 6 '06 #43
mdh said:
>
Richard Heathfield wrote:
>mdh said:
>>
Your program periodically requires input. How are you supplying this
input?

There is a standard input available during debugging. One of the poster
earlier mentioned that ungetch() is a named function in C.
In fact it is not a standard C function.
It turns out
that Xcode objected to the use of a "modified" version of ungetch().
Compilers are not permitted, when invoked in conforming mode, to use the
name ungetch(). It is possible that you did not invoke the compiler in
conforming mode.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 6 '06 #44
mdh

Richard Heathfield wrote:
>>
Compilers are not permitted, when invoked in conforming mode, to use
the
name ungetch(). It is possible that you did not invoke the compiler in
conforming mode.
Sorry, am not familiar with the notion of "conforming mode". Will look
it up, thanks.

Jul 6 '06 #45
"mdh" <md**@comcast.netwrites:
Richard Heathfield wrote:
>Compilers are not permitted, when invoked in conforming mode, to
use the name ungetch(). It is possible that you did not invoke the
compiler in conforming mode.

Sorry, am not familiar with the notion of "conforming mode". Will look
it up, thanks.
(The quoting level was messed up; I think I've corrected it.)

Compilers commonly have options (command-line or otherwise) that make
them behave differently. A compiler will typically have a set of
options that cause it to behave as a conforming C compiler; other sets
of options may enable extensions that, while perhaps useful, can break
conforming code.

"ungetch" happens to be the name of a non-standard function provide by
the curses or ncurses library, but as far as the C standard is
concerned, it's just another identifier. A conforming C compiler must
allow you to define "ungetch" for yourself in any way you like, as
long as you don't have a #include for some non-standard header that
defines it. A compiler that gives you trouble because you've used
that identifier is not conforming. There *should* be a way to tell
the compiler to behave in a conforming manner; it may or may not be
the default.

On the other hand, avoiding such identifiers isn't a bad idea. If you
define your own ungetch() function, and later decide to have your
program use the curses library, you're going to have problems. But
nobody can reasonably be expected to keep track of all the identifiers
declared by every third-party library in existence, and the C standard
is defined in such a way that you don't have to.

<OT>
Something like C++'s namespaces would make this easier. Rather than
ungetch(), curses could define curses::ungetch(), and your own
my_library::ungetch() wouldn't conflict with it. You'd just have to
ensure that each namespace has a unique name, not each identifier in
every library. A common workaround in C is to use a common prefix as
part of each identifier, but that can get very ugly very quickly.
</OT>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 6 '06 #46
Joe Wright wrote:
Do me a favor please. Correct me when I'm wrong. Otherwise read for
enlightenment.
Heh, sweeter words have never been uttered.
The declaration and definition of a function named getch() in a
program is NOT an implementation of it. Really.
Actually, it is.
The implementation is the compiler suite for your...
Actually, the implementation of anything is not your compiler. Your
compiler takes the code implemented by you and by your compiler vendor
and (hopefully) makes a software program which can be executed on the
target platform.

So yeah, read and learn.

-tom!
Jul 6 '06 #47
mdh wrote:
...so it's obviously hanging in your ungetch function.

Yes...I agree....I have had this situation before where it compiles and
runs fine, but when I step through it, it hangs...as here.
Maybe it's not hanging, but rather waiting for user input?
-tom!
Jul 6 '06 #48
On Thu, 06 Jul 2006 14:08:26 -0700, in comp.lang.c , Tom Plunket
<ga*****@fancy.orgwrote:
>Joe Wright wrote:
>The declaration and definition of a function named getch() in a
program is NOT an implementation of it. Really.

Actually, it is.
Actually, I agree. AN implementation of getch() was provided.
>The implementation is the compiler suite for your...

Actually, the implementation of anything is not your compiler.
In the context of ISO C it is. The key word is "the".
>Your compiler takes the code implemented by you
Thats not the definition of "the implementation" that the C standard,
and by extension people round here, use.

The Implementation is the environment in which you convert your code
into some sort of excutable product. You, the programmer, may not mess
with it, nor wander into its namespace, etc.
>So yeah, read and learn.
indeed.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Jul 6 '06 #49
Keith Thompson wrote:
Joe Wright <jo********@comcast.netwrites:
>MQ wrote:
>>>You can't implement getch() in terms of getchar().
That's not my point. My point is that you said getch() and ungetch()
were not implemented, but they clearly are in this case
Do me a favor please. Correct me when I'm wrong. Otherwise read for
enlightenment. The declaration and definition of a function named
getch() in a program is NOT an implementation of it. Really.

The implementation is the compiler suite for your particular platform
provided by whoever you got it from. Theoretically, the Implementor
knows more about this than you (the Programmer/User) knows. You don't
implement anything here.

Yes, I think you're wrong.

"getch" is just another identifier. There is nothing by that name in
the standard library, and it's in the user namespace. If I declare
and define a function called getch(), that's just as much an
implementation of getch() as a declaration and definition of foobar()
is an implementation of foobar() -- and I can make my getch() function
do whatever I like.

It happens that there are are functions named "getch" in other
libraries, the curses/ncurses library and MS Windows (and they're
incompatible with each other). Because of that, I would probably
avoid using that identifier in my own code -- but there's nothing in
the C standard that forbids it, or even discourages it. If an
implementation prevents me from declaring my own getch() when I
haven't #included any system-specific header that declares it, then
that implementation is non-conforming.
Why wrong? I didn't say you couldn't declare and define you own getch().
Of course you can and your getch() can do anything you like, maybe
generate a list of local churches. Anything.

I meant to say the function getch() that we know and love from conio.h,
ncurses and friends can't be successfully written in terms of getchar().
Further I meant to take issue with 'implementation'.

I do regret the tone of my post. The 'read for enlightenment' was a
little over the top. Sorry all.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jul 6 '06 #50

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

Similar topics

8
by: Bart Nessux | last post by:
am I doing this wrong: print (time.time() / 60) / 60 #time.time has been running for many hours if time.time() was (21600/60) then that would equal 360/60 which would be 6, but I'm not getting...
5
by: David Stockwell | last post by:
I'm sure this has been asked before, but I wasn't able to find it. First off I know u can't change a tuple but if I wanted to increment a time tuple by one day what is the standard method to do...
6
by: David Graham | last post by:
Hi I have asked this question in alt.php as the time() function as used in setcookie belongs to php - or does it belong equally in the javascript camp - bit confused about that. Anyway, can anyone...
3
by: Szabolcs Nagy | last post by:
I have to measure the time of a while loop, but with time.clock i always get 0.0s, although python manual sais: "this is the function to use for benchmarking Python or timing algorithms" So i...
6
by: Rebecca Smith | last post by:
Today’s question involves two time text boxes each set to a different time zone. Initially txtCurrentTime will be set to Pacific Time or system time. This will change with system time as we travel...
3
by: luscus | last post by:
Thanks for all the responses on my first question. Unfortunately the answers I was given were too complicated for my small brain , and neophite condition to understand. So if you could talk down to...
3
by: cj | last post by:
If I want to check to see if it's after "11:36 pm" what would I write? I'm sure it's easy but I'm getting tired of having to work with dates and times. Sometimes I just want time or date. And...
1
by: davelist | last post by:
I'm guessing there is an easy way to do this but I keep going around in circles in the documentation. I have a time stamp that looks like this (corresponding to UTC time): start_time =...
2
by: Roseanne | last post by:
We are experiencing very slow response time in our web app. We run IIS 6 - windows 2003. I ran iisstate. Here's what I got. Any ideas?? Opened log file 'F:\iisstate\output\IISState-812.log'...
9
by: Ron Adam | last post by:
I'm having some cross platform issues with timing loops. It seems time.time is better for some computers/platforms and time.clock others, but it's not always clear which, so I came up with the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.