boolean value resetting on next iteration of loop












-2














I'm taking classes in C++, C#, and Java right now, so I think I'm getting some stuff mixed up. I'm having trouble setting an objects bool value and what's weird is that the int values I'm setting on a different objects are maintaining the change. I'm originally setting it in a function inside Main. After leaving the function and returning to the Main function, the values are correct. On the next iteration, the first object still has the new values, but the second object has reverted to the original values.



This is the loop in my Main function



    while (true) {

Player player = playerQueue.front();

//stores the current cell the player is on
int cellNumber = gameBoard.findCell(player.getCellNum())->nodeNumber;
Cell currCell = gameBoard.getCellObject(cellNumber);

//check cell property
if (currCell.getCanBuy() == false){
//pay the property owner
} else {
DisplayPlayerInfo(player, currCell);
}
}


This is where the player has an option to buy the cell. The functions for the Player object change properly, but the Cell(prop) object changes, but then reverts after the while loop iterates.



void DisplayPlayerInfo(Player &player, Cell &prop) {

if (input == "y" || input == "Y") {
if (player.getCurrency() >= prop.getPrice()) {

//buy property
prop.setCanBuy(false);
player.subtractCurrency(prop.getPrice());
player.addProperty(prop);
}
}
else if (input == "n" || input == "N"){
return;
} else {
//return error
}
}


These are my setters for the Player object and Cell object respectively



//Player setter
void Player::setPlayerName(std::string name) { pName = name; }
void Player::setQueuePos(int q) { queuePos = q; }
void Player::setCellNum(int num) { cellNum = num; }
void Player::addCurrency(int amt) { currency += amt; }
void Player::subtractCurrency(int amt) { currency -= amt; }

//Cell setter
void Cell::setCanBuy(bool val) { canBuy = val; }


edit: Shortened the code to what I think is relevant to the question.










share|improve this question




















  • 1




    One thing I noticed while skimming through your code is this line: Player player = playerQueue.front(); That creates a copy of the player object, which is probably not what you want. Anyways please create a Minimal, Complete, and Verifiable example so we can properly help.
    – Max Vollmer
    Nov 23 at 0:50










  • @Max Did not know that...I thought it would just store the address of the object. Should I have made it a pointer to a Player object? Player *player = playerQueue.front();
    – Sean
    Nov 23 at 0:54










  • @Neil college requirements are C, C++, and Java. My elective choices were C#, VB, Javascript, it wasn't my idea to take all of them.
    – Sean
    Nov 23 at 1:06










  • Change colleges. The problem is trying to learn three languages at once, not particularly what the languages are. You are being badly taught.
    – Neil Butterworth
    Nov 23 at 1:07








  • 1




    @NeilButterworth the problem is not three languages at once, it's three languages that can all look similar, where one of them that means a wildly different thing
    – Caleth
    Nov 23 at 14:13


















-2














I'm taking classes in C++, C#, and Java right now, so I think I'm getting some stuff mixed up. I'm having trouble setting an objects bool value and what's weird is that the int values I'm setting on a different objects are maintaining the change. I'm originally setting it in a function inside Main. After leaving the function and returning to the Main function, the values are correct. On the next iteration, the first object still has the new values, but the second object has reverted to the original values.



This is the loop in my Main function



    while (true) {

Player player = playerQueue.front();

//stores the current cell the player is on
int cellNumber = gameBoard.findCell(player.getCellNum())->nodeNumber;
Cell currCell = gameBoard.getCellObject(cellNumber);

//check cell property
if (currCell.getCanBuy() == false){
//pay the property owner
} else {
DisplayPlayerInfo(player, currCell);
}
}


This is where the player has an option to buy the cell. The functions for the Player object change properly, but the Cell(prop) object changes, but then reverts after the while loop iterates.



void DisplayPlayerInfo(Player &player, Cell &prop) {

if (input == "y" || input == "Y") {
if (player.getCurrency() >= prop.getPrice()) {

//buy property
prop.setCanBuy(false);
player.subtractCurrency(prop.getPrice());
player.addProperty(prop);
}
}
else if (input == "n" || input == "N"){
return;
} else {
//return error
}
}


These are my setters for the Player object and Cell object respectively



