-2

I am trying to create a minigame to play with my friends, where someone puts a number, and then we have 10 tries to guess it. Unfortunately, we a number is scanned, it stays in the terminal, so everyone can read and cheat. I also tried to do something kinda dumb, which was use printf("\n\n\n\n\n\n\n\n\n\n\n\n\n...") with 29, but that way the code goes down and looks bad. Can anyone help me?

4 Answers4

1

You didn't specify the platform (OS). In General there are several methods:

Maybe you should have searched elsewhere first!

U. Windl
  • 3,480
  • 26
  • 54
0

Probably the simplest thing (but certainly not the best)you can do is shell out to stty to manipulate the terminal database. Eg:

#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
        char b[32];
        printf("prompt: ");
        system("stty -echo");  /* Turn off echo */
        if( scanf("%31s", b) == 1 ){
                printf("\nyou entered %s\n", b);
        }
        system("stty echo");   /* Turn echo back on */
        return 0;
}

Note that this does not handle signals well, and your terminal may end up with a modified state.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
0

If you use windows, you can specifies the maximum number of commands in the history buffer.:

system("doskey /listsize = 0");

In addition, You can simulate the keyboard shortcut ALT+F7 to clear the history.

See more for windows.

If you use Unix, you can clean the history:

system("history -c");
Arcaniaco
  • 400
  • 2
  • 10
0

if you #include <stdlib.h>, system("cls"); should clear the terminal after the number is entered. I tried the game, its fun!