Thread: if / else if / else statement not functioning properly

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    100

    if / else if / else statement not functioning properly

    I'm having a very peculiar problem with my if / else if / else statement block. Certain inputs seems to be accessing multiple parts of the block, and some statements don't seem to be working at all. Here is my code:

    Code:
    int cc;
        ii = 0;
        for(cc = 0; cc < 1; cc--) { // infinite loop
            fgets(aa[ii], LINE_SZ, stdin);  // LINE_SZ = 90
    
            if(strlen(aa[ii]) == 1) // WORKS
                exit(0);
    
            if(strlen(aa[ii]) > 80) {
    
                fprintf(stderr, "Inside > 80\n");   
        
            } else if(aa[ii][0] == '<') {
                fprintf(stderr, "Inside <");
    
                // pop
                if(ii == 0) {
                    fprintf(stderr, "Empty stack.\n");
                } else {
                    printf("ii: &#37;d", ii);
                    fprintf(stdout, aa[ii-1]);
                }
    
            } else if(aa[ii][0] == '>') {
                fprintf(stderr, "Inside >\n");
    
                // push
                if(ii == stackSize) {
                    fprintf(stderr, "Full stack.\n");
                } else {
                    strcpy(aa[ii], aa[ii]+1);
                    ii++;
                }
    
            } else {
    
                // bad input
                fprintf(stderr, "Inside else - Bad input.\n");
    
            } 
                    
        }
    Even if my input line (aa[ii]) is over 80 chars long, it displays "Inside > 80" but then still goes to the else statement and displays "Inside else - Bad input."
    Last edited by Beowolf; 09-11-2007 at 07:47 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    but then still goes to the else statement and displays "Inside else - Bad input."
    ... on the next iteration, perhaps?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    What do you mean? I only enter one line of input in the test.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What do you mean? I only enter one line of input in the test.
    Ah, but your loop is an infinite loop. Incidentally, since you never use cc, you could use for ( ;; ) if you really want an infinite loop.

    Still, why not print cc to check which iteration of the loop you are in?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    Hmm between the first if statement for the 80 byte check and the else statement, CC is decreasing by 1, meaning it is going through the loop again. But why would it do that?

    That would explain why the else is executing next - because when it goes through again, it is picking up a null value, so it is below 80 strlen and is not < or >, so else executes.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    I understand what it is doing now. If my input is greater than 80, the fgets line only takes the first 80 characters because that is what is specified by LINE_SZ. But say if my input is 91 chars long, that means 11 chars are left over. So when the if-else statements are done executing, fgets automatically retrieves those 11 chars, and since they do not begin with > or <, it labels them as bad input.

    I include fflush(stdin) inside my if statement checking for over 80 chars, and it seems to work now.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I include fflush(stdin) inside my if statement checking for over 80 chars, and it seems to work now.
    You have the right idea, but the wrong implementation. Read the FAQ on Why fflush(stdin) is wrong.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > strcpy(aa[ii], aa[ii]+1);
    Very few standard C functions are guaranteed to work on overlapping memory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And, this is not infinite - just VERY LONG:
    Code:
    for(cc = 0; cc < 1; cc--) { // infinite loop
    It may be infinite for the purposes of this code, but it will run out after it wraps around to positive numbers. If what you want is an infinite loop, there are four options, listed here in the order I would recommend using them:
    Code:
    // 1
      for(;;) ... 
    
    // 2
      while(1) ...
    
    // 3 
      do { ... } while(1);
    
    // 4
    Label:
      ... 
      goto Label;     // Not really a good option in most cases.
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. terminal output not showing output properly
    By stanlvw in forum C Programming
    Replies: 13
    Last Post: 11-19-2007, 10:46 PM
  2. Switch statement
    By beene in forum C++ Programming
    Replies: 21
    Last Post: 07-01-2007, 08:13 AM
  3. If statement being ignored?
    By FincH in forum C Programming
    Replies: 3
    Last Post: 04-18-2007, 01:51 PM
  4. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM