Help with, for(;;)

This is a discussion on Help with, for(;;) within the C Programming forums, part of the General Programming Boards category; I started to make this program but when I run it, it acts funny with the way it sets itself ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    43

    Question Help with, for(;;)

    I started to make this program but when I run it, it acts funny with the way it sets itself up.

    /***************SOURCE***********CODE**************/
    void main()
    {

    int x;

    printf("\n\nMicrosoft(R) Windows 98\n\
    (C)Copyright Microsoft Corp 1981-1998.\n\n");


    for(x=0;x<6;x+=1)
    {
    printf("C:\\WINDOWS\\>");
    getchar();
    printf("\nBad command or file name\n\n");
    }
    }
    /************************************************** */

    Thanks to the person that can help me out.
    ()()() MonKey ware
    Kyle J. R.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Need a bit more elaboration on what your problemis, although it looks like using gets() instead of getch() might be your answer.

    Also, I suggest avoiding using
    void main ()
    main returns an int, it's part of the standard, and even if you say that it doesn't return anything, it's always going to return an int to the operating system.
    int main ()
    {
    ...
    return 0; // Last line
    }
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Unregistered
    Guest
    The reson for the getchar() is so you can type anything in it just acts as a pause. When you just hit enter it works fine, but if you put one+ char then hit enter it messes up. I am trying to make it so you type all 6 times, and can type as much as you want with out it completing the "for" untill the 6th time.


    main()
    {

    int x;

    printf("\n\nMicrosoft(R) Windows 98\n\
    (C)Copyright Microsoft Corp 1981-1998.\n\n");

    for(x=0;x<6;x+=1)
    {
    printf("C:\\WINDOWS\\>");
    getchar();
    printf("\nBad command or file name\n\n");
    }
    }

  4. #4
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    lol... I really po'd my prof with a program like this. Only difference was I added a runtime error to boot. :P

    try

    #include <stdio.h>

    int main()
    {

    char input[80];

    printf(windows banner);

    for(; - // GRRR stupid smily face anyway. Double semi colons
    {

    printf("C:\\WINDOWS\\>_");

    gets(input);

    printf("\nbad command or filename.");

    }

    return 0; // program will never make it here. stuck in a forever loop.

    }

    Or something like that. C++ is easier to remember.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Here's basically what happens when you get prompted for input...

    first getch() call...
    The input buffer is empty...
    You type in a couple letters, and press enter.
    Upon pressing Enter, the letters you typed in (and the \n) get put into the buffer.
    getch() takes one character off the buffer, and your program continues.
    getch() is called again
    The input buffer still has stuff in it (unless you only pressed ENTER, in which case one \n was put into the buffer)
    getch() takes on character off the buffer, and your program continues.


    Here's how it'd work with gets()....
    first gets() call...
    The input buffer is empty...
    You type in a couple letters and press enter.
    Upon pressing Enter, the letters you typed in (and the \n) get put into the buffer.
    gets() takes all the characters off the buffer, up to the \n, and your program continues.
    gets() is called again
    The input buffer is empty, since gets took all the characters up to the \n, and \n is obviously the last character in the buffer since Enter is the last key you pressed, so it took all the characters.
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

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