getch() not responding to stop loop

1.7k Views Asked by At

Assume that ncurses header is there and that my random_number_generator() method is working. I am trying to exit the loop by hitting 'a' (eventually I want to exit by hitting CtrlE).

My code continues to print random numbers every second as intended. I want it to exit if either condition is met:

  1. It will exit if the random number is a multiple of the divisor.

  2. It will exit if I use getch(). I think this should work but it is not exiting when I hit a.

    int main(int argc, char *argv[]){
      int lowest = atoi(argv[1]);
      int highest = atoi(argv[2]);
      int divisor = atoi(argv[3]);
    
      int random_number;
      srand((unsigned)time(0));
    
      int c;
      do
      {
        random_number = random_number_generator(lowest, highest);
        cout << random_number << endl;
        sleep(1);
      } while(random_number % divisor != 0 || (c=getch())!=49);
    
      return 0;
    }
    

Edit:

I tried changing the condition to && instead but getch still does not let it exit:

Random number outputs range 1-10:

4
a3    <-hit a
aaa4  <-hit a many times
5     <-exits
8

There are 8 best solutions below

1
On

Because random_number % divisor != 0 is true thats why it does't care whatever you enter

use && instead

11
On

Because of Short-Circuit behavior of logical or operator || if first expression true second doesn't execute.

    random_number % divisor != 0  ||  (c=getch())!=49
 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^      ^
       True                           not get executes       
0
On

Whenever you use a condition, make sure that all parts are behaving as expected.

To be more clear you should better use:

do
    {
        random_number = random_number_generator(lowest, highest);
        cout << random_number << endl;
        sleep(1);
    } while(random_number % divisor != 0 && (c=getch())!='a');

Now if you enter 'a', the second condition is is false, and the loop terminates.

3
On

Your while loop has an OR operator (||) with two conditions, evaluation is performed from left to right in this case. There are two situations where the condition can be true, one is if the random_number % divisor == 0 , the other is c=getch() == 49 , if either of theses conditions is true then loop will terminate, otherwise it will perform its next iteration.

Since the condition evaluation is performed from left to right, so if the first condition inside the while() is true, the next iteration is performed, so whenever the random_number % divisor == 0 then next iteration will be performed.

Solution: put the c=getch() != 49 on the left side, make it the first condition and put the random_number % divisor !=0 on the right side, make it the second condition from left to right.

0
On

because at any given input atleast on condition is satisfied and or(||) continues the loop.USe and(&&) instead.

2
On

Change your operation || to && and also make c=getch()!=49 first control. You can see example below;

int lowest = atoi(argv[1]);
int highest = atoi(argv[2]);
int divisor = atoi(argv[3]);

int random_number;
srand((unsigned)time(0));

int c;
do
{
    random_number = random_number_generator(lowest, highest);
    cout << random_number << endl;
    sleep(1);
} while((c=getch())!=49 && random_number % divisor != 0);

return 0;
2
On
char c;
    do
        {
            random_number = random_number_generator(lowest, highest);
            cout << random_number << endl;

            if(random_number % divisor == 0)
            break;
            else if((c=getchar())=='a')
            break;
            else;

            sleep(1);

        } while(1);
8
On
#include <stdio.h>
#include <conio.h> 

int main(int argc, char *argv[])
{
  int lowest = atoi(argv[1]);
  int highest = atoi(argv[2]);
  int divisor = atoi(argv[3]);

  int random_number;
  srand((unsigned)time(0));

  char c;
  do
  {
    random_number = random_number_generator(lowest, highest);
    printf("%d\n",random_number);
     if(random_number % divisor == 0)
     break;
     else if(kbhit()){
            c = getch();
            if(c=='a')
            break;
     }
     else;
    sleep(1);
  } while(1);

  return 0;
}