There are a few problems in your code.
First of all, the do-while loop always executes at least once, which is not what you seem to intend in this case. No matter what the user enters, you always print out the Wrong number message at least once.
Second of all, the condition you use in your loop will always evaluate to true. You compare color, a boolean value to 0 and to 1. You compare a bool to an int value, meaning the bool will be converted to an int. When converting a bool to an int, it will always be converted to either 0 (false) or 1 (true), meaning the expression will always compare to true. You can solve this by checking if cin failed reading a bool.
To solve the first two problems, you could change your loop to the following:
1 2 3 4 5 6 7 8 9
|
bool color;
cin >> color;
while(cin.failed())
{
cout << "Wrong number" << endl;
cin.clear(); //Clear the failed status
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); //Clear the buffer
cin >> color; //Try again
}
|
Third of all, you declare a pointer to Piece:
Piece* pE1
, but never set the pointer. Later on you use the pointer:
pE1->setN(N);
. This causes undefined behavior. You could solve this by declaring your Piece as a normal variable instead of a pointer. Try using
Piece E1
and using it as
E1.setN(N);
.