Program prints a message telling the user to push a key to start the game.
Once in game, output tells the player which key to push. The key to press should be determined randomly. Game runs until wrong key is pressed.
Include timer function with each round decreasing the time (2.5 seconds-2seconds-1.5seconds-etc until 0.
Once hit 0 you win.
Please find the requested program below. Also including the screenshot of sample output and code to understand the indentation
Please provide your feedback
Thanks and Happy learning!
#include <stdio.h>
#include<stdlib.h>
#include <time.h>
#include <ctype.h>
int main()
{
srand(time(NULL));
char cUserInput;
char randLetter = (rand() % 26) + 'A';
//Program prints a message telling the user to push a key to start the game.
printf("Press %c to start the game\n", randLetter);
scanf_s(" %c", &cUserInput, sizeof(cUserInput));
double duration = 2.5; //seconds
time_t startTime;
time_t currentTime;
char cUserInputUpper = toupper(cUserInput);
while ((cUserInputUpper == randLetter))
{
//Once in game, output tells the player which key to push.
//The key to press should be determined randomly.
//Game runs until wrong key is pressed.
if (duration == 0)
{
printf("You WON!!\n");
break;
}
randLetter = (rand() % 26) + 'A';
printf("Press %c \n", randLetter);
startTime = time(NULL);
scanf_s(" %c", &cUserInput, sizeof(cUserInput));
cUserInputUpper = toupper(cUserInput);
currentTime = time(NULL);
if (currentTime - startTime > duration)
{
printf("You LOOSE!. Timed out.\n");
break;
}
duration = duration - 0.5;
}
getchar();
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.