Non Console Specific Hiding Input

So, this is my first attempt at writing any sort of code besides "Hello World."
It's a Rock, Paper, Scissors game. The problem is that player one's input just sits there when it's player two's turn. I've tried to look up solutions, but all I could find were OS specific, which I've heard is generally something that should be avoided. I most likely won't be using this specific program outside of right here on my Windows computer. However, I would like to become equipped with the skills to write multi-platform code/programs. Any additional comments/help related to this specific bit of code or coding/programming in general would be greatly appreciated. Thank You!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 #include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
	string scp = "Scissors Cut Paper! ";
	string rcs = "Rock Crushes Scissors! ";
	string pcr = "Paper Covers Rock! ";
	string w = " Wins! ";
	string name1, name2, end;
	string p1choice, p2choice;
	int p1, p2, result;

	
	cout << "Player 1, What is your name? " << endl;
	getline(cin, name1);
	cout << "Thank you, " << name1 << "! " << endl;
	cout << "Player 2, What is your name? " << endl;
	getline(cin, name2);
	cout << "Thank you, " << name2 << "! " << endl;
do {
	cout << "P1 Rock=1, Paper=2, Scissors=3 " << endl;
	getline(cin, p1choice);
	stringstream(p1choice) >> p1;
	cout << "P2 Rock=1, Paper=2, Scissors=3 " << endl;
	getline(cin, p2choice);
	stringstream(p2choice) >> p2;

	if ((p1 == 1) && (p2 == 1))
		cout << "Rock vs Rock! Tie! " << endl;
	else if ((p1 == 1) && (p2 == 2))
		cout << pcr << name2 << w << endl;
	else if ((p1 == 1) && (p2 == 3))
		cout << rcs << name1 << w << endl;
	else if ((p1 == 2) && (p2 == 1))
		cout << pcr << name1 << w << endl;
	else if ((p1 == 2) && (p2 == 2))
		cout << "Paper vs Paper! Tie " << endl;
	else if ((p1 == 2) && (p2 == 3))
		cout << scp << name2 << w << endl;
	else if ((p1 == 3) && (p2 == 1))
		cout << rcs << name2 << w << endl;
	else if ((p1 == 3) && (p2 == 2))
		cout << scp << name1 << w << endl;
	else if ((p1 == 3) && (p2 == 3))
		cout << "Scissors vs Scissors! Tie! ";
	
	else cout << "Oops! ";
	cout << "Start Over? " << endl << "Q to quit, any other key to restart... "<< endl;
    getline(cin, end);
	} while (end != "Q");

	return 0;
}
As all consoles are different and C++ standard library does not have stuff to manipulate console, there is no true crossplatform way to do this. Your best be is to make a wrapper and then use conditional compilation to select implementation depending on target OS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* Your code */
#include "console.h"
//...
clearScreen();
//...

/* console.h */
//...
void clearScreen();
//...

/* console.cpp */
#if defined(_WIN32) || defined(_WIN16)
void clearScreen()
{
  //Do stuff for all Windows versions
}
#elif defined(__linux__) && !defined(__ANDROID__)
void clearScreen()
{
  //Do stuff for Linux versions
}
#elif defined(__ANDROID__)
void clearScreen()
{
  //Android stuff
}
//etc
#endif 
http://stackoverflow.com/questions/142508/how-do-i-check-os-with-a-preprocessor-directive
So if i add ALL of that code, it would determine which to use on its own? Or are you saying to replace "console.h" with whichever OS header is needed for the one i happen to be porting this to? Sorry if it sounds like i REALLY don't know what i'm talking about, because i don't.
You need to put code for each supported platform between preprocessor directives, and then when compiledon specific platform correct vesion will be chosen.
Or you can select a crossplatform library which wil handle tsis. For windows here is PDcurses
Yeah, C++ doesn't have a way to manipulate console, so using cross-platform library would be your best shot. And as mentioned, curses is the library that allows just that.

Cheers!
Thanks for the info!
Topic archived. No new replies allowed.