Exits the loop after a certain amount of time if there's no input

后端 未结 3 1375
南方客
南方客 2021-01-19 03:22

I\'m just wondering whether is it possible and how do I implement this feature, where we exit from the loop if there\'s no input from the user. For example, I want to exit t

3条回答
  •  攒了一身酷
    2021-01-19 04:08

    Although the time_t structure returned by time() is most likely a number of seconds, you should not be performing maths on it. Instead use difftime()

    double difftime ( time_t time2, time_t time1 );
    

    Calculates the difference in seconds between time1 and time2.

    You do not need to call main() from within main() and I'm not sure why you would think that is a good idea.

    Also, getchar() will wait for a key to be pressed, so it won't be counting time in the background.

提交回复
热议问题