I have four if conditions and for all four conditions else is single.
I four conditions are true they have their own result to be executed but if they all fail they should enter in else part which common for all and can be executed only once.
For example, for save button all four fields should have values if not, respective field will throw an error and stop saving form further.
c.save = function () {
if(a == '' || a == undefined){
throw an error for a
}else
if(b == '' || b == undefined){
throw an error for b
}else
if(c == '' || c == undefined){
throw an error for c
}else
if(d == '' || d == undefined){
throw an error for d
}else
c.data.save = true;
}
You could use a bool to check if all if statements have failed.
bool someBoolean = true;
if(a == true) {
// code to execute
someBoolean = false;
}
else if(b == true) {
// code to execute
someBoolean = false;
}
else if(c == true) {
// code to execute
someBoolean = false;
}
else if(d == true) {
// code to execute
someBoolean = false;
}
// check if the bool is still true
if(someBoolean) {
// code to execute if all if statements failed
}
And then all you have to do is set the bool back to it's original state to use it again.
Related
I have run into a problem while creating the if statements for the autonomous car. The car skips most of the if statements and immeaditly goes to the else statement. The sensors give of the right values. Is it because i use "else if" statements or something else? The car is supposed to react to its surroundings, so i had to give it many if statements as possible. But instead it just does the last bit where it goes backwards waits goes backwards left and backwards right. So my question is do i have to add more if statements so it reacts better to its surroundings or is there more to it? Here is the code of the if statements:
if (sensors[0] >= 50 ) { //if the distance of the front sensor is greater than 50cm, than set Fwd true. Otherwise its false.
Fwd = true;
} else {
Fwd = false;
}
delay(50);
if ((Fwd == true) && (sensors[1] > 50) && (sensors[2] > 50)) {
fwd();
} else if ((Fwd == true) && (sensors[1] < 50)) {
fwdRight();
} else if ((Fwd == true) && (sensors[2] < 50)) {
fwdLeft();
} else if ((Fwd == false) && (sensors[1] < 50) && (sensors[2] < 50)) {
Stp();
} else if ((Fwd == false) && (sensors[1] < 50)) {
bwdRight();
} else if ((Fwd == false) && sensors[2] < 50) {
bwdRight();
} else {
Stp();
delay(1000);
bwd();
delay(500);
bwdLeft();
delay(500);
bwdRight();
}
Start by tidying up your code, and it may be obvious where things may be going wrong. For example, you are calling multiple checks to Fwd by doing:
if ((Fwd == true) && ... ) {
...
} else if ((Fwd == true) && ... ) {
...
} else if ((Fwd == true) && ... ) {
...
} else if ((Fwd == false) && ... ) {
...
} else if ((Fwd == false) && ... ) {
...
}
This uses up valuable resources in your program memory. It would be much more efficient to do a single check, and evaluate from there:
if (Fwd){
// Check sensor conditions here
} else {
// Check more sensor conditions here
}
In fact, you could probably omit the Fwd variable (unless you are using it elsewhere) altogether, saving you more memory space:
// Check whether to go forward or backwards.
// >= 50 - forward
// < 50 - backward
if (sensors[0] >= 50) {
// Check sensor conditions here
} else {
// Check more sensor conditions here
}
Overall, you could end up with something like:
// Check whether to go forward or backwards.
// >= 50 - forward
// < 50 - backward
if (sensors[0] >= 50) {
// Going forward, but which direction?
if (sensors[1] < 50) {
fwdRight();
} else if (sensors[2] < 50) {
fwdLeft();
} else {
// sensors[1] >= 50 AND sensors[2] >= 50
// Going straight forward
fwd();
}
} else {
// Check backward sensor conditions here
}
This answer might not directly answer your question, but it should help you diagnose better what is going on.
I'm writing board game and I need following functionality: player rolls two dices, if he rolled doubles (same number on both dice), he gets to roll again, if he rolled doubles again, he goes to jail.
In my Game class it looks like that
void logic::Game::rollTheDice() {
m_throwsInCurrentTurn++;
int firstThrow = m_firstDice.roll();
int secondThrow = m_secondDice.roll();
m_totalRollResult += firstThrow + secondThrow;
if (firstThrow == secondThrow) m_doublesInCurrentTurn++;
}
std::string logic::Game::checkForDoubles() {
std::string message;
if (m_doublesInCurrentTurn == 0 && m_throwsInCurrentTurn == 1) {
m_canThrow = false;
m_canMove = true;
}
if (m_doublesInCurrentTurn == 1 && m_throwsInCurrentTurn == 1) {
message = "Doubles! Roll again.";
m_canThrow = true;
m_canMove = false;
}
if (m_doublesInCurrentTurn == 1 && m_throwsInCurrentTurn == 2) {
m_canThrow = false;
m_canMove = true;
}
if (m_doublesInCurrentTurn == 2 && m_throwsInCurrentTurn == 2) {
message = "Doubles again! You are going to jail.";
m_canThrow = false;
m_canMove = false;
getActivePlayer().lockInJail();
}
return message;
}
void logic::Game::setInMotion(unsigned number) {
m_players[m_activePlayer].startMoving();
m_players[m_activePlayer].incrementPosition(number);
}
m_canThrow basicly enables or disables ability to click "Roll the Dice" button, m_canMove decides if player token can start moving, m_players[m_activePlayer] is std::vector<Player>, startMoving() does that,
void logic::Player::startMoving() {
m_isMoving = true;
}
needed for token movement, so baiscly not relevant here.
Last function from Game class I need to show you is reset(), used mainly for testing purposes
void logic::Game::reset() {
m_throwsInCurrentTurn = 0;
m_doublesInCurrentTurn = 0;
m_totalRollResult = 0;
}
Now finnaly Unit Test that sometimes goes wrong. Sometimes, I mean completely random, like 1 out of 10-20 times.
//first throw is double, second throw is not
TEST_F(GameTestSuite, shouldFinishAfterSecondRollAndMove) {
auto game = m_sut.get();
do {
if (game.getThrowsInCurrentTurn() == 2) game.reset();
game.rollTheDice();
game.checkForDoubles();
if (game.getThrowsInCurrentTurn() == 1 && game.getDoublesInCurrentTurn() == 1) {
ASSERT_EQ(game.canThrow(), true);
ASSERT_EQ(game.canMove(), false);
}
} while (game.getThrowsInCurrentTurn() != 2 && game.getDoublesInCurrentTurn() != 1);
ASSERT_EQ(game.canThrow(), false);
ASSERT_EQ(game.canMove(), true);
game.setInMotion(game.getTotalRollResult());
ASSERT_EQ(game.getActivePlayer().isMoving(), true);
ASSERT_EQ(game.getActivePlayer().getPosition(), game.getTotalRollResult());
}
This line exactly, ASSERT_EQ(game.canThrow(), false); sometimes is equal true after do-while loop that should end once m_canThrow is set to false
Shouldn't:
} while (game.getThrowsInCurrentTurn() != 2 && game.getDoublesInCurrentTurn() != 1);
be
} while (game.getThrowsInCurrentTurn() != 2 && game.getDoublesInCurrentTurn() <= 1);
You want to allow up to two turns but 0 or 1 doubles.
I have been working on a menu that is injected with a dll into call of duty modern warfare 3. As I added options to the menu, I wanted to create a void that allows me to swap a bool value.
I have tried that:
void swapBool(bool& xbool)
{
if (xbool)
!xbool;
else if (!xbool)
xbool;
}
However, it isn't working.
This is what I want it to achieve:
if (displayInfoBox)
displayInfoBox = false;
else if (!displayInfoBox)
displayInfoBox = true;
Because of that many bools and many more coming i wanted to create a void...
if (offHostScroll == 0)
{
if (rgbEffects)
rgbEffects = false;
else if (!rgbEffects)
rgbEffects = true;
}
else if (offHostScroll == 1)
{
if (rgbMenu)
rgbMenu = false;
else if (!rgbMenu)
rgbMenu = true;
}
else if (offHostScroll == 2)
{
if (rgbBakcground)
rgbBakcground = false;
else if (!rgbBakcground)
rgbBakcground = true;
}
else if (offHostScroll == 3)
{
if (rgbMaps)
rgbMaps = false;
else if (!rgbMaps)
rgbMaps = true;
}
else if (offHostScroll == 4)
{
if (rgbInGameIcons)
rgbInGameIcons = false;
else if (!rgbInGameIcons)
rgbInGameIcons = true;
}
else if (offHostScroll == 5)
{
swapBool(displayInfoBox);
/*if (displayInfoBox)
displayInfoBox = false;
else if (!displayInfoBox)
displayInfoBox = true;*/
}
Simply:
displayInfoBox = !displayInfoBox;
or
xbool = !xbool;
! by itself does not change the value of the variable, it just creates a new value by negating it, so you have to reassign it.
You also will have to make swapBool pass xbool by reference.
void swapBool(bool& xbool);
I am looking for best way to implement this scenario:
I have 4 objects that have Boolean member that in the flow of the app sometimes they are set to true and sometimes are set to false depending on conditions;
Then I have final function that gets 1 of this objects and needs to check if in the other 3 objects one of them has the member set to true .
The problem is I know how to do the dirty check , and I am searching for cleaner way here is my code for the final function:
class Obj
{
public :
Obj(int _id) : id(_id)
bool status;
int id // only 4 objects are created 0,1,2,3
}
m_obj0 = new Obj(0) ;
m_obj1 = new Obj(1) ;
m_obj2 = new Obj(2) ;
m_obj3 = new Obj(3) ;
bool check(Obj* obj)
{
if(obj->id == 0)
{
if(m_obj1->status || m_obj2->status || m_obj3->status)
{
return true;
}
return false;
}else if(obj->id == 1)(
if(m_obj0->status || m_obj2->status || m_obj3->status)
{
return true;
}
return false;
}else if(obj->id == 2)(
if(m_obj0->status || m_obj1->status || m_obj3->status)
{
return true;
}
return false;
}else if(obj->id == 3)(
if(m_obj0->status || m_obj1->status || m_obj2->status)
{
return true;
}
return false;
}
is there a shorter and cleaner way to accomplish this check function ?
You can set m_obj as an array. Then use a for loop to check
bool check(Obj* obj)
{
for (int i = 0; i < 4; i ++) {
if (obj->id == i) continue;
if (m_obj[i]->status == true)
return true;
}
return false;
}
Or add them together, then subtract m_obj[obj->id]->status。Check the result is zero or not
bool check(Obj* obj)
{
int result = m_obj[0]->status+m_obj[1]->statusm_obj[2]->status
+m_obj[3]->status-m_obj[obj->id]->status;
return (result!=0);
}
I have this method in one of my cpp files where I have navigated my failure to be. I have also added cout statements and checked that there is content in both foo and mainWord. I think my problem has to do with how I've added elements to foo or how I am trying to re-add them. The size mainWord is 88 and the size of foo is more than 1000. Here is where I add elements to foo:
while (myfile>>magic)//store the colours in an array
{
foo.push_back(magic);
}
and here is where I try and change them and add them back in.
void Penguin::addWord(std::vector<int> foo)
{
unsigned fooCounter=0;
int temp;
for (int i=0;i<88;i+2)
{
if(foo.at(fooCounter) == 11111111 && foo.at(fooCounter) != NULL)
{
if(mainWord[i]==1 && mainWord[i+1]==1)
{
foo.at(fooCounter) = 11111111;
}
else if(mainWord[i]== 1 && mainWord[i+1] == 0)
{
foo.at(fooCounter) = 11111110;
}
else if(mainWord[i]== 0 && mainWord[i+1] == 1)
{
foo.at(fooCounter) = 11111101;
}
else
{
foo.at(fooCounter) = 11111100;
}
}
else if (foo.at(fooCounter) == 11111111 && foo.at(fooCounter) != NULL)
{
if(mainWord[i]== 1 && mainWord[i+1] == 1)
{
foo.at(fooCounter) = 00000011;
}
else if(mainWord[i]== 1 && mainWord[i+1] == 0)
{
foo.at(fooCounter) = 00000010;
}
else if(mainWord[i]== 0 && mainWord[i+1] == 1)
{
foo.at(fooCounter) = 00000001;
}
else
{
foo.at(fooCounter) = 00000000;
}
}
fooCounter++;
}
}
I keep getting an error that says: "Debug Assertion Failed. Vector subscript out of range"
...Please help
You have an infinite loop because i is never updated in the for loop. You need to fix the typo:
for (int i=0;i<88;i+=2)
^
^