If statement inside parenthesis changes the value in a vector - c++

I have a following problem.
I have two vectors correct_data_labels.label and data_labels.label , label is the vector<int > whereas data_labels and correct_data_labels are the instance of my class.
I have a method where I have if statement inside it. When I use IF statement, the argument that I used inside the parenthesis does an arithmetic and changes the value. As seen in the code below: When I run the code it replaces the value written inside an IF statement. So data_labels.label[row] is replaced by correct_data_labels.label[row]
unsigned int num=0;
double percentage=0.00;
for(register unsigned int row=0;row<data_labels.label.size();row++)
{
if((data_labels.label[row]=correct_data_labels.label[row]))
{
num=num+1;
}
}
percentage = (num/data_labels.label.size());
This code is written in c++, I suppose other programming paradigms can answer the above query too.

You used the assignment operator = instead of the equality operator ==. You probably got a warning about it which is why you have an extra set of parenthesis around the expression: suggest parentheses around assignment used as truth value (on my compiler at least)
Replace = with == to do the comparison instead of the assignment.

Related

Single colons in arbitrary expressions?

I need to figure out what this obfuscated C++ code (written by someone else) does. I've figured pretty much everything, except one tricky part:
bool part1(char *flag)
{
int *t = (int *) memfrob(flag, 8);
unsigned int b[] = {3164519328, 2997125270};
for (int i = 0; i < 2; b[i] = ~b[i], ++i);
return !(0<:t:>-0<:b:>+1<:t:>-1<:b:>);
}
What is going on in the return statement of this function? I have no idea what these colons mean...
I've tried googling what does the colon operator in C++ do, but found only answers about class constructors and the conditional expression, which doesn't seem relevant to this problem.
The code is making use of two-letter alternative tokens, also known as "digraphs". Specifically, <: is [, and :> is ].
So, syntax like 0<:t:> is just 0[t], and since array subscripts can be swapped with the array identifier, this is just t[0].
A great tool that can help with deobfuscating code is cppinsights.io. As can be seen in the link, the code is just doing some arithmetic on the array values (ignore the static_cast for this example, it's not important for the purposes of understanding the transformation).

How to overcome null pointer initialisation error? (C/C++)

I'm having a hard time figuring out why the initialisation at NULL of one pointer is bugging my program.
In order to not receive a compilation error, almost all the pointers in my code need to be initialise as nullptr like usual. Problem is, when I tried to use them afterwards, I get a whole bunch of errors telling me the null pointer is invalid. To overcome this error, the buggy pointers are now declare as an array. This way all my problems with this initialisation are gone. Everything seems to work fine until I realise one of my conditional branch was always returning false.
The full code is a bit too long and some parts use french words, so let's just paste what interest us...
struct Numero {
int num;
char client[50];
Message *listeMessage = nullptr;
Numero *suivant = nullptr;
int nbmsg = 0;
};
char buff_char[50];
number = new Numero;
file >> number->client; // getting the right char client[50] from the file
if (number->client == buff_char)
break;
I also tried if(*number->client == *buff_char) without success. I'm now thinking that the problem is probably the array of char, but because of my previous problem I can't really change that so I'm hoping somebody can figure out what's going on here.
Note: Don't suggest anything related to strings, I can't use them because it's part of the challenge here to practice with pointers.
number->client == buff_char
Both of these are arrays, which decay into pointers. They are two different arrays with two different addresses, so this will always return false.
*number->client == *buff_char
This would compare the first element in each array, which is also not what you want.
You can use a standard library function like strcmp.
If you can't use the standard library, loop over each array until one has a different element than the other (not equal), they both have '\0' (equal), or they both reach 50 (equal).
In If do a comparison instead of assignment
if (number->client == buff_char)
In C++ = is the assignment operator. You want == for equality. What your if statement does right now is sets client equal to buff_char and then evaluates based on the newly assigned value of client.

Cannot understand for loop with two variables [duplicate]

This question already has answers here:
How does the Comma Operator work
(9 answers)
Closed 9 years ago.
When I use two variables in a for loop with different conditions two conditions like I have used below i<3,j<2 the for loop is always executing till the second condition fails.
#include<iostream>
#include<conio.h>
using namespace std ;
int main()
{
int i,j ;
for(i=0,j=0;i<3,j<2;i++,j++)
{
cout<<"hello" ;
}
getch() ;
return 0 ;
}
In that code, hello is printed 2 times. Why?
If I use i<3,j<10, "Hello" is printed 10 times. I can't understand why the first condition is being neglected. Is it compiler dependent or something else?
Every thing works normal if I replace with conditions like || (OR) or &&(AND).An other thing is that I cannot initialize i and j in the for loop itself, it is showing me an error, but works fine when I declare variables in C style or one variable outside the for loop, why is it so?
Compiler I have used is Orwell Dev C++.
Thanks in advance.
for(i=0,j=0;i<3,j<2;i++,j++)
is equivalent to
for(i=0,j=0;j<2;i++,j++)
The comma expression takes on the value of the last expression.
Whichever condition is first, will be disregarded, and the second one will be used only.
The for loop consists of:
for(START_STATEMENT; CONDITION_EXPRESSION, LOOP_EXPRESSION) BODY_BLOCK
Where:
START_STATEMENT is any single statement, which may include variable declaration. If you want to declare 2 variables, you can write int i=0, j=0, but not int i=0; int j=0 because the latter are actually 2 statements. Also node, that variable declaration is a part of statement, but cannot be a part of (sub) expression. That is why int i=0, int j=0 would also be incorrect.
CONDITION_EXPRESSION is any single expression that evaluates to a boolean value. In your case you are using a coma operator which has the following semantics: A, B will do:
evaluate A (it will evaluate, not just ignore)
ditch the result of A
evaluate B
return B as the result
In your case: i<3,j<2 you are comparing i<3, you are just ignoring the result of this comparison.
Comma expressions are useful when the instructions have some side effects, beyond just returning a value. Common cases are: variable increment/decrement or assignment operator.
LOOP_EXPRESSION is any single expression that does not have to evaluate to anything. Here you are using the comma expression again, ignoring the result of the left-hand-side. In this case however, you are not using the result anyway, and just using the ++ side effect - which is to increment the values of your variables.
BODY_BLOCK is either a single statement or a block, encapsulated with curly braces.
The above for can be compared to:
{
START_STATEMENT;
while(EXPRESSION) {
BODY_BLOCK;
LOOP_EXPRESSION;
}
}
The c complier always used second condition.
therefore j<2 is used.
use this for loop
for(i=0,j=0;j<10;i++,j++)

Is this a valid assignment in C++?

While I was looking at the library code, I found the below line
int number = config.nodes,i,fanout=numP/2;
I assume config is a pointer to something, but can there be commas in the statement? and make assignment like that?
This declares three variables. It's the same as:
int number = config.nodes
int i;
int fanout = numP/2;
Please note that commas are handled specially in declarations (and argument lists), C++ also has a "comma operator" which is not being used here.
It's valid, number is not being assigned the entire line you see though.
i and fanout are 2 other integers also being created at that time, fanout is also being initialized at this time.
That one line is equivalent to:
int number = config.nodes;
int i;
int fanout = numP/2;
Its basically many declaration:
int number = config.nodes;
int i;
int fanout=numP/2;
A more recognizable way to write this would be:
int number, i, fanout;
number = config.nodes;
fanout = numP/2;
I personally would never write something like your example because it takes too long for the reader to work out what's going on.
I have the following additions:
1) The whitespace is always ignored by the C++ compiler. So,
int number = config.nodes,i,fanout=numP/2;
is equivalent to
// declaring three variables number, i and fanout
int number=config.nodes, i, fanout = numP/2;
The comma here is to tell the compiler that i have more than one variable to declare. So, number will be initialized with config.nodes. If config is a pointer (as you mentioned above), then you cannot access member variables using . operator. You have to use -> instead of ..
2) The following line has different semantic:
// only one variable will be declared, which is number
int number = (config.nodes,i,fanout=numP/2);
Inside the parenthesis is an expression and the comma here is a comma operator. In this case, config, i, fanout and numP are previously defined. The value of comma operator is the value of last expression fanout=numP/2.

C++ string.compare()

I'm building a comparator for an assignment, and I'm pulling my hair out because this seems to simple, but I can't figure it out.
This function is giving me trouble:
int compare(Word *a, Word *b)
{
string *aTerm = a->getString();
string *bTerm = b->getString();
return aTerm->compare(bTerm);
}
Word::getString returns a string*
Error:
In member function `virtual int CompWordByAlpha::compare(Word*, Word*)':
no matching function for call to...
...followed by a bunch of function definitions.
Any help?
You're comparing a string to a string pointer, and that's not valid. You want
return aTerm->compare(*bTerm);
You aren't getting the different uses of the * operator. The use of the * in "string* bTerm = b->getString()" means "bTerm is a pointer to a string". The use of the * inside of compare(*bTerm) means "take the value of the location pointed to by bTerm" instead of just using compare(bTerm) which simply attempts to compare the value of bTerm itself, which is a hex address.
This is also happening on the left side of that call:
aTerm->compare(*bTerm); //this statement
(*aTerm).compare(*bTerm); //is the same as this statement
The -> operator just reduces the amount of typing required.
P.S.: This kind of stuff you could have easily figured out from Google or your programming textbook. Although others may disagree, I don't feel that questions about completely basic syntax have any place on Stack Overflow.