Swift 3 - convert Int32 to Int [duplicate] - casting

This question already has answers here:
How can I convert Int32 to Int in Swift?
(4 answers)
Closed 5 years ago.
Using Swift 3 and am having problems comparing an Int32 and Int in an IF statement.
// myIntFromCoreData is an Int32
// kMyConstant is an Int
if myIntFromCoreData == kMyConstant // error: Binary operator "==" cannot be applied to operands of type 'Int32' and 'Int'
{
Log("Cool")
}
So, I tried casting the values to Int32 or Int using:
myint32.intValue // Value of type 'Int32' has no member 'intValue'
myint32 as Int // Cannot convert value of type 'Int32' to type 'Int' in coercion
kMyConstant as Int32 // Cannot convert value of type 'Int' to type 'Int32' in coercion
But kept getting the above errors. Can anybody provide some direction no how to handle this?

You can try this.
let number1: Int32 = 10
let number2 = Int(number1)

Related

Why will it not let me pass an int as an int argument? [duplicate]

This question already has answers here:
Returning an array using C
(8 answers)
What is array to pointer decay?
(11 answers)
Closed 20 days ago.
I'm trying to create a function that inserts an integer into an array, but it gives an error when calling it, because it tells me that it needs to be a pointer I think.
The function is
int *arrayShifter(int myArray[], int arraySize, int insertLoc, int insertVal)
and I call it with
int s[7] = {1,2,3,5,6,7, 8};
int z[8];
z = arrayShifter(s, 7, 3, 4);
it gives the error
error: incompatible types in assignment of 'int*' to 'int [8]'

problem during typecasting during function calling [duplicate]

This question already has answers here:
Convert char to int in C and C++
(14 answers)
Closed 2 years ago.
In the program i am converting the character datatype into integer but it is displaying the ASCII code of the character, why? what should i do to print 4 as output?
void fun(int x)
{
cout<<x;
}
int main()
{
char ch='4';
fun((int)ch);
return 0;
}
I have tried changing the parameter from 'int x' to 'char x' and then typecasting in cout as 'cout<<(int)x;'
Casting a char to int will do what it should, cast the stored value into an integer. When you say char ch='4';, the variable holds the ASCII value of '4', it does not store the integer value 4. So casting it will not give you the integer value 4.
To get the integer value of ch (get 4 from '4'), we can subtract the integer value by '0' so that we get the actual integer value,
void fun(int x)
{
cout << x - '0';
}

Why does trying to print enum class in c++ give an error? [duplicate]

This question already has answers here:
How can I output the value of an enum class in C++11
(9 answers)
Closed 2 years ago.
I want to print u that has km as unit
enum class Unit { km, m, cm };
int main()
{
Unit u = Unit::km;
std::cout<<u;
return 0;
}
Why do i get an error?
error:
error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream}’ and ‘Unit’)
Your Unit enum is a scoped enumeration, enum class, these types of enums don't allow implicit casting. You'll have to expliticly cast if you want it to work with cout:
std::cout << static_cast<int>(u);

Compiler showing warning during compilation of the following code [duplicate]

This question already has answers here:
warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)’
(3 answers)
Closed 5 years ago.
#include<stdio.h>
int main()
{
char str[6][50] ; // a 2d character array
int i ;
for(i = 0 ; i < 6 ; i++)
{
scanf("%s",(str+i)) ; // in here the warning was shown
}
return 0 ;
}`
During Output :-
scanf() is showing warning on compilation - warning: format ‘%s’ expects argument of type ‘char ’, but argument 2 has type ‘char ()[50]’ [-Wformat=]
Remember that arrays naturally decays to pointers to their first element? That means str by itself is treated as a pointer. With str + i you are doing pointer arithmetic, the result of which is another pointer (to the i:th element in this case). The type of that pointer is a pointer to an array, char (*)[50], it's not the array itself.
What you need to do is to dereference the pointer: *(str + i). Or as it's the same, str[i].
It seems like you are trying to access each element of str in turn. To do this use scanf("%s",str[i]); (as per usr's comment).

Convert from Object to Float not working c++ [duplicate]

This question already has answers here:
Why does dividing two int not yield the right value when assigned to double?
(10 answers)
Closed 6 years ago.
I'm trying to convert a object to a float, but it doesn't seems to work the way I want.
int main()
{
Fraction A(20,6);
float E;
E = A;
cout << E << endl;
}
I already did a type convert operator inside the class
operator float () const
{
return static_cast<float>(num/den); //Being num and den private members, 20 and 6 in this case.
}
When I run the program, the result is 3 (with no decimal places).
Any help?
I can't see your code, but based on the behavior you're observing, is it possible that num and den are defined as int data types? If so, num/den is being treated as integer division, and therefore resulting in the result being truncated.