Hiding user input

I recently created a very simple program. It is just my first, but I have been trying to perfect it.
#include <iostream>
using namespace std;
int main ()
{
unsigned long y;
unsigned long z;
cout << "This is a game for two players. It is very simple. One player will type in a number between one and a hundred, and their opponent will try to guess what it is. If the number the second player guesses is too high or too low, they will be notifyed. the goal is to guess your opponents number in the least amount of guesses. Let's begin by having the first player type in a number: ";
cin >> z;
do {cout << " It's the second player's turn to guess the number. Type in your guess: ";
cin >> y;
if (y < z)
cout << "The number you guessed is to low.";
if (y > z)
cout << "The number you guessed is to high.";
if (y == z)
cout << "Awesome! You guessed the number!";
} while (y != z);
int x;
std::cin >> x;
return 0;
}
The problem I have encountered is when the first player types in their number, the second player can easily see it when they're guessing. I've tried using getch, but that hasn't worked. Do you have a solution?
getch() does not echo to the screen but you probably need header conio.h or stdio.h (can't remember)
You could also consider generating a random number to compare the user guess with and printing it out at program end to prove the result.
Yeah, I was using conio.h when I was trying getch(). I suppose I should try a random number generator.
You could just clear the screen after getting the number.

Check this out -> http://www.cplusplus.com/forum/articles/10515/
Last edited on
closed account (zb0S216C)
You could use system( "CLS" ) but that would make your program OS specific, which you don't wan't. However, if you're running Windows and this is not a cross-platform program, I don't see any harm in using system( "CLS" ). If this is a cross-platform program, don't use it.

You can use any function you wan't. There's no law that says you cannot use the system( ) function. It is best to avoid it when writing more serious projects though.
Topic archived. No new replies allowed.