+ 3

Casting

Hi! I have an exercise in c++ where I have to transform a floating point to an integer, doing some changes and after that to change back the integer to float . I tried many ways to transform, the visual studio doesn't give error, but isn't transforming. Ex: Int a; Float b=0; B=b+a Or a=(float)a I tried The cast function as well

20th Nov 2018, 5:48 AM
Olga Mohan
Olga Mohan - avatar
15 odpowiedzi
+ 11
You are close when you specify a=(float)a; The problem here is you have cast an int to a float but are trying to store the result in an int. Try: #include <iostream> using namespace std; int main() { int a = 0; float b = 5.3f; cout << "Before casting:" << endl << a << endl << b << endl; //cast float to int a = (int)b; //cast int to float b = (float)a; cout << "After casting:" << endl << a << endl << b << endl; return 0; }
20th Nov 2018, 5:58 AM
jay
jay - avatar
+ 10
https://code.sololearn.com/cA6dhbRf5sZo/?ref=app What results were you expecting and what are the input values? Try copy and paste the following code into visual studio. Are the results the same?
20th Nov 2018, 6:31 AM
jay
jay - avatar
+ 5
It might be a compiler setting, it should/will give you a warning about loss of precision but it 'should' compile
20th Nov 2018, 6:02 AM
jay
jay - avatar
+ 5
Also please note that the following structure is a C-style way of doing the job (target_type)variable_name or (target_type)immidiate value In C++, there's an ugly form of conversion between two static types which was a deliberate design decision for making any sort of typecasting in the codebase distinguishable easily like so static_cast<target_type>(variable_name) It comes down to the fact that the good practice emphasizes minimizing all sort of typecasting (making the types consist by forcing the compiler to ignore the danger) throughout the program.
20th Nov 2018, 6:10 AM
Babak
Babak - avatar
+ 4
Olga Mohan The best advice I can give you is to install a second IDE like Code::Blocks on your machine for days like today when you come up with some hiccups. Hopefully you're gonna find the issue pretty soon. Also don't forget to use F10 key and 'watch' feature for keeping track of the program flow.
20th Nov 2018, 6:59 AM
Babak
Babak - avatar
+ 3
This is what I have done and is not working in visual studio and I don't find the answer why. Sorry I wrote incorrect example
20th Nov 2018, 6:00 AM
Olga Mohan
Olga Mohan - avatar
+ 3
Thank you for your help , appreciate, but is not working. I definitely have a problem with some settings. :( But at list I know I am not wrong
20th Nov 2018, 6:36 AM
Olga Mohan
Olga Mohan - avatar
+ 3
Thank you!
20th Nov 2018, 8:39 AM
Olga Mohan
Olga Mohan - avatar
+ 3
You _declared_ a as int. a will _always_ be int. a = 19.666; should result in 20. You _cannot_ say a = (float) a; coz: int a = 5.0; // For example // a = 5 coz float is rounded a = (float) a; // What you did a = 5.0; // Result of what ya did // a = 5 as per above explanation Hope you _understand_ now. You returned float to an int in your example by doing a = (float) a; thus being like a = 1.0; which would yield a value of 1 for a.
21st Nov 2018, 12:36 AM
non
+ 1
Thank you!
20th Nov 2018, 6:13 AM
Olga Mohan
Olga Mohan - avatar
+ 1
I tried with static_cast and is the same result. There is a problem with settings or something?, doesn't give error but isn't transforming
20th Nov 2018, 6:22 AM
Olga Mohan
Olga Mohan - avatar
0
Thanks I will try to find out :D
20th Nov 2018, 6:07 AM
Olga Mohan
Olga Mohan - avatar
0
здравствуйте
23rd Nov 2018, 9:48 AM
Ёрмуҳаммад
0
тут кто-то может говорит на русском языке
23rd Nov 2018, 9:49 AM
Ёрмуҳаммад
0
Hello friends,I am not getting upcasting and downcasting can you explain please?
16th Nov 2020, 4:01 AM
Rohini S