That aside, what makes you think there's a mistake in the real code? How do you know it isn't working properly?
I have a class in which i get a dynamic array with the names of player in it. Are there 3 Players the 4th is automatically the "Computer". Are there more, you can decide whether you want him or not. After the programm gets this array the following function plays into account:
void CStart::round()
{
vector <int> penalties(mNumberOfPlayers); //tried the vector stuff, could also be a dynamic array
int wuerfel1, wuerfel2; //numbers put in by user/player
bool leave = false; // exit programm
bool maexle; //special case for game winning number
bool checking = false; // next player can check the player before him
int direction = 1; //after "maxle" change game direction ( b is checking if a is having maexle so the last player or Computer starts the new round)
int arrayposition = 0; //current player
while (leave == false)
{
CWuerfel::randomnumber(); //two random numbers in int min and int max stored
cout << mPlayer[arrayposition] << ", pls give your first number: ";
cin >> wuerfel1;
cout << mPlayer[arrayposition] << ",pls give your second number: ";
cin >> wuerfel2;
output(wuerfel1, wuerfel2);
if (wuerfel1 == 2 && wuerfel2 == 1) //Mäxle, special case
{
maexle = true;
}
else {
cout << mPlayer[arrayposition + 1] << ", will you trust?";
cin >> proof;
if (wahl == proof)
{
checking = true;
}
}
if (maexle == true)
{
if ((wuerfel1 == max) && (wuerfel2 == min))
{
cout << mPlayer[arrayposition] << " told the truth." << endl;
penalties.at(arrayposition + 1) += 1;
cout << mPlayer[arrayposition + 1] << " has now " << penalties.at(arrayposition + 1) << " penalties." << endl;
}
else
{
cout << mPlayer[arrayposition] << " lied" << endl;
penalties.at(arrayposition) += 1;
cout << mPlayer[arrayposition] << " has now " << penalties.at(arrayposition) << " penalties." << endl;
}
direction = -1; //Change direction
}
elseif (checking == true)
{
if ((max == wuerfel1) && (min == wuerfel2))
{
cout << mPlayer[arrayposition] << " told the truth" << endl;
penalties.at(arrayposition + 1) += 1;
cout << mPlayer[arrayposition + 1] << " has now " << penalties.at(arrayposition + 1) << " penalties." << endl;
}
else
{
cout << mPlayer[arrayposition] << " lied" << endl;
penalties.at(arrayposition) += 1;
cout << mPlayer[arrayposition] << " has now " << penalties.at(arrayposition) << " penalties." << endl;
}
direction = -1; //Change direction
}
else {
direction = 1; //normal direction
}
arrayposition += direction;
if (arrayposition == -1)
{
arrayposition = mNumberOfPlayers - 1; //Backwards
}
if (arrayposition == mNumberOfPlayers)
{
arrayposition = 0; //Vorwards
}
}
}
The code works for more players then 3. But when the number is 3, so the "computer" is added, he gets overtaken when a change of direction accures. So if a has maexle, next round player c starts instead of "Computer", when I write: cout << mPlayer[3] << endl; i get "Computer" as an output, so theoretically he should be there. Heres my Constructor function:
CStart::CStart(int pPlayer) :mPlayer(pPlayer)
{
mPlayer = new string[pPlayer];
if (pPlayer == 3)
{
// special case: will actually be 4 players
// with the 4th player being the computer
mPlayer = new string[4];
mPlayer[3] = computer;
}
else
{
//With Computer or without?
mPlayer = new string[pPlayer];
cout << "DO you like to play with Computer? 1 = yes; 0 = no ";
cin >> wahl;
if (wahl) //Computer gewünscht
{
mPlayer = new string[pPlayer + 1]; //Array plus 1
mPlayer[pPlayer] = computer; //Computer adding
}
}
//Name request
for (int i = 0; i < pPlayer; i++)
{
cout << "Player " << i + 1 << ", pls input your name: ";
cin >> mPlayer[i];
}
//choosed computer or pPlayer forces it -> ammount of Players +1
if ((pPlayer == 3) || (wahl))
{
pPlayer += 1;
cout << "Computer was added as a Player." << endl;
}
}
I hope i made my problem more understandable,
thx for your answers
CStart::CStart(int pPlayer) :mPlayer(pPlayer)
Is that a typo? You throw away this value and set mPlayer in the first statement of the constructor. You also never save the value of pPlayer. Should you?
You also overwrite that value at line 10 or 16, which leaks memory.
And you overwrite the value from line 16 at line 21, leaking memory again.
CStart::CStart(int pPlayer) :mPlayer(pPlayer)
Is that a typo? You throw away this value and set mPlayer in the first statement of the constructor. You also never save the value of pPlayer. Should you?
i give pPlayer to mPlayer, so its shouldnt be thrown away? So its saved in mPlayer?
You also overwrite that value at line 10 or 16, which leaks memory.
you mean i should use vectors? But I thought i just add one extra space to the Array, to add the player "Computer"
i give pPlayer to mPlayer, so its shouldnt be thrown away? So its saved in mPlayer?
This just makes no sense. You're leaking memory. What is pPlayer?
Whatever is going on here just makes no sense. Beginners shouldn't use new, shouldn't use arrays, shouldn't be manually managing memory. Just use a vector.
No. when you say mPlayer = new string[pPlayer + 1]; it does exactly what it says: new string[pPlayer+1] creates an array of strings with pPlayer+1 items in it. Then it assigns the address of the array to mPlayer. If you wanted to save or delete the previous value of mPlayer, that's up to you.
1 2 3
CStart::CStart(int pPlayer) :mPlayer(pPlayer)
{
mPlayer = new string[pPlayer];
On line1, you initialize mPlayer with pPlayer. The rest of the code seems to indicate that mPlayer is a pointer, so it's not clear that this initialization will compile. It certainly doesn't make sense.
Then line 3 creates an array of strings assigns mPlayer to the address of an array. So the assignment at line 1 has no effect.
Follow Repeater's advice and use a vector instead. It handles the memory management for you.