#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
int iCurrentTime = 0;
int iElapsedTime = 0;
char cYesNo = '\0';
int i1 = 0;
int i2 = 0;
int i3 = 0;
int iRes1 = 0;
int iRes2 = 0;
int iRes3 = 0;
srand(time(NULL));
printf("Do you want to play a concentration game? (y or n or 1): ");
scanf("%c", &cYesNo);
if(cYesNo == 'y' || cYesNo == 'Y' || cYesNo == '1')
{
i1 = (rand() % 99) + 1;
i2 = (rand() % 99) + 1;
i3 = (rand() % 99) + 1;
printf("Concentrate on these numbers:\n\n%d\t%d\t%d", i1, i2, i3);
iCurrentTime = time(NULL);
do
{
iElapsedTime = time(NULL);
} while( (iElapsedTime - iCurrentTime) < 3 ); //end do while loop
system("clear");
printf("Enter the numbers separated by spaces:\n");
scanf("%d%d%d", &iRes1, &iRes2, &iRes3);
if(i1==iRes1 && i2==iRes2 && i3==iRes3)
{
printf("Congratulations!!");
}//end if
else
{
printf("Better luck next time.");
}
}//end if
return 0;
}//end main
I am a high-school student who is new to programming and I borrowed a book from my local library to learn C: "C Programming for the Absolute Beginner".
I copied this code from the book but it does not work for some reason. When I execute it, it should:
- Ask the user if she/he wants to play a concentration game. Accepts "y" or "n" or "1"
- displays three numbers
- Clears the screen after 3 seconds and prompts us for the three numbers.
The first part works just fine.
The second and third parts don't work the way expected. (See the picture)
The screen doesn't clear before prompting us and some numbers on the left that are probably time.
The description of what the program should do according to the book, word-to-word:
The Concentration game uses many of the techniques you learned about in this chapter. As shown in Figure 4.14, the game generates random numbers and displays them for a short period for the user to memorize. While the random numbers are displayed, the player would try to memorize the numbers and their sequence. After a few seconds have passed, the screen is cleared and the user is asked to enter the memorized numbers in the correct sequence.
(sorry I can't give "Figure 4.14")