Javascript If Else If statement issues [closed] - if-statement

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am having an issue with an If Else If statement in my code. Basically, I have four four element arrays (Table1, Table2, Table3, Table4). The value of a variable (frameSizeClass) taken from drop down menu will determine which of the arrays to use (via the If Else If statement), and another variable will determine which element in the array to return to a text box.
What is happening is that regardless of what the value of frameSizeClass is, it acts as if the variable=1, and return values from array Table1. If I comment out the other portions of the If Else If and test the individual If statements, they work, but not as a combined If Else If. I'm at a loss and would welcome any help. Code is below.
if (frameSizeClass = 1){
baseLubeIntVal = greaseTable1[ratedSpeedClass];
} else if (frameSizeClass = 2){
baseLubeIntVal = greaseTable2[ratedSpeedClass];
} else if (frameSizeClass = 3){
baseLubeIntVal = greaseTable3[ratedSpeedClass];
} else if (frameSizeClass = 4){
baseLubeIntVal = greaseTable4[ratedSpeedClass];
}

In most programming languages the = operator means assignment and == means equal.
The correct code would look like this
if (frameSizeClass == 1){
baseLubeIntVal = greaseTable1[ratedSpeedClass];
} else if (frameSizeClass == 2){
baseLubeIntVal = greaseTable2[ratedSpeedClass];
} else if (frameSizeClass == 3){
baseLubeIntVal = greaseTable3[ratedSpeedClass];
} else if (frameSizeClass == 4){
baseLubeIntVal = greaseTable4[ratedSpeedClass];
}

Related

How do i get rid of this error right operand of comma operator has no effect ( wunsued value)? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
i am having an issue with this code so all the tarif = x; is underlined when i run the program, but i don't see how i could solve this and exactly what is the error ? thank you for your help !
double saaq::Camion::tarificationAnnuelle() const
{
double tarif;
if(m_nbEssieux == 2 && m_poids >= 3001 && m_poids <= 4000)
{
tarif = 570,28;
}
if(m_nbEssieux == 2 && m_poids >= 4001)
{
tarif = 905,28;
}
if(m_nbEssieux == 4)
{
tarif = 2206,19;
}
if(m_nbEssieux == 5)
{
tarif = 2821,76;
}
if(m_nbEssieux >= 6)
{
tarif = 3729,76;
}
return tarif;
}
It's an obvious typo when you write double constant:
tarif = 570,28;
should be
tarif = 570.28;
that applies to all other double assignments..
Also you should initialise your tarif variable,
double tarif = 0;

Why does using the and operator to compare 2 objects in an if statement throw an error when using 2 if statements does not [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I a method isTileEqual in my tile class, I am trying to compare the private variables shape and colour with another tile class. When I use the current line using the and operator Visual Studio Code gives me an error "expression must be a modifiable value"
when i nest the if statements as commented out below I receive no error.
bool Tile::isTileEqual(Tile tile) {
Colour colour = tile.getColour();
Shape shape = tile.getShape();
if(this->shape == shape && this->colour = colour) {
//if(this->colour == colour) {
return true;
//}
}
return false;
}
The expected result would be true if 2 tiles have identical properties. or false if they differ
You've got only one = on the one that uses &&.
With a single = it is an assignment, you want to do a comparison.
Should be:
if(this->shape == shape && this->colour == colour) {
I'd also suggest that you just return the value of the expression directly:
return this->shape == shape && this->colour == colour;
The if (x) return true; else return false; ... is considered ugly/bad practice.

C++ comparing string elements to an int [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have number lying in a string variable.
I wanna check if every one of its elements is equal to some value, so I use the for loop to loop over every element and use if:
int zera = 0, jedynki = 0;
for (int i = 0; i < liczba.length(); i++) {
if (liczba[i] == 0) zera ++;
else if (liczba[i] == 1) jedynki ++;
}
liczba is a string.
I know now that I can't do that. I tried to convert this int into char but still, nothing happened.
What's wrong here? What should I do?
you're comparing int with char
should be:
if (liczba[i] == '0') {}
else if (liczba[i] == '1') {}
should be used:
if (liczba[i] == '0')
or using atoi:
if (atoi(liczba[i]) == 0)

error: expected unqualified-id before if [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have looked through previous answers, but none of them have explained why I am getting this error.
Here is my code with the error. It occurs on "if(pathID==2...)" and every if statement after that.
void add_path(int a,int b, int current_step,int pathID){
if(pathID == 0){
path[current_step] = new step(a,b,"Filled A",path[current_step]);
}
if(pathID == 1)
path[current_step] = new step(a,b,"Filled B",path[current_step]);
}
if(pathID == 2){
path[current_step] = new step(a,b,"Empty A",path[current_step]);
}
if(pathID == 3){
path[current_step] = new step(a,b,"Empty B",path[current_step]);
}
if(pathID == 4){
path[current_step] = new step(a,b,"Pour B to A",path[current_step]);
}
if(pathID == 5){
path[current_step] = new step(a,b,"Pour A to B",path[current_step]);
}
}
All this code is meant to do is add to the linked list at a given position in an array. The pathID is passed in and tells it what action was performed, so we know we know what to add to the linked list.
Later in the program I use that linked list to determine what actions were taken. I still need to make it a doubly linked list so it does not print in reverse, but that's another problem.
You forgot a curly brace after
if(pathID == 1)
Add it and it'll work fine.

c++ variable keeps resetting instead of incrementing? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
void RetailerOrder::addItem(Product* p)
{
bool space = false;
int counter = 0;
while ((space == false) && (counter < manifest.size()))
{
if (manifest[counter] == nullptr);
{
manifest[counter] = p;
space = true;
}
counter++;
}
if (space == false)
{
cout << "no space" << endl;
}
}
Why does the counter reset to zero with each pass through the while loop? If I use it as it is, only the last product that I enter gets stored in the array because i is always 1. Is there a way to let the counter increase.
if (manifest[counter] == nullptr); // << see here
{
manifest[counter] = p;
space = true;
}
That semi-colon at the end of your if statement means "if condition, do nothing".
Then the braced scope following that executes always.
It should instead be:
if (manifest[counter] == nullptr)
{
manifest[counter] = p;
space = true;
}
As an aside, I'm not a big fan of things like:
if (space == false) ...
since, if you name you boolean values intelligently, you can avoid the comparisons:
if (! foundSpace) ...
The intelligent naming means that the boolean variable itself should be readable, such as with foundSpace or haveFinished or userIsClinicallyInsane. Once it is, you don't need to compare it to boolean constants at all.
That greatly reduces the code size and avoids the reductio-ad-absurdum situation where you don't know when to stop comparing booleans to constants:
if ((((haveFinished == true) == true) != false) == true) ...
As a second aside, I value my vertical space greatly when looking at code and there are ways to simplify your code which make it shorter and, in my opinion, easier to understand.
Since you're only using space as a way to insert the item and exit the loop prematurely, you could use something like:
void RetailerOrder::addItem (Product* p) {
// Check every possible slot.
for (int i = 0; i < manifest.size(); i++) {
// If one free, use it and exit.
if (manifest[i] == nullptr) {
manifest[i] = p;
return;
}
}
// None were free, complain bitterly.
cout << "no space" << endl;
}
Still, that's a stylistic approach of mine and you should feel free to ignore it if you disagree.
The "multiple return points are bad" crowd may not like it but that's usually because they don't understand the reason why it's considered suspect, the inability to easily see the many code paths. With a function of this size, it's not really a issue.