Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8493267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:57:26+00:00 2026-06-10T22:57:26+00:00

I want a infinit loop to break when I press q key on my

  • 0

I want a infinit loop to break when I press “q” key on my keyboard.
Problems I did’nt realize: standard getchar waits for the user to make an
input and press enter, which halt the execution of the loop there.

I got around the “enter” problem, but the loop still halts and waits for the input.

here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <termios.h>

int getch(void); // Declare of new function

int main (void) { char x;

do 
{ 
   if (x = getch())
      printf ("Got It! \n!); 
   else 
   { 
      delay(2000);
      printf ("Not yet\n!); 
   }

}while x != 'q');

return 0;
}


int getch(void)
{
int ch;
struct termios oldt;
struct termios newt;

tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
return ch;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T22:57:27+00:00Added an answer on June 10, 2026 at 10:57 pm

    I had to do following to make it work correctly, thanks for input!!

    #include <stdio.h> 
    #include <stdlib.h> 
    #include <stdint.h> 
    #include <unistd.h> 
    #include <termios.h> 
    #include <fcntl.h>
    
    int getch(void); // Declare of new function 
    
    int main (void) 
    { 
    
    char x; 
    
    do  
    {  
    x = getch();
            if (x != EOF)
            {
                printf ("\r%s\n", "Got something:");
                printf ("it's %c!",x); //  %c - for character %d - for ascii number   
    }else  
       {  
          delay(2000); 
          printf ("Not yet\n!);  
       } 
    
    }while x != 'q'); 
    
    return 0; 
    } 
    
    
    int getch(void)
    {
        int ch;
        struct termios oldt;
        struct termios newt;
        long oldf;
        long newf;
    
        tcgetattr(STDIN_FILENO, &oldt);             /* Store old settings */
        newt = oldt;
        newt.c_lflag &= ~(ICANON | ECHO);           /* Make one change to old settings in new settings */
        tcsetattr(STDIN_FILENO, TCSANOW, &newt);    /* Apply the changes immediatly */
    
        oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
        newf = oldf | O_NONBLOCK;
        fcntl(STDIN_FILENO, F_SETFL, newf);
    
        ch = getchar();
        fcntl(STDIN_FILENO, F_SETFL, oldf);
        tcsetattr(STDIN_FILENO, TCSANOW, &oldt);    /* Reapply the old settings */
        return ch;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How can I make a while True break if certain key is
want to open pdf file when a user clicks on hyperlink shown in gridview
I want to make sure that my web application is performance wise and will
I want to have a menu display that accepts user input. However, I want
I have an infinite loop that I want to sleep for a second but
In x86, if you want to cause an infinite loop, you can emit an
For example I want press 'v' to get 'asdfv' by autohotkey, but when I
I want the user to enter a number which is scanned by the following
I have to make a java program that when user enters 0 it should
I have one infinite for loop and i want to run this for loop

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.