Thread: Condition testing question

1. Condition testing question

I'm in the process of reading through K&R, and I'm kind of stumped on this one exercise which they suggest; namely, rewriting the following loop so it does not utilize && or ||:

for (i=0; i<lim-1 && (c=getchar()) != '\n' && c != EOF; ++i)
s[i] = c;

I'm really not sure how one would approach this problem, without creating some horrid hack involving multiple nested loops or something like that(the wording seems to stipulate that you only use one one loop, but I could be misinterpreting).

Can someone suggest me a solution(you don't need to write out full code, just a basic explanation of the logic behind the solution should suffice)?

2. Code:
```for( i = 0; i < lim-1; i++ )
{
switch( (c=getchar()) )
{
case EOF: case '\n': break;
default: s[i] = c;
}
}```
I doubt that's what they had in mind. But there's a fun way to do it.

Quzah.

3. Yeah, the book hasn't gotten to switch statements yet. I'm familiar with them, but I'm reading this book from cover to cover because other sources of info I've used have proven themselves flawed, and I don't want to start my programming career with bad habits.

4. Okay, I see. I'm glad I wasn't going mad, after all. I'd come up with all of the solutions posted/linked to, thus far, but never actually bothered to type them out, since I thought there was some new conceptual approach I had utterly failed to understand.

5. Code:
```for (i=0; i < lim-1 ? (c= getchar()) != '\n' ? c != EOF : 0 : 0; ++i)
s[i] = c;```
edit: too late again. bad code anyways.

6. Ah well.

I'm curious, is anyone here up to the task of looking over some proof-of-concept code I've been mucking with?

I'm afraid it really sucks, so I rather don't want everyone on the board gawking at it, but it'd be nice to be able to have someone or other look at it.

7. We live for it.

8. Originally Posted by Aerie
I'm afraid it really sucks, so I rather don't want everyone on the board gawking at it
I don't think anyone will bite your head off. Otherwise I'd probably be a headless zombie by now.

edit: of course you have no way to tell whether I wasn't actually one...

9. Live for looking at the terrible code of complete blithering idiots such as myself?
How unfortunate...

Anyway, this is just another exercise I'm trying to make sure I understand; the one where you replace a tab with enough spaces to format to the next tab stop.

I'm pretty sure I'm doing something wrong here, but I'd like someone to look the code over and A. tell me what is wrong logically, and B. what is wrong syntactically.

Code:
```#include <stdio.h>

#define MAXLEN 1024 /* Set max line length to 1024 */

int tabNuker() {

int newLineMarker, colCtr, strCtr, colWidth;

char strOp[MAXLEN];

strCtr = colCtr = newLineMarker = 0;

printf("What is the width of each tab?");
scanf("%d", &colWidth);

while ((strOp[strCtr] = getchar()) != EOF && strCtr < (MAXLEN -1)) {
if (strOp[strCtr == '\n') {
colCtr = 0;
newLineMarker = strCtr;
}
if ((((strCtr + 1) - newLineMarker) % colWidth) == 0)
++colCtr;
if (c == '\t') {
while (strCtr <= (colCtr * colWidth) && strCtr < (MAXLEN - 1)) {
strOp[strCtr] = ' ';
++strCtr;
}
}
else
++strCtr;
}
}```
Edit: It's nowhere near a complete program, just a function snippet that does the job in question...

Edit2: Fixed some stupid omissions/typos.

Edit3: More stupid typos...

10. Originally Posted by Nyda
I don't think anyone will bite your head off. Otherwise I'd probably be a headless zombie by now.

edit: of course you have no way to tell whether I wasn't actually one...
Braaaaiiiins.......

11. Well, just to make a start, are you sure this works ?

Originally Posted by Aerie
Code:
`while ((strOp[strCtr] = getchar()) != EOF && strCtr < (MAXLEN -1)) {`
What is the result of an assignment? Which type does it have, what type does getchar() return and what type might EOF be of?

12. Well, aside from the fact that I made strOp a char instead of an int, I thought that assigning strOp[strCtr] to getchar() would assign that index of the array(is that the correct term? I get confused with terminology sometimes) to whatever was input, in this case from the keyboard.

I actually made a mistake, since the input assumes that \n would occur as a newline inside a text file - I got mixed up and started treating the input like a stream from a text file, which did foul things up a bit...

Bah. I'll edit this some and repost it.

13. How about something like:
Code:
```#include <stdio.h>

#define TABWIDTH 8

int main(void)
{
int c;
int pos = 0;
int nspaces;

while((c = getchar()) != EOF)
{
switch(c)
{
case '\t':
nspaces = TABWIDTH - (pos % TABWIDTH);
pos += nspaces;
while(nspaces--)
putchar(' ');
break;
case '\n':
putchar(c);
pos = 0;
break;
default:
putchar(c);
pos++;
}
}

return 0;
}```
Then you can just run tabstospaces < inputfile.txt > outputfile.txt

EDIT: I didn't flush stdout before exiting the program which, in retrospect, seems like it might be a good idea.

EDIT 2: Cleaned up the space-creation routine. I should learn to look over my code before posting.

14. Originally Posted by Aerie
Well, aside from the fact that I made strOp a char instead of an int, I thought that assigning strOp[strCtr] to getchar() would assign that index of the array(is that the correct term?
The result of an assignment (a=b) is the value of a, represented in the type of a. a is a char, so the result of your assignment is also a char. Since EOF is not representable as char, your comparison of the result with EOF won't work out.

Code:
```#include <stdio.h>

int main() {
int           b= 300;
unsigned char a;

if ((a= b) == b)
printf("you won't ever see this\n");
else
printf("see?\n");
}```
edit: Whoever wrote this as postrating without signing it, thanks for calling me a thief. I actually did write this on my own.
edit2: Uhm, yes, Dave's link looks awfully similar. So then, I guess you would have expected me to use FALSE if it was my own code - because using that operator there isn't much else that could be done differently. Then give it a try and see for yourself with those headers. Anyways, I guess I should just ignore events like these. Whatever...

Originally Posted by Anonymous Coward
Don't try to pass other's code off as your own. If you copy code then say so and cite the source.

Popular pages Recent additions