mardi 4 avril 2017

using getch() to affect conditionals

I'm trying to use getch() as a way to shift between conditionals, such that if the user enters a specific character, the condition that holds true in the if-statement will be invalid and the "else" code will then execute. My code is as follows:

#include <stdlib.h>
#include <stdio.h>
#include <ncurses.h>

int main(int argc,char *argv[]){

    int k = 1;
    char k_char = k + '0';
    int r;

    while(1){

        int n = getch();

        if (n == 'a'){
            k = 0;
        }

        if(k == 1){
            r = rand() % 999;
            printf("%d\n", r);
            napms(300);
        }
        else{
            r = rand() % 4;
            printf("%d\n", r);
            napms(300);
        }
    }

    return 0;
}

entering 'a' doesn't do anything... and I'm having trouble figuring out why, any insight is appreciated.

Aucun commentaire:

Enregistrer un commentaire