Hi All,
I am new member of this forum. I am trying to implement the simple c logic for the following problem :
I want to come out of a while loop only when I press the key 'e' But condition is that, it should not wait for me to enter a key during each iteration as in the following code,

int i = 0;
char c = 'a';
while(1)
{
printf("%d",i++);
if(getch() == 'e')
break;
}

here each time I need to enter a key then it checks if it is 'e' and then it continues, but I dont want it to wait for me.It should ignore all the keys except 'e' without waiting.

If anybody can provide a logic, please reply

Sulley's Boo commented: ^,^ +2

Unfortunately there is no standard c or c++ way to accomplish that. So you have to resort to some non-standard functions that you compiler may support, such as those in conio.h. I believe the functions in that header file were originally developed by Borland for their Turbo C compiler, but other compilers have picked up many of those functions. Check to see if you compiler has conio.h and if it does then look in that file for kbhit() (which checks to see of a key has been pressed) and getche().

This is just an idea how can solve your problem

#include "stdio.h"
#include "pthread.h"

pthread_t threadEvent;
unsigned char breakLoop = 0;

void * Exit(void * ptr)
{
   char c = 'a';
   scanf("%c", &c);
   if ( c == 'e')
      breakLoop = 1;
}

main()
{
   int i = 0;
   pthread_attr_t attr;
   char * arg = NULL;
   pthread_attr_init(&attr);
   int threadID;
   
   threadID = pthread_create(&threadEvent, &attr, Exit, arg); // create thread wich recives user input	
   while(1)
   {
      printf("%d\n",i++);
      
      if (breakLoop)
         break;
      sleep(1);
      
   }

   return 0;
}

So the logic is that you create a thread wich will take input from user :).
Adopt this code for your compiler. I dont have conio.h so I used scanf instead of getch. But avoid scanf. Modify sleep with usleep. Compiled with gcc.

commented: =') +2

A little bug just change the Exit func to

void * Exit(void * ptr)
{
   char c;
   while (1)
   {
      scanf("%c", &c);
  
      if ( c == 'e')
      breakLoop = 1;
   }
}

hi ancient dragon,
thanks for the suggestion I am trying to impliment it

hi andor,
thanks for the suggestion I am trying to execute your code on my compier

Hi Ancient Dragon,
I am very thankful about your suggestion. Your idea of using kbhit() function has solved the problem. Fortunately I have Borland's compiler installed on my machine.
Here is the solution code :

#include <conio.h>
void main()
{
   int i=0;
   while(1)
            {
                if(kbhit())
                     {
                         if(getch() == 'e')
                             break;
                     }
               else
                     printf("%d",i++);
              delay(5000);        /*just to slow down the printing of nos */
           }
}

Unfortunately there is no standard c or c++ way to accomplish that. So you have to resort to some non-standard functions that you compiler may support, such as those in conio.h. I believe the functions in that header file were originally developed by Borland for their Turbo C compiler, but other compilers have picked up many of those functions. Check to see if you compiler has conio.h and if it does then look in that file for kbhit() (which checks to see of a key has been pressed) and getche().

just be aware that your program may or may not compile with your instructor's compiler (assuming your instructor compiles student's work). Find out what compiler your instructor uses.

ok, that's not the problem

just be aware that your program may or may not compile with your instructor's compiler (assuming your instructor compiles student's work). Find out what compiler your instructor uses.

using ANSI i don't see why you should have any issues also you can do it using break and continue!

Hi,
can you please give me more detail idea about it

using ANSI i don't see why you should have any issues also you can do it using break and continue!

thsi code is rite and works absolutely fine for me i need not press enter..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.