I have wrote a function that ask the user for two numbers. If the user got the right two numbers the function the function repeats itself untill the user guessed right four times.
The problem is however that at some point the input isn't stored correctly or something.
For example, If 4 and 3 are the correct numbers and I enter them the programm will say that I got them right and asks again for two numbers, but when I enter another pair of numbers (different numbers than 4 & 3), for some reason the programm will store 4 and 3 to the variable despite whatever number I enter.
Does this has something to do with cin.ignore() and cin.clear()????? because I am not using those functions.
So I am making a battleship game. And this is the code for the player shoting at the ships of the computer. The problem is, is that sometimes the programm uses a previous input while I already entered new coordinates.
a created a 2d-array as board so the player can see where he fired already.
void Player_FireAtComputer(){
for (char count5 = 0; count5 < 1; count5++){
if (Speler_Hit == 4){ // The player can hit 4 possible targets so if he hits times 4 he has won
for (char count6 = 0; count6 < 5; count6++){
cout << "\Player has won!!!";
}
break;
}
cin >> GokLocatie1Rij_C; // GokLocatie1Rij_C is the row number the player is shoting at
GokLocatie1Rij_C--;
cin >> GokLocatie1Kolom_C; // GokLocatie1Kolom_C is the colom number the player is shoting at
GokLocatie1Kolom_C--;
cout << "\n";
if ((GokLocatie1Rij_C == LocatieBoot1_Rij_C) && (GokLocatie1Kolom_C = LocatieBoot1_Kolom_C)){ //if the coordinate entered is equal to one of the ships of the computer
cout << "HIT 1" << endl;
SpelBordTegenstander [GokLocatie1Rij_C][GokLocatie1Kolom_C] = "R"; //if the coordinate is right.the letter R is placed in the 2d array
Speler_Hit++; //adds one to Player_Hit
SpeelVeldComputer(); //function to print the 2d array
Player_FireAtComputer(); //the function is called again because if the player hits he can shoot again
}
elseif ((GokLocatie1Rij_C == ExtraBoot1_Rij_C) && (GokLocatie1Kolom_C == ExtraBoot1_Kolom_C)){ //this statement works the same as the previous one
cout << "HIT 2" << endl;
SpelBordTegenstander [GokLocatie1Rij_C][GokLocatie1Kolom_C] = "R";
Speler_Hit++;
SpeelVeldComputer();
Player_FireAtComputer();
}
elseif ((GokLocatie1Rij_C == LocatieBoot2_Rij_C) && (GokLocatie1Kolom_C = LocatieBoot2_Kolom_C)) { //this statement works the same as the previous one
cout << "HIT 3" << endl;
SpelBordTegenstander [GokLocatie1Rij_C][GokLocatie1Kolom_C] = "R";
Speler_Hit++;
SpeelVeldComputer();
Player_FireAtComputer();
}
elseif ((GokLocatie1Rij_C == ExtraBoot2_Rij_C) && (GokLocatie1Kolom_C == ExtraBoot2_Kolom_C)){ //this statement works the same as the previous one
cout << "HIT 4" << endl;
SpelBordTegenstander [GokLocatie1Rij_C][GokLocatie1Kolom_C] = "R";
Speler_Hit++;
SpeelVeldComputer();
Player_FireAtComputer();
}
else{ // if the player doesn't hit anything, a X is marked on the board and the board is printed and the Computer can fire at the player
cout << "You missed." << endl;
SpelBordTegenstander [GokLocatie1Rij_C][GokLocatie1Kolom_C] = "X";
cout << endl;
SpeelVeldComputer();
Computer_FireAtPlayer();
}
}
}