Need to do an infinite loop until I press a key to stop it

This is a discussion on Need to do an infinite loop until I press a key to stop it within the C Programming forums, part of the General Programming Boards category; Need a bit of guidance here. A simple program I wrote before I add in my complicated robot control command. ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    5

    Talking Need to do an infinite loop until I press a key to stop it

    Need a bit of guidance here. A simple program I wrote before I add in my complicated robot control command. I need to make this program keep repeating itself until I press a key to stop it. It's like keep commanding the robot to move until I want it to stop. Please do look at my simple program first. Thanks in advance.

    Code:
    #include <stdio.h>
    #include <termios.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <ctype.h>
    
    int kbhit(void)
    {
    	struct termios oldt, newt;
    	int ch;
    	int oldf;
    	
    	tcgetattr(STDIN_FILENO, &oldt);
    	newt = oldt;
    	newt.c_lflag &= ~(ICANON | ECHO);
    	tcsetattr(STDIN_FILENO, TCSANOW, &newt);
    	oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
    	fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
    
    	ch = getchar();
    
    	tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
    	fcntl(STDIN_FILENO, F_SETFL, oldf);
    
    	if(ch !=EOF)
    	{
    		ungetc(ch, stdin);
    		return 1;
    	}
    	return 0;
    	}
    
    int main()
    {
    	char ch;
    	int asc;	
    	while(!kbhit())
    	scanf("%c",&ch);
    	asc=toascii(ch);
    	if (asc==119)
    	{
    	 printf("\n move front \n");
    	}
    	else if (asc==97)
    	{
    	 printf("\n move left \n");
    	}
    	else if (asc==115)
    	{
    	 printf("\n move back \n");
    	}
    	else if (asc==100)
    	{
    	 printf("\n move right \n");
    	}
    	else if (asc==104)
    	{
    	 printf("\n H \n");
    	}
    	else
    	{
    	printf("\n stop \n");
    	}
    	return 0;
    }

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    As in all the time or wait for a keypress and check if it's correct?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Well, that's not going to do it for you.

    You should build a very small "snake" type of game, so you can see what you're going to get with your current logic. It's not at all what you want.

    You need something like getche() to get the key you press, you can't get an unbuffered keypress with scanf(). And for getche() you need to include conio.h, or use ncurses library, or use Windows API.

    Do you have Turbo C or some C compiler that includes the conio.h header?

    If you're not already set with a conio.h compatible compiler, I'd recommend using the Windows API for this. It's more complicated, but the basic functionality of your robot program should be good for years to come, so why not take the trouble to make it up with an up to date compiler and on the Windows OS, instead of the older DOS type commands in conio.h? I know they work in Windows up to XP, but I don't know if they'll work in Windows 7 or beyond.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The problem with your code, if you actually have kbdhit is that you aren't using it correctly to get a loop going. You don't actually have anything tied together with your loop, except one scanf line. You probably want:
    Code:
    while( !kbdhit() )
    {
        ... do all the stuff you do while you wait for it to be hit...
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Also, look at the last example here. You will see that your kbhit function is not quite right either.

    As an aside, why are you doing the following?

    Code:
    asc=toascii(ch);
    	if (asc==119)
    	{
    	 printf("\n move front \n");
    	}
    	else if (asc==97)
    You could skip using toascii() altogether, and do your tests like this:

    Code:
    if(ch == 'w') {
                printf("\n move front \n");
    }
    And so on. In this way, you are not using magic numbers, and the code should be easier to read.

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    5
    Yes. All the time until a certain key is pressed to stop the program.

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    5
    I was a windows user but my robot needs linux terminal/Mac to communicate with the robot. It would be trouble for me to switch to windows again since I don't have much time left. I tried conio.h but somehow I got an error saying that it couldn't find conio.h. Maybe my gcc compiler is not up for conio.h.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's because it's a DOS file, usualy something like Turbo C from Borland. You may want to look into the curses libraries (ncurses).


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Jul 2010
    Posts
    20
    I think you should use threads. One thread that includes a while loop...

    Code:
    while (flag == 0) {
    
    // do movement stuff...
    
    }

    ...and another thread that waits for user input...

    Code:
    do {
    
         scanf("%c", &ch);
    
         if (ch == 'w') {
    
              flag = 1;
              // do more stuff
    
         }
    
    } while (ch != 27);  // 27 is for ESC

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  2. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  3. Window message loop (Keys)
    By Blackroot in forum Windows Programming
    Replies: 3
    Last Post: 09-12-2006, 05:15 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Fake Key Press
    By (TNT) in forum Windows Programming
    Replies: 9
    Last Post: 04-18-2002, 02:32 AM

Tags for this Thread


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