Time to ask?

M

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

p
Printing line 1
Printing line 2
Printing line 3
Printing line 4


**/
 
M

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
 
J

Joe Wright

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.

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.
 
M

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
 
A

Andrew Poelstra

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.
 
J

Joe Wright

MQ said:
You don't have a coding

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.
 
M

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
 
M

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
 
M

mdh

Andrew said:
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.
 
M

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
 
B

Barry Schwarz

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
 
C

Chris F.A. Johnson

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):
p
Printing line 1
Printing line 2
Printing line 3
Printing line 4


**/
 
J

Joe Wright

MQ said:
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.
 
M

mdh

MQ said:
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.
 
K

Keith Thompson

Joe Wright said:
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.
 
A

Andrew Poelstra

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.
 
A

Andrew Poelstra

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.
 
A

Andrew Poelstra

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top