Thread: Loop isn't printing

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    17

    Loop isn't printing

    I'm new to C and was trying to teach myself some simple things first. I don't particularly know why this code is not performing like I would think it would:

    Code:
    int main() {
        int ch;
        int i4 = 0;
        while ((ch = getchar()) != '\n' && ch != EOF) {
            printf("%i", i4);
            i4++;
        }
        return(0);
    }
    I would think that this would be an infinite loop that would print integers increasing by 1 and the loop wouldn't stop until I pressed enter.

    The loop just exits when I hit enter with no printing at all to the screen. What's wrong here?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The input is line-buffered, the the first getchar doesn't occur until you've pressed enter for a whole line of text. Try entering a few characters and then press enter to see what I mean. So if you only press enter by itself, the first getchar kills the loop before it can execute once.
    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.*

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    17
    I see what you're saying. What would be a way to check for input from the user while still looping and printing those numbers?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Something nonstandard like kbhit and getch (hey, those would make great search terms). But you may want to mention your compiler and OS.
    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
    Registered User
    Join Date
    Jun 2006
    Posts
    17
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int infiniteloop(int index);
    
    int main() {
        infiniteloop(0);
    }
    
    int infiniteloop(int index) {
        int ch;
        int i4 = index;
        while (!_kbhit()) {
            i4++;
            printf("%d\n", i4);
        }
        
        if((ch = _getch()) != 13) {
            infiniteloop(i4);
        }
        
        return(0);
    }
    This works now, but I have a dependency on Windows. I'm using Visual Studio 2005 on XP Pro and want to write code that's not OS dependent.

    I want to be able to loop numbers, print them to the screen, and stop the loop when I press enter. Is there a way to do it without using _kbhit?

    This just seems like something that somebody must have done before but I'm not coming up with anything specifically like this.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I'm using Visual Studio 2005 on XP Pro and want to write code that's not OS dependent.
    You could try setvbuf before calling anything else
    http://man.he.net/man3/setvbuf

    That is, set the input to be unbuffered.

    However, this only sets the standard library to be unbuffered, the OS may still have other ideas, so I don't think this isn't a "given" either.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutiliplcation Program that uses a (do, while, and for loop)?
    By Debbie Bremer in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2008, 06:04 PM
  2. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 05:47 PM
  3. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-31-2006, 12:59 AM
  4. For loop ; printing records
    By Prezo in forum C Programming
    Replies: 3
    Last Post: 09-15-2002, 06:38 AM
  5. printing values loop.
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 07-02-2002, 02:56 PM