1
#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 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")

Shubh Roy
  • 37
  • 6
  • Change printf to `printf("Concentrate on these numbers:\n\n%d\t%d\t%d\t", i1, i2, i3);` – EsmaeelE Jan 18 '20 at 06:28
  • Good game for beginner level. – EsmaeelE Jan 18 '20 at 06:29
  • Instead of the do loop - use sleep – Ed Heal Jan 18 '20 at 06:36
  • You are asking two different things, about the missing clear screen and the strange value. I only realised that after editing and making the alternate text for your picture visible. Please choose one of the two problem and consider removing the other one from your question. Or at least identify one of the two as the main one. – Yunnosch Jan 18 '20 at 07:13
  • As it turns out, it was all because i didn't put a newline character(\n) at the end of my printf statement. – Shubh Roy Jan 20 '20 at 01:59

4 Answers4

4

Here you tell printf() to output four values, but you only provide three.

printf("Concentrate on these numbers:\n\n%d\t%d\t%d\t%d", i1, i2, i3);

You should change that to match the number of given values;

printf("Concentrate on these numbers:\n\n%d\t%d\t%d", i1, i2, i3);
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • 1
    This is the easy part, solving what can be solved without going into environment details. I hope the answer by electromaggot helps you for the other part. – Yunnosch Jan 18 '20 at 07:19
2

I figured out the problem with your program. Turns out it has nothing to do with clear (which you said you tried and is working anyway) or carriage return or any of that.

The key: you absolutely must end your printf strings with newlines (\n characters).

Especially this line:

printf("Concentrate on these numbers:\n\n%d\t%d\t%d\n", i1, i2, i3);
//                                                 ^^ needs this

Otherwise, the %d\t%d\t%d part of your string is not being flushed to the standard output, at least not immediately. So you are not seeing that part display until after your screen-clear, and even then, not until after the next string you print ("Enter the numbers...") finishes the line with its own \n. Only then does that accumulated string flush to your screen -- because the system prefers to flush full lines, not partial ones (unless you force the fflush(stdout)).

You also need to end your "Congratulations" and "Better luck..." strings with newlines too, or those won't look right either when your program ends.

That should do it!

Prior suggestion:

Unfortunately this doesn't seem like an appropriate program for beginners. Yes it's simple, but it contains some system-specific or compiler-specific things that are giving you behavior that you're not expecting.

First of all, like others have mentioned, you have a printf line specifying a format containing four numerical %d values, to which you are only providing three integer parameters. The fourth "tries" to print, which is that -2143927864 value you see.

Next, the system-related functions like system("clear") don't appear from your screenshot to actually be clearing the screen. Also, time(NULL) may not be returning the "realtime" integer value that you are expecting.

Finally, it even seems like your environment is not treating your newline \n characters consistently, or as your C program intends. Printing this character should move your cursor to the beginning of the next line. However, it may behave differently on different terminal programs on different platforms! \n sometimes performs a combination of actions provided by both "linefeed" (0x0A) and "carriage return" (\r or 0x0D) characters. The latter merely moves your cursor to the beginning of the current line, not the next line, then prints more characters over previously-printed ones. This appears to be what you are seeing.

electromaggot
  • 634
  • 6
  • 16
  • Might not be the immediate help OP is hoping for, but definitly a valid angle on helping on a more general level. – Yunnosch Jan 18 '20 at 07:18
  • Thank you, but any ideas on how to make **system("clear")** work the way expected? What is "realtime" integer value and why isn't time(NULL) returning it? – Shubh Roy Jan 19 '20 at 07:22
  • I can't see the name of my PC and $ sign which i usually do so the system("clear") is working but not the way expected because the random numbers should be cleared. – Shubh Roy Jan 19 '20 at 07:55
  • `system(...)`, like its name suggests, runs a command that's dependent on the system or OS you're using -- like if you had typed it in on the command line. From your screenshot, it looks like you're running Cygwin. Searching here on "clear screen" and "cygwin", I see that clearing the screen is [not such a simple matter](https://stackoverflow.com/questions/11249070/how-do-i-get-the-clear-command-in-cygwin). – electromaggot Jan 19 '20 at 15:45
  • Try typing `clear` from your Cygwin command prompt and see if it clears the screen. Different systems have different ways of clearing the screen (e.g. in a Windows command prompt, it is the `cls` command). – electromaggot Jan 19 '20 at 15:45
  • As for `time(NULL)` -- it's probably working. Are you seeing a delay when your program runs, before it then prints "Enter the numbers..."? If so, then `time` is working, so ignore that I brought it up. If it's not working, this could be another issue related to your Cygwin setup, because the realtime clock is something in hardware that `time` pulls from the operating system. – electromaggot Jan 19 '20 at 15:46
  • "clear" is definitely working when i am just typing it in the command prompt but not working in the code for some reason. I mean to display the three numbers for some time and then make them disappear. But instead they are appearing after a delay and then not disappearing. – Shubh Roy Jan 20 '20 at 00:20
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/206247/discussion-between-shubh-roy-and-electromaggot). – Shubh Roy Jan 20 '20 at 00:21
  • How is it that OP environment is treating new-line inconsistently? In C printing a `\n` [always advances to the beginning of the next line](http://port70.net/~nsz/c/c11/n1570.html#5.2.2). External vs. internal representations of a new-line character are not usually an issue for the programmer (sometimes problems can arise with file I/O, e.g. reading text files produced on a different platform). – ad absurdum Jan 20 '20 at 02:07
  • Prior to my realizing that OP was using Cygwin, I had no idea what TTY emulation was in use and why newlines "appeared" to not advance to next line, so I was prognosticating that it was perhaps terminal-setting-related. TTYs from decades ago required: linefeed + carriage return = newline. That's an ancient issue that, while no longer a TTY problem, still plagues us in file formatting. – electromaggot Jan 20 '20 at 02:36
1

WRONG : printf("Concentrate on these numbers:\n\n%d\t%d\t%d\t%d", i1, i2, i3); CORRECT :printf("Concentrate on these numbers:\n\n%d\t%d\t%d", i1, i2, i3);

YOU ENTERED EXTRA %d ..

1

The error is in the below mentioned line.

printf("Concentrate on these numbers:\n\n%d\t%d\t%d\t%d", i1, i2, i3);

You have used four format specifier %d instead of three. The code should be

printf("Concentrate on these numbers:\n\n%d\t%d\t%d", i1, i2, i3);
Kundan
  • 1,754
  • 16
  • 37