Home > Enterprise >  Any way to prevent/limit user input
Any way to prevent/limit user input

Time:09-18

I am creating a simple number memorising game which can be played using command lines/console. Each round there will be one more digit of number you need to memorise. Then you have to enter it, if it's correct you get some points and the game continues.

There is a problem where the use can input/type when the number you have to memorise is displaying.

Is there anyway to stop or clear user input?

Here is my game code:

#include<bits/stdc  .h>
using namespace std;
bool correct=true;
long long score,r=1,num,ans;
int rng(){
    srand(time(NULL));
    int rn=rand();
    return rn;
}
void checkans(int ans){
    if(ans==num)score =r*100;
    else correct=false;
}
void displaytext(string t,double delay,bool end){
    int i=0;
    while (t[i]!='\0'){
        cout<<t[i];
        usleep(1000000*delay);
        i  ;
    }
    if(end)cout<<endl;
}    
int main(){
    displaytext("Hello!",0.1,false);
    usleep(1000000);
    system("CLS");
    displaytext("Welcome to Just a Number Memorising Game!",0.1,false);
    usleep(1000000);
    system("CLS");
    usleep(1000000);
    displaytext("Let's begin!",0.1,false);
    usleep(1000000);
    system("CLS");
    displaytext("[SCORE]: ",0.1,false);
    displaytext(to_string(score),0.05,true);
    for(int i=1;i<=to_string(score).size() 9;i  )displaytext("-",0.05,false);
    while(correct){
        system("CLS");
        num=num*10 rng();
        cout<<"[SCORE]: ";
        displaytext(to_string(score),0.05,true);
        for(int i=1;i<=to_string(score).size() 9;i  )cout<<"-";
        displaytext("-",0.05,true);
        displaytext("MEMORISE THIS --> ",0.05,false);
        displaytext(to_string(num),0.2,false);
        usleep(2000000);
        system("CLS");
        cout<<"[SCORE]: "<<score<<endl;
        for(int i=1;i<=to_string(score).size() 9;i  )cout<<"-";
        cout<<"-"<<endl;
        displaytext("Enter what you memorised: ",0.05,false);
        cin>>ans;
        checkans(ans);
        r  ;
        usleep(500000);
        system("CLS");
    }
    displaytext("[GAME OVER]",0.2,true);
    usleep(500000);
    displaytext("[SCORE]: ",0.1,false);
    displaytext(to_string(score),0.05,true);
    usleep(10000000);
    system("CLS");
    return 0;
}

CodePudding user response:

Since C cannot know, on which platform or terminal it is running, it cannot have native support for any potential terminal. Hence, you will not find such a functionality in pure C .

Your usage of function usleep suggests that you are maybe working on a Linux compatible platform.

Here, a very often used solution is the ncurses library. This will give you everything that you need.

And it maybe already installed on your machine. Please check . . .

  •  Tags:  
  • c
  • Related