Invalid operands error for addition and integer division?
In count = (count/2)
and pairs = (pairs+count)
, you aren't referring to the int
you declared because it isn't in scope. You're actually referring to the function std::count
because you have using namespace std;
somewhere in that compilation unit. This is one of the reasons you shouldn't do that.
To fix this you need to declare count
in the appropriate scope:
int sockMerchant(int n, vector<int> ar) {
int pairs;
int count = 0;
for (int i = 1; i <= 100 ; i++) { //for every number ("Color") between 1 and 100
for (int j = 0; j < n; j++) { //iterate thru array to look for it
if (ar[j] == i) { //once found,
for (int k = j; k < n ; k++) { //count how many of that number is there
if (ar[k] == i) {
count++;
}
}
count = (count/2);
pairs = (pairs+count);
}
}
}
return pairs;
}
Invalid operands error for addition and integer division?
In count = (count/2)
and pairs = (pairs+count)
, you aren't referring to the int
you declared because it isn't in scope. You're actually referring to the function std::count
because you have using namespace std;
somewhere in that compilation unit. This is one of the reasons you shouldn't do that.
To fix this you need to declare count
in the appropriate scope:
int sockMerchant(int n, vector<int> ar) {
int pairs;
int count = 0;
for (int i = 1; i <= 100 ; i++) { //for every number ("Color") between 1 and 100
for (int j = 0; j < n; j++) { //iterate thru array to look for it
if (ar[j] == i) { //once found,
for (int k = j; k < n ; k++) { //count how many of that number is there
if (ar[k] == i) {
count++;
}
}
count = (count/2);
pairs = (pairs+count);
}
}
}
return pairs;
}
Why am i Getting this error? error: invalid operands of types 'int' and 'int(int, int)' to binary 'operator/'
You are getting the error because you cannot divide integers by functions.
Instead of
int rem = lcm / gcd;
it seems you wanted to call the function gcd
.
int rem = lcm / gcd(n1, n2);
Use of pointers in a division in C99, error: invalid operands to binary
Your immediate problem is that you have declared x
as a pointer to int, but you are trying to use it as an int. To simply correct the last line, dereference the pointer (just like you've correctly done inside test()
):
float res = *x / 2;
Now, it appears you actually tried that and got an error, which is not surprising, because you've initialized x
badly:
int *x = 20;
This doesn't create an int value of 20 and make x point to it. It sets x to point at the memory address represented by the integer value 20. That's probably reserved memory, which is why you get an error when you try to dereference it.
(You don't get that error in test()
because you've passed the address of x
as the argument. So dereferencing it there actually does get you 20 - probably.)
To make the pointers work, either do:
int x = 20;
...
test(&x, y)
...
float res = x / 2;
or:
int *x = malloc(sizeof(int));
*x = 20;
...
test(x, y)
..
float res = *x /2;
But you are really making this too difficult. Since you only need to output one value from the function, just make the function return that value. Then you have no need to mess about with pointers at all:
int test(x,y) { return x+y; }
...
int x = 20;
...
x = test(x,y);
...
float res = x / 2;
(And finally, I believe that in any case you want to use 2.0
in the last line, not just 2
, if you want to get a float result instead of an int.)
what's wrong in int=float%int;?
There is a constraint such that both operands of the %
operator must have integer type:
6.5.5 Multiplicative operators
...
Constraints
2 Each of the operands shall have arithmetic type. The operands of the%
operator shall
have integer type.
C 2011 Online Draft
Emphasis added.
If you need to get a remainder on division of float
values, use fmod()
:
float lftovr = fmod( m, 100.0f );
In function 'main': error: invalid operands to binary % (have 'float' and 'int') [duplicate]
Quoting C11
, chapter 6.5.5
The operands of the
%
operator shall have integer type.
num
is a floating point variable. You may need to use fmod()
instead.
That said,
scanf("%f", num);
is wrong, you need to supply an address to a float
, not a float
variable itself. At least you'd need
scanf("%f", &num);
keeping aside the success and sanity check.
error: invalid operands to binary % when taking modulus of float [duplicate]
%
is an integer operator - use fmod or fmodf for doubles or floats.
Alternatively if you expect your float to represent integer values then convert it to an int
first, e.g.:
if ((int)someFloat % 2 == 1) // if f is an odd integer value
{
...
}
Related Topics
Calling C++ Member Functions Via a Function Pointer
How to Enforce Move Semantics When a Vector Grows
When to Use References Vs. Pointers
Why Can't I Make a Vector of References
Check If a Class Has a Member Function of a Given Signature
What Will Happen When I Call a Member Function on a Null Object Pointer
Multicharacter Literal in C and C++
Reflection and Refraction Impossible Without Recursive Ray Tracing
What Is a "Cache-Friendly" Code
What Is Std::Move(), and When Should It Be Used
How to Concatenate a Std::String and an Int
Virtual/Pure Virtual Explained
Size of Character ('A') in C/C++
Can a Class Member Function Template Be Virtual
Why Can't C++ Be Parsed With a Lr(1) Parser
Conditions For Automatic Generation of Default/Copy/Move Ctor and Copy/Move Assignment Operator