I'm working on a simple video game program that will require timed responses. The easiest example I can think of would be something like:
char ans; // answer to question
(can't remember the variable type) starttime; //when clock starts, I'm at work and don't have access to my C++
(same story, when I get home, I'll fix this) timer; // makes sure person does go over time limit
cout<<"Press choose your answer shown on the screen (A/B/C/D)\n";
/*not actually what I'm doing, just an easier version with the same idea, basically just a multiple choice question with limited time to answer. */
char ans; // answer to question
(can't remember the variable type) starttime; //when clock starts, I'm at work and don't have access to my C++
(same story, when I get home, I'll fix this) timer; // makes sure person does go over time limit
cout<<"Press choose your answer shown on the screen (A/B/C/D)\n";
/*not actually what I'm doing, just an easier version with the same idea, basically just a multiple choice question with limited time to answer. */
Code:
starttime=time(NULL); //once again, I believe that's right, but am at work do { cout<<"You're in space, you:\nA) Die.\nB) Vomit.\nC) Play.\nD) Fall anyway.\n"; // I want this to stay, so no "CLS," but I want it to disappear after time is up timer=time(NULL); //makes timer soon after starttime while( (starttime+10>=timer)&&( (ans!='A')||(ans!='a')||(ans!='B')||(ans!='b')||(ans!='C')||(ans!='c')||(ans!='D')||(ans!='d') ) { /*This upcoming part is what is actually confusing me, because I'm not sure how to stop letting the person give input, the other option would be to make sure the value of 'timer' isn't over 10 more than 'starttime', or it switches to 'L', but I'd rather it just stop the input. Any input?*/ cin>>ans; // I want them to be able to enter for limited time, but a while loop doesn't stop until the cin is entered, will it? timer=time(NULL); // resets timer in case while loop has more time and person didn't enter a valid answer } if( (ans!='A')&&(ans!='a')&&(ans!='B')&&(ans!='b')&&(ans!='C')&&(ans!='c')&&(ans!='D')&&(ans!='d') ) // if person doesn't answer in time, they are assigned an answer { ans='L'; } } while( (ans!='A')&&(ans!='a')&&(ans!='B')&&(ans!='b')&&(ans!='C')&&(ans!='c')&&(ans!='D')&&(ans!='d')&&(ans!='L' ); // person really REALLY has an answer if( (ans=='a')||(ans=='A') ) { cout<<"Your head a splode."; // gotta have humor } if( (ans=='b')||(ans=='B') ) { cout<<"\nYou technicolor yawn."; // I'm bored at work, what can I say? } if( (ans=='c')||(ans=='C') ) { cout<<"You twiddle your thumbs."; // Some jokes aren't meant to be funny. } if( (ans=='d')||(ans=='D') ) { cout<<"You fall to your doom.\n\n...That's kinda weird, what with the lack of gravity. You're my hero!"; // Gotta admit, REALLY bored at work } if(ans=='L') { cout<<"\nBefore you can react, you get hit by a bus." } /*That's all that I think really relates, but I'll write more when I get home.*/
Comment