//Player setter
void Player::setPlayerName(std::string name) { pName = name; }
void Player::setQueuePos(int q) { queuePos = q; }
void Player::setCellNum(int num) { cellNum = num; }
void Player::addCurrency(int amt) { currency += amt; }
void Player::subtractCurrency(int amt) { currency -= amt; }

//Cell setter
void Cell::setCanBuy(bool val) { canBuy = val; }


edit: Shortened the code to what I think is relevant to the question.










share|improve this question




















  • 1




    One thing I noticed while skimming through your code is this line: Player player = playerQueue.front(); That creates a copy of the player object, which is probably not what you want. Anyways please create a Minimal, Complete, and Verifiable example so we can properly help.
    – Max Vollmer
    Nov 23 at 0:50










  • @Max Did not know that...I thought it would just store the address of the object. Should I have made it a pointer to a Player object? Player *player = playerQueue.front();
    – Sean
    Nov 23 at 0:54










  • @Neil college requirements are C, C++, and Java. My elective choices were C#, VB, Javascript, it wasn't my idea to take all of them.
    – Sean
    Nov 23 at 1:06










  • Change colleges. The problem is trying to learn three languages at once, not particularly what the languages are. You are being badly taught.
    – Neil Butterworth
    Nov 23 at 1:07








  • 1




    @NeilButterworth the problem is not three languages at once, it's three languages that can all look similar, where one of them that means a wildly different thing
    – Caleth
    Nov 23 at 14:13
















-2












-2








-2







I'm taking classes in C++, C#, and Java right now, so I think I'm getting some stuff mixed up. I'm having trouble setting an objects bool value and what's weird is that the int values I'm setting on a different objects are maintaining the change. I'm originally setting it in a function inside Main. After leaving the function and returning to the Main function, the values are correct. On the next iteration, the first object still has the new values, but the second object has reverted to the original values.



This is the loop in my Main function



    while (true) {

Player player = playerQueue.front();

//stores the current cell the player is on
int cellNumber = gameBoard.findCell(player.getCellNum())->nodeNumber;
Cell currCell = gameBoard.getCellObject(cellNumber);

//check cell property
if (currCell.getCanBuy() == false){
//pay the property owner
} else {
DisplayPlayerInfo(player, currCell);
}
}


This is where the player has an option to buy the cell. The functions for the Player object change properly, but the Cell(prop) object changes, but then reverts after the while loop iterates.



void DisplayPlayerInfo(Player &player, Cell &prop) {

if (input == "y" || input == "Y") {
if (player.getCurrency() >= prop.getPrice()) {

//buy property
prop.setCanBuy(false);
player.subtractCurrency(prop.getPrice());
player.addProperty(prop);
}
}
else if (input == "n" || input == "N"){
return;
} else {
//return error
}
}


These are my setters for the Player object and Cell object respectively



//Player setter
void Player::setPlayerName(std::string name) { pName = name; }
void Player::setQueuePos(int q) { queuePos = q; }
void Player::setCellNum(int num) { cellNum = num; }
void Player::addCurrency(int amt) { currency += amt; }
void Player::subtractCurrency(int amt) { currency -= amt; }

//Cell setter
void Cell::setCanBuy(bool val) { canBuy = val; }


edit: Shortened the code to what I think is relevant to the question.










share|improve this question















I'm taking classes in C++, C#, and Java right now, so I think I'm getting some stuff mixed up. I'm having trouble setting an objects bool value and what's weird is that the int values I'm setting on a different objects are maintaining the change. I'm originally setting it in a function inside Main. After leaving the function and returning to the Main function, the values are correct. On the next iteration, the first object still has the new values, but the second object has reverted to the original values.



This is the loop in my Main function



    while (true) {

Player player = playerQueue.front();

//stores the current cell the player is on
int cellNumber = gameBoard.findCell(player.getCellNum())->nodeNumber;
Cell currCell = gameBoard.getCellObject(cellNumber);

//check cell property
if (currCell.getCanBuy() == false){
//pay the property owner
} else {
DisplayPlayerInfo(player, currCell);
}
}


This is where the player has an option to buy the cell. The functions for the Player object change properly, but the Cell(prop) object changes, but then reverts after the while loop iterates.



void DisplayPlayerInfo(Player &player, Cell &prop) {

if (input == "y" || input == "Y") {
if (player.getCurrency() >= prop.getPrice()) {

//buy property
prop.setCanBuy(false);
player.subtractCurrency(prop.getPrice());
player.addProperty(prop);
}
}
else if (input == "n" || input == "N"){
return;
} else {
//return error
}
}


