Hiding Input in C

Abaze -  
 ajslenegus -
Hello,
I would like to know if there is a way to hide input using the C language.

Actually, I am developing an application (... rock, paper, scissors... lol it’s for a professional project :( )

And in the Player 1 vs Player 2 part, it would be great if each player could enter their choice without it being visible on the screen... otherwise, hello cheating lol (that's -5 on the BTS, that's why I’m freaking out lol)

That's all, if you have a little tip I’m ready to receive it

Thanks in advance @+++

14 réponses

fafani86
 
Hello,
Please, who can help me? Tomorrow morning I have a graded lab in programming: write a function SAISIR_PASSWD that allows entering a password with **** displayed on the screen. The password will be encrypted before being stored.
2
NoirDelire Posted messages 8 Status Membre 4
 
What tool do you use? Visual C++?

BlackDelight
1
Luffy =)
 
Hi =)

You could perhaps relate the key choice to the press of a button, and detect it without displaying it using the function (well, if you’re using VC++) PreTranslateMessage(). And place the keys side by side to minimize the risk of cheating (ideally, it would be best to make the keys configurable).

++
1
abaze
 
Yes, that's right, I use VC++.....but I'm not very familiar with the configuration...but I will check how to use the PreTranslateMessage() function.
Thank you for your help ;) ++
1
abaze
 
...if it can help to better see where I'm coming from:
You will see below the player's menu...and I'm trying to find a way to hide their input

int submenu()
{
char y;

printf("--Player 1--\n");

printf("___________________________________\n");
printf("--MAKE YOUR CHOICE--\n\n");
printf("[1] Rock\n");
printf("[2] Paper\n");
printf("[3] Scissors\n");
printf("___________________________________\n");
puts("What is your choice:\n");
scanf("%c",&y);

while(y<49 || y>51)
{ printf("Incorrect choice!\n");
printf("Please re-enter your choice: ");
scanf(" %c",&y);
system("cls");
}

return y;
}

I'm looking at the keyboard key detection functions....:O it's not that straightforward (lol I'm a bit lost, I admit)

Well, if you know how to do it, even just a little help would be appreciated lol
thanks in advance
1
SKZ
 
Yes, with getch() or its equivalent, it is definitely possible.
1
kij_82 Posted messages 4102 Registration date   Status Contributeur Last intervention   857
 
Well, here we are! Finally, and to think it was only 5 letters away from us!
So thank you SKZ!
:)

--
~ iclic @ left, iclic, iclic @ right, iclic, iclic
and there's no big mister! ~
0
SKZ > kij_82 Posted messages 4102 Registration date   Status Contributeur Last intervention  
 
No credit, it's gravachol who said it first ;°)
0
abaze
 
Yeah, thanks guys,
but I gave up, I preferred to develop my program under Windows with <conio.h> (which solves all the problems at once BAM!!!! lol) because it goes way too far under Linux, I tried <curses.h> and <ncurses.h> and it’s clear that it’s SUPER ANNOYING!!
Well, thanks again for your answers, I still learned some things anyway lol
Alright, good luck ;) ++</ncurses.h></curses.h></conio.h>
1
Luffy =)
 
I'm sorry, I didn't realize that you were playing your game in the console; I thought you had created a dialog box.

Also, I just checked on MSDN and the PreTranslateMessage() function belongs to the CWnd class, so it's C++. I'll see if there's another way.

++
0
abaze
 
lol Yeah, my game is on the console (I didn't overthink it lol ...:( )

Thanks for your help Luffy ;)

...now I just have the part "hide the player input" left and my project will be almost done! I hope lol

+++
0
Ravachol Posted messages 568 Status Membre 120
 
Hi,
If you don't necessarily need to validate the input with "Enter", you can try using the getch function found in the conio.h header.
Not knowing the Windows environment well, I'm not sure about the header, so check that ;-)

See you soon
--
Freedom is not given; it is taken...
0
kij_82 Posted messages 4102 Registration date   Status Contributeur Last intervention   857
 
I don't think it's possible to hide the writing on the console, as that's not part of your program but rather the console's - if you know what I mean. Anyway, if you find a way, let us know.

thanks

++
--
~ click @ left, click, click @ right, click, click
and there's no bug, sir! ~
0
abaze
 
Yeah, thanks guys, with getch() it works fine!
LOL but I'm so disgusted.....
I made the application at home on Windows XP with Visual C+++.....
But at school I have to work on Linux Debian...
LOL and as if by chance the library <conio.h> (which contains the famous getch() function) doesn't exist under Linux (LOL I'm seriously going crazy)... I searched on the internet, and it said that the equivalent of <conio.h> under Linux is <ncurses.h>... I tried it but well :( the getch() function is still not recognized

LOL it seems like this is never going to end!! I'm continuing my research to see more in detail about <ncurses.h>

Okay, I'm getting back to it, and thanks again for your suggestions seriously, how much that helps speed things up ;)
A++++
0
Ravachol Posted messages 568 Status Membre 120
 
Hi,
You will find a tutorial on ncurses here: http://ariffart.club.fr/sommaire01.html#planpgrc

See you++
--
Freedom is not given, it is taken...
0
tafiscobar Posted messages 1281 Status Contributeur 177
 
Hello, under Linux/Unix it is possible to hide what the user types on the keyboard with the POSIX API. It's not easy though. The principle is that a terminal receives the data from the keyboard, but they are stored in a queue. If the ECHO option is enabled, then everything you type is displayed. Check out termios, you have its manpage here http://www.opengroup.org/onlinepubs/009695399/basedefs/termios.h.html
--
tafiscobar ""lou waye def bopame"
nullity does not exist, ignorance does, oh I suppose!!!
0
SKZ
 
Yep. Or you could use the same code, with :
 #ifdef _WIN #include <conio.h> #endif // some code ... #ifdef WIN a = getch(); #elif a = getchar(); // stdio.h function #endif 


I’m thinking, there's maybe a way to use getchar() in both cases?
0
ajslenegus
 
Merci le gars, ça m'aide beaucoup, j'avais le même problème qu'Abaze.
0