Overide for loop condition

This is a discussion on Overide for loop condition within the C Programming forums, part of the General Programming Boards category; Is it possible to overide a for loop condition? I need to enter this for loop for(index = optind; index ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    70

    Overide for loop condition

    Is it possible to overide a for loop condition?

    I need to enter this for loop for(index = optind; index < argc; index++){: and use stdin

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main(int argc, char *argv[]){
      int optchar;
      int index;
      FILE *fp;
      int lineCount = 1;
      int nflag = 0;
      int sflag = 0;
      int vflag = 0;
      int tflag = 0;
      int c;
      int endline = 1;
      int newlineCount = 0;
      int cntrlFlag = 0;
      int keyboardFlag = 0;
    
      while((optchar = getopt (argc, argv, "nsvt")) != -1){
        switch(optchar){
          case 'n':
                nflag = 1;
            break;
          case 's':
                sflag = 1;
            break;
          case 'v':
                vflag = 1;
            break;
          case 't':
                tflag = 1;
            break;
        }
      }
    
      if(argv[2] == NULL){
        keyboardFlag = 1;
      }
    
      for(index = optind; index < argc; index++){
        if(keyboardFlag == 1){
          fp = stdin;
        }else{
          fp = fopen(argv[index], "r");
        }
          if(fp == NULL){
            fprintf(stdout,"File %s couldn't be opened!\n", argv[index]);
          }
            while((c = fgetc(fp)) != EOF){
              cntrlFlag = 0;
              if(c != '\n'){
                newlineCount = 0;
              }
              if(nflag && endline){
                if(sflag && newlineCount < 2){
                  printf("%5d  ", lineCount);
                  lineCount++;
                }
                if(!sflag){
                  printf("%5d  ", lineCount);
                  lineCount++;
                }
                endline = 0;
              }
              if(iscntrl(c) && tflag && c != '\n'){
                c += 64;
                printf("^%c", c);
                cntrlFlag = 1;
              }
              if(newlineCount < 2 && sflag && !cntrlFlag){
                printf("%c", c);
              }
              if(!sflag && !cntrlFlag){
                printf("%c", c);
              }
              if(c == '\n'){
                endline = 1;
                newlineCount++;
              }
            }
        fclose(fp);
      }
      return 0;
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    What do you mean "override"? You can reset index inside the loop as much or to whatever you want.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    You can reset index inside the loop as much or to whatever you want.
    Yes, you can. Be careful doing that however, as it can often lead to unclear, messy code. If you find that becoming a necessity, it's a good sign you might need to refactor your code or something (kind of like with goto).

    In any case, like Dino, I'm not sure what you mean by override. Some other possibility are statements like continue (which terminates execution of the current iteration and restarts the loop in the next iterationg) and break which breaks outside of the current loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. condition variable on read/write locks
    By ShwangShwing in forum C Programming
    Replies: 3
    Last Post: 04-29-2009, 09:32 AM
  2. Condition variables
    By sethjackson in forum Windows Programming
    Replies: 16
    Last Post: 03-19-2008, 11:42 AM
  3. SDL Condition variables.
    By antex in forum Game Programming
    Replies: 3
    Last Post: 11-11-2005, 06:11 AM
  4. Looping condition
    By Chaplin27 in forum C++ Programming
    Replies: 3
    Last Post: 05-29-2005, 02:06 PM
  5. Race condition
    By Roaring_Tiger in forum C Programming
    Replies: 5
    Last Post: 10-24-2004, 09:42 PM

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