Condition testing question

This is a discussion on Condition testing question within the C Programming forums, part of the General Programming Boards category; I'm in the process of reading through K&R, and I'm kind of stumped on this one exercise which they suggest; ...

  1. #1
    Widdle Coding Peon Aerie's Avatar
    Join Date
    Dec 2004
    Posts
    115

    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)?
    I live in a giant bucket.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Widdle Coding Peon Aerie's Avatar
    Join Date
    Dec 2004
    Posts
    115
    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.
    I live in a giant bucket.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Widdle Coding Peon Aerie's Avatar
    Join Date
    Dec 2004
    Posts
    115
    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.
    I live in a giant bucket.

  6. #6
    not-a-geek
    Join Date
    Apr 2004
    Posts
    210
    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.
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  7. #7
    Widdle Coding Peon Aerie's Avatar
    Join Date
    Dec 2004
    Posts
    115
    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.
    I live in a giant bucket.

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    We live for it.
    If you understand what you're doing, you're not learning anything.

  9. #9
    not-a-geek
    Join Date
    Apr 2004
    Posts
    210
    Quote 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...
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  10. #10
    Widdle Coding Peon Aerie's Avatar
    Join Date
    Dec 2004
    Posts
    115
    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...
    Last edited by Aerie; 12-13-2004 at 10:53 AM.
    I live in a giant bucket.

  11. #11
    Widdle Coding Peon Aerie's Avatar
    Join Date
    Dec 2004
    Posts
    115
    Quote 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.......
    I live in a giant bucket.

  12. #12
    not-a-geek
    Join Date
    Apr 2004
    Posts
    210
    Well, just to make a start, are you sure this works ?

    Quote 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?
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  13. #13
    Widdle Coding Peon Aerie's Avatar
    Join Date
    Dec 2004
    Posts
    115
    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.
    I live in a giant bucket.

  14. #14
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    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.
    Last edited by itsme86; 12-13-2004 at 11:48 AM.
    If you understand what you're doing, you're not learning anything.

  15. #15
    not-a-geek
    Join Date
    Apr 2004
    Posts
    210
    Quote 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...

    Quote 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.
    Last edited by Nyda; 12-13-2004 at 01:42 PM.
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Condition question
    By jw232 in forum C++ Programming
    Replies: 1
    Last Post: 02-27-2008, 11:53 PM
  2. Question From"While" Help
    By RahulDhanpat in forum C Programming
    Replies: 13
    Last Post: 02-15-2008, 01:56 PM
  3. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 05:22 AM
  4. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 06:10 AM
  5. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 07:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21