My topic isn't extremely accurate, nonetheless, here is my question.
My question is how to make an int value take a different type of value? This sounds very confusing, here is an example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
usingnamespace std;
int add(int x, int y)
{
return x + y;
}
int main()
{
int number = 89;
longlongint verylonginteger = 8438584392938578439;
int number34 = 34;
//Now i want int number to take the value of verylonginteger + number34
int number = add(verylonginteger + number34);
cout << int number;
}
How do i make int number be able to take the other two values added up without a warning coming up?
I know that if you add two different integer types it doesnt work, (if thats what you mean) but how do you make them add together without getting the warning.
#include <iostream>
usingnamespace std;
longlongint add(longlongint x, int y);
longlongint add(longlongint x, int y)
{
return x + y;
}
int main()
{
longlongint number = 89;
longlongint verylonginteger = 8438584392938578439;
int number34 = 34;
//Now i want int number to take the value of verylonginteger + number34
number = add(verylonginteger, number34);
cout << number;
}
There is a way of converting values to different types, a static cast:
static_cast<longlong>(number);
The '<' and '>' brackets enclose a template. You will probably learn about templates later on, so you don't need to bother with it yet. Between the bigger/smaller than signs you put the goal type, and the name of the variable you want to convert between the round brackets.
I hope this helps!
If not, have a cookie!
(>^w^)>O
...that is also the wrong thing to do, because you are telling the compiler to ignore all the type information it has about 'number' and clobber any memory that happens to be nearby. Don't do that.
If you want to use space, allocate it first the proper way. See Darkmaster's example.