Thread: Need help in controlling an infinite loop..!!

  1. #1
    Registered User
    Join Date
    Jan 2012
    Location
    Chennai, Tamil nadu, India
    Posts
    30

    Need help in controlling an infinite loop..!!

    Hello Friends..! Need ur help..!!

    Actually i'm trying to code a game like thing..! In that, there is an aquarium of two fishes..

    Both moving in opposite directions simultaneously and endlessly..

    And two options will be displayed below the bottom of the aquarium..!!

    One is To exit and another one is to feed those fishes...!!

    If i press esc, the program should end and when i press enter, it should shower '*' symbols.

    And i did only the second part.., i mean, an infinite loop to shower the symbol and the movement of fishes..

    And i don't know how to control the infinite loop by those keystrokes...

    Actually is that possible..?? Programmers..!! Help me please...!!!
    Last edited by Rehman khan; 10-13-2012 at 09:24 AM.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    One way (on windows) is to use the functions in conio.h. Try the program below. Press spacebar to switch between printing x's and printing o's. Press any other key to quit.
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(void) {
        int state = 0;
        while (1) {
            if (_kbhit()) {
                if (_getch() == ' ')
                    state ^= 1;
                else
                    break;
            }
                
            switch (state) {
            case 0:
                putchar('x');
                break;
            case 1:
                putchar('o');
                break;
            }
        }
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Jan 2012
    Location
    Chennai, Tamil nadu, India
    Posts
    30

    Thanks a lot..!! I did it..!!

    Well, Thank you oogabooga... you're really great... I finished my work only because of your idea..!

    I'm so happy now.. Really thanks a lot..!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-14-2011, 11:33 PM
  2. Infinite Loop
    By bolivartech in forum C Programming
    Replies: 3
    Last Post: 10-25-2009, 01:31 PM
  3. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM
  4. almost infinite loop
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 02-11-2002, 06:01 PM
  5. Infinite loop?
    By ER in forum C++ Programming
    Replies: 2
    Last Post: 12-02-2001, 05:47 PM