Within my int main () has an if statement that suppose to add 1, 2 or 3 to a variable named anicrds and then add that to a either player one or player two total animal cards depending on whether it player one or two turn but when ever the conditions are true (player one or player two are on the spot 7, 14, etc) The program doesn't recognise the if statement. It's getting very frustrating please help.
N.B. This program I had to do in 3 steps. This is the 3rd one. Everything isn't fine tuned so bare with it. I tested alot with it and something might not be right.
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
usingnamespace std;
//This function will tell the user to roll the dice.
void rollPrompt(int press) {
if (press == 1) {
cout << "Please press enter to roll the dice." << endl;
cin.ignore();
} else {
cout << "Please press enter to roll the dice again." << endl;
}
cin.ignore();
}
//This Function is the Random number genrate and declares whether the player has landed on binocuars or not.
int diceRoll(){
int ran;
cout << "" <<endl;
for (int x = 0; x < 2; x++) {
srand(time(0));
ran = rand()%6+1;
if (ran == 1) {
cout << "You rolled the binoculars." << endl;
} else {
cout << ran;
}
return ran;
}
}
int main() {
//Declaring all variables used in this scoop.
string pOne, pTwo, startFirst, startSecond;
int pOneAge, pTwoAge, fDie, sDie, total;
//Setting player start point to 1.
int playerOne = 1;
int playerTwo = 1;
int playerTurn = 0;
int anicrds = 0;
int POanicrds = 0;
int PTanicrds = 0;
int anicrdsT = 0;
cout << "Please enter your name." <<endl;
cin >> pOne;
cout << "Please enter your age " << pOne << endl;
cin >> pOneAge;
cout << "" <<endl;
cout << "" <<endl;
cout << "Please enter your name." << endl;
cin >> pTwo;
cout << "Please enter your age " << pTwo << endl;
cin >> pTwoAge;
system("CLS");
cout << "" <<endl;
cout << "" <<endl;
//Determining who is player one and by default player 2.
if (pOneAge <= pTwoAge){
startFirst = pOne;
startSecond = pTwo;
cout << pOne << " is to play first!" << endl;
}else{
startFirst = pTwo;
startSecond = pOne;
cout << pTwo << " is to play first!" << endl;
}
cout << "" <<endl;
cout << "" <<endl;
//Main loop function.
do {
//Used to loop between players.
if (playerTurn == 0) {
cout << startFirst << endl;
cout << "" <<endl;
//Prompting user to roll dice.
rollPrompt(1);
//Generating first dice roll.
fDie = diceRoll();
cout << "" <<endl;
//Prompting user to roll dice again.
rollPrompt(2);
//Generating second dice roll.
sDie = diceRoll();
cout << "" <<endl;
cout << "" <<endl;
//Calculating total of the 2 dice rolls.
total = fDie + sDie;
cout << "" <<endl;
system("pause");
system("CLS");
//Displaying total of the 2 dice rolls.
cout << startFirst << endl;
cout << "Your total dice roll is " << total << endl;
//Adding dice rolls to player one score to keep track of his/her position on the board.
playerOne+=total;
cout << "" <<endl;
if (playerOne == 7 || playerOne == 14 || playerOne == 21 || playerOne == 28 || playerOne == 35 || playerOne == 42 || playerOne == 49 || playerOne == 56) {
srand(time(0));
anicrdsT = rand()%3+1;
POanicrds += anicrdsT;
cout << "You landed on a Binoculars." << POanicrds << endl;
}
//Displayer total position of player on the board.
cout << startFirst << " is currently on " << playerOne << endl;
cout << "" <<endl;
//Displaying how far they are from the base camp or the end of the game.
cout << "You are " << 65 - playerOne << " away from the base camp!" <<endl;
cout << "" <<endl;
system("pause");
system("CLS");
}
//Starting player 2 turn.
cout << startSecond << endl;
cout << "" <<endl;
//Prompting user to roll dice.
rollPrompt(1);
//Generating first dice roll.
fDie = diceRoll();
cout << "" <<endl;
cout << "" <<endl;
//Prompting user to roll dice again.
rollPrompt(2);
//Generating second dice roll.
sDie = diceRoll();
cout << "" <<endl;
cout << "" <<endl;
//Calculating total of the 2 dice rolls.
total = fDie + sDie;
cout << "" <<endl;
system("pause");
system("CLS");
//Displaying total of the 2 dice rolls.
cout << startSecond << endl;
cout << "Your total dice roll is " << total << endl;
//Adding dice rolls to player two score to keep track of his/her position on the board.
playerTwo+=total;
cout << "" <<endl;
if (playerTwo == 7 || playerTwo == 14 || playerTwo == 21 || playerTwo == 28 || playerTwo == 35 || playerTwo == 42 || playerTwo == 49 || playerTwo == 56) {
srand(time(0));
anicrdsT = rand()%3+1;
PTanicrds += anicrdsT;
cout << "You landed on a Binoculars." << PTanicrds << endl;
}
//Displayer total position of player on the board.
cout << startSecond << " is currently on " << playerTwo << endl;
cout << "" <<endl;
//Displaying how far the player is from the base camp or the end of the game.
cout << "You are " << 65 - playerTwo << " away from the base camp!" <<endl;
cout << "" <<endl;
cout << "" <<endl;
system("pause");
system("CLS");
//Conditions for the loop.
} while (playerOne <= 65 || playerTwo <= 65);
//Condition to determine the winner of the game.
if (playerOne >= 65) {
cout << "The winner is " << startFirst << endl;
} elseif (playerTwo >= 65) {
cout << "The winner is " << startSecond << endl;
};
return 0;
}
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
usingnamespace std;
//This function will tell the user to roll the dice.
void rollPrompt(int press) {
if (press == 1) {
cout << "Please press enter to roll the dice." << endl;
//cin.ignore(); Don't need this
} else {
cout << "Please press enter to roll the dice again." << endl;
}
cin.get(); // Added this
}
//This Function is the Random number genrate and declares whether the player has landed on binocuars or not.
int diceRoll(){
int ran;
//cout << "" <<endl;
ran = rand()%6+1;
// if (ran == 1) {
// cout << "You rolled the binoculars." << endl;
// } else {
cout << ran << endl; // Output the number and then return it
// }
return ran;
}
int main() {
srand(time(0)); // Do this only once here
//Declaring all variables used in this scoop.
string pOne, pTwo, startFirst, startSecond;
int pOneAge, pTwoAge, fDie, sDie, total;
//Setting player start point to 1.
int playerOne = 1;
int playerTwo = 1;
int playerTurn = 0;
// int anicrds = 0; This is unused
int POanicrds = 0;
int PTanicrds = 0;
int anicrdsT = 0;
cout << "Please enter your name." <<endl;
cin >> pOne;
cout << "Please enter your age " << pOne << endl;
cin >> pOneAge;
// cout << "" <<endl;
// cout << "" <<endl;
cout << "Please enter your name." << endl;
cin >> pTwo;
cout << "Please enter your age " << pTwo << endl;
cin >> pTwoAge;
// system("CLS");
// cout << "" <<endl;
// cout << "" <<endl;
//Determining who is player one and by default player 2.
if (pOneAge <= pTwoAge){
startFirst = pOne;
startSecond = pTwo;
cout << pOne << " is to play first!" << endl;
}else{
startFirst = pTwo;
startSecond = pOne;
cout << pTwo << " is to play first!" << endl;
}
// cout << "" <<endl;
// cout << "" <<endl;
//Main loop function.
do {
//Used to loop between players.
if (playerTurn == 0) {
cout << startFirst << endl;
// cout << "" <<endl;
//Prompting user to roll dice.
rollPrompt(1);
//Generating first dice roll.
fDie = diceRoll();
// cout << "" <<endl;
//Prompting user to roll dice again.
rollPrompt(2);
//Generating second dice roll.
sDie = diceRoll();
// cout << "" <<endl;
// cout << "" <<endl;
//Calculating total of the 2 dice rolls.
total = fDie + sDie;
// cout << "" <<endl;
// system("pause");
// system("CLS");
//Displaying total of the 2 dice rolls.
cout << startFirst << endl;
cout << "Your total dice roll is " << total << endl;
//Adding dice rolls to player one score to keep track of his/her position on the board.
playerOne+=total;
// cout << "" <<endl;
if (playerOne == 7 || playerOne == 14 || playerOne == 21 || playerOne == 28 || playerOne == 35 || playerOne == 42 || playerOne == 49 || playerOne == 56) {
cout << "You got in if" << endl; // Added this, so you can notice when you get in if, remove it later
anicrdsT = rand()%3+1;
POanicrds += anicrdsT;
cout << "You landed on a Binoculars." << POanicrds << endl;
}
//Displayer total position of player on the board.
cout << startFirst << " is currently on " << playerOne << endl;
// cout << "" <<endl;
//Displaying how far they are from the base camp or the end of the game.
cout << "You are " << 65 - playerOne << " away from the base camp!" <<endl;
// cout << "" <<endl;
// system("pause");
// system("CLS");
}
//Starting player 2 turn.
cout << startSecond << endl;
// cout << "" <<endl;
//Prompting user to roll dice.
rollPrompt(1);
//Generating first dice roll.
fDie = diceRoll();
// cout << "" <<endl;
// cout << "" <<endl;
//Prompting user to roll dice again.
rollPrompt(2);
//Generating second dice roll.
sDie = diceRoll();
// cout << "" <<endl;
// cout << "" <<endl;
//Calculating total of the 2 dice rolls.
total = fDie + sDie;
// cout << "" <<endl;
// system("pause");
// system("CLS");
//Displaying total of the 2 dice rolls.
cout << startSecond << endl;
cout << "Your total dice roll is " << total << endl;
//Adding dice rolls to player two score to keep track of his/her position on the board.
playerTwo+=total;
// cout << "" <<endl;
if (playerTwo == 7 || playerTwo == 14 || playerTwo == 21 || playerTwo == 28 || playerTwo == 35 || playerTwo == 42 || playerTwo == 49 || playerTwo == 56) {
cout << "You got in if" << endl; // Added this, so you can notice when you get in if, remove it later
anicrdsT = rand()%3+1;
PTanicrds += anicrdsT;
cout << "You landed on a Binoculars." << PTanicrds << endl;
}
//Displayer total position of player on the board.
cout << startSecond << " is currently on " << playerTwo << endl;
// cout << "" <<endl;
//Displaying how far the player is from the base camp or the end of the game.
cout << "You are " << 65 - playerTwo << " away from the base camp!" <<endl;
// cout << "" <<endl;
// cout << "" <<endl;
// system("pause");
// system("CLS");
//Conditions for the loop.
} while (playerOne <= 65 || playerTwo <= 65);
//Condition to determine the winner of the game.
if (playerOne >= 65) {
cout << "The winner is " << startFirst << endl;
} elseif (playerTwo >= 65) {
cout << "The winner is " << startSecond << endl;
};
return 0;
}
Well, I mostly commented things out.
Added couple things and changed some things, I added comments about what I changed roughly.
add a breakpoint on the line where your 'dodgy' if statement is. when you hit it examine the contents of your playerTwo/playerOne variables just to make 100% sure that the if condition should evaluate to true.
(or print out the values of playerOne and playerTwo just before the if statements)