简体   繁体   中英

Pause and resume loop in C

I am writing a game code in C which involves display of numbers with a time gap entered by the user.

srand ( time(NULL) ); //this avoids generating the same random number everytime
start:
    mySleep(userdelay);
    printf("\n\n\a\t%d",rand()%90); //prints random numbers with the time delay entered by the user.
goto start;

So I just wanted to know how to pause this loop with user input like "Press 1 to pause" and resume the same loop with user input like "Press 2 to resume".

Thanx in advance!

I'm not sure this may be the best way, but it could be one working way. Try using a while loop in your command console:

int main(){
    int i;
    scanf("%d",&i)
    switch(i){
        case 1: while(i != 2){
                    sleep(1); //or define duration by a variable if time should be "dynamic"
                    scanf("%d",&i);
                    break;
                }
    }
}

I hope you get an idea of the idea.

EDIT:

If you want the user to input data until "exit key" a while loop is much bether than goto. If you are not familiar with switch(){} i advice you to take a look here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM