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
**/
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
**/