These are my setters for the Player object and Cell object respectively



//Player setter
void Player::setPlayerName(std::string name) { pName = name; }
void Player::setQueuePos(int q) { queuePos = q; }
void Player::setCellNum(int num) { cellNum = num; }
void Player::addCurrency(int amt) { currency += amt; }
void Player::subtractCurrency(int amt) { currency -= amt; }

//Cell setter
void Cell::setCanBuy(bool val) { canBuy = val; }


edit: Shortened the code to what I think is relevant to the question.







c++ loops return-value mutators






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 at 1:31









Soco

81




81










asked Nov 23 at 0:40









Sean

158




158








  • 1




    One thing I noticed while skimming through your code is this line: Player player = playerQueue.front(); That creates a copy of the player object, which is probably not what you want. Anyways please create a Minimal, Complete, and Verifiable example so we can properly help.
    – Max Vollmer
    Nov 23 at 0:50










  • @Max Did not know that...I thought it would just store the address of the object. Should I have made it a pointer to a Player object? Player *player = playerQueue.front();
    – Sean
    Nov 23 at 0:54










  • @Neil college requirements are C, C++, and Java. My elective choices were C#, VB, Javascript, it wasn't my idea to take all of them.
    – Sean
    Nov 23 at 1:06










  • Change colleges. The problem is trying to learn three languages at once, not particularly what the languages are. You are being badly taught.
    – Neil Butterworth
    Nov 23 at 1:07








  • 1




    @NeilButterworth the problem is not three languages at once, it's three languages that can all look similar, where one of them that means a wildly different thing
    – Caleth
    Nov 23 at 14:13
















  • 1




    One thing I noticed while skimming through your code is this line: Player player = playerQueue.front(); That creates a copy of the player object, which is probably not what you want. Anyways please create a Minimal, Complete, and Verifiable example so we can properly help.
    – Max Vollmer
    Nov 23 at 0:50










  • @Max Did not know that...I thought it would just store the address of the object. Should I have made it a pointer to a Player object? Player *player = playerQueue.front();
    – Sean
    Nov 23 at 0:54










  • @Neil college requirements are C, C++, and Java. My elective choices were C#, VB, Javascript, it wasn't my idea to take all of them.
    – Sean
    Nov 23 at 1:06










  • Change colleges. The problem is trying to learn three languages at once, not particularly what the languages are. You are being badly taught.
    – Neil Butterworth
    Nov 23 at 1:07








  • 1




    @NeilButterworth the problem is not three languages at once, it's three languages that can all look similar, where one of them that means a wildly different thing
    – Caleth
    Nov 23 at 14:13










1




1




One thing I noticed while skimming through your code is this line: Player player = playerQueue.front(); That creates a copy of the player object, which is probably not what you want. Anyways please create a Minimal, Complete, and Verifiable example so we can properly help.
– Max Vollmer
Nov 23 at 0:50




One thing I noticed while skimming through your code is this line: Player player = playerQueue.front(); That creates a copy of the player object, which is probably not what you want. Anyways please create a Minimal, Complete, and Verifiable example so we can properly help.
– Max Vollmer
Nov 23 at 0:50












@Max Did not know that...I thought it would just store the address of the object. Should I have made it a pointer to a Player object? Player *player = playerQueue.front();
– Sean
Nov 23 at 0:54




@Max Did not know that...I thought it would just store the address of the object. Should I have made it a pointer to a Player object? Player *player = playerQueue.front();
– Sean
Nov 23 at 0:54












@Neil college requirements are C, C++, and Java. My elective choices were C#, VB, Javascript, it wasn't my idea to take all of them.
– Sean
Nov 23 at 1:06




@Neil college requirements are C, C++, and Java. My elective choices were C#, VB, Javascript, it wasn't my idea to take all of them.
– Sean
Nov 23 at 1:06












Change colleges. The problem is trying to learn three languages at once, not particularly what the languages are. You are being badly taught.
– Neil Butterworth
Nov 23 at 1:07






Change colleges. The problem is trying to learn three languages at once, not particularly what the languages are. You are being badly taught.
– Neil Butterworth
Nov 23 at 1:07






1




1




@NeilButterworth the problem is not three languages at once, it's three languages that can all look similar, where one of them that means a wildly different thing
– Caleth
Nov 23 at 14:13






@NeilButterworth the problem is not three languages at once, it's three languages that can all look similar, where one of them that means a wildly different thing
– Caleth
Nov 23 at 14:13














1 Answer
1






active

oldest

votes


















0














Cell currCell = `gameBoard.getCellObject(cellNumber);`


This is creating a new object, local to the while loop. A copy of the original object.



void DisplayPlayerInfo(Player &player, Cell &prop) {


This is passing that new object by reference into the function, which is why it maintains the altered values after leaving the function. However, these new altered values are not stored in the original object so when the loop ends, the local object is destroyed along with those altered values. Change the gameBoard.getCellObject(cellNumber); to return a reference instead of a copy of the original object to fix the problem. Cell &getCellObject(int i);






share|improve this answer

















  • 1




    And make currCell a reference
    – Caleth
    Nov 23 at 14:09










  • @Caleth yup...gotta do that too.
    – Sean
    Nov 23 at 15:02











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439447%2fboolean-value-resetting-on-next-iteration-of-loop%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Cell currCell = `gameBoard.getCellObject(cellNumber);`


This is creating a new object, local to the while loop. A copy of the original object.



void DisplayPlayerInfo(Player &player, Cell &prop) {


This is passing that new object by reference into the function, which is why it maintains the altered values after leaving the function. However, these new altered values are not stored in the original object so when the loop ends, the local object is destroyed along with those altered values. Change the gameBoard.getCellObject(cellNumber); to return a reference instead of a copy of the original object to fix the problem. Cell &getCellObject(int i);






share|improve this answer

















  • 1




    And make currCell a reference
    – Caleth
    Nov 23 at 14:09










  • @Caleth yup...gotta do that too.
    – Sean
    Nov 23 at 15:02
















0














Cell currCell = `gameBoard.getCellObject(cellNumber);`


This is creating a new object, local to the while loop. A copy of the original object.



void DisplayPlayerInfo(Player &player, Cell &prop) {


This is passing that new object by reference into the function, which is why it maintains the altered values after leaving the function. However, these new altered values are not stored in the original object so when the loop ends, the local object is destroyed along with those altered values. Change the gameBoard.getCellObject(cellNumber); to return a reference instead of a copy of the original object to fix the problem. Cell &getCellObject(int i);






share|improve this answer

















  • 1




    And make currCell a reference
    – Caleth
    Nov 23 at 14:09










  • @Caleth yup...gotta do that too.
    – Sean
    Nov 23 at 15:02














0












0








0






Cell currCell = `gameBoard.getCellObject(cellNumber);`


This is creating a new object, local to the while loop. A copy of the original object.



void DisplayPlayerInfo(Player &player, Cell &prop) {


This is passing that new object by reference into the function, which is why it maintains the altered values after leaving the function. However, these new altered values are not stored in the original object so when the loop ends, the local object is destroyed along with those altered values. Change the gameBoard.getCellObject(cellNumber); to return a reference instead of a copy of the original object to fix the problem. Cell &getCellObject(int i);






share|improve this answer












Cell currCell = `gameBoard.getCellObject(cellNumber);`


This is creating a new object, local to the while loop. A copy of the original object.



void DisplayPlayerInfo(Player &player, Cell &prop) {


This is passing that new object by reference into the function, which is why it maintains the altered values after leaving the function. However, these new altered values are not stored in the original object so when the loop ends, the local object is destroyed along with those altered values. Change the gameBoard.getCellObject(cellNumber); to return a reference instead of a copy of the original object to fix the problem. Cell &getCellObject(int i);







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 at 13:53









Sean

158




158








  • 1




    And make currCell a reference
    – Caleth
    Nov 23 at 14:09










  • @Caleth yup...gotta do that too.
    – Sean
    Nov 23 at 15:02














  • 1




    And make currCell a reference
    – Caleth
    Nov 23 at 14:09










  • @Caleth yup...gotta do that too.
    – Sean
    Nov 23 at 15:02








1




1




And make currCell a reference
– Caleth
Nov 23 at 14:09




And make currCell a reference
– Caleth
Nov 23 at 14:09












@Caleth yup...gotta do that too.
– Sean
Nov 23 at 15:02




@Caleth yup...gotta do that too.
– Sean
Nov 23 at 15:02


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439447%2fboolean-value-resetting-on-next-iteration-of-loop%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo