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

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.

Related

C++ can't compare string position and int [duplicate]

This question already has answers here:
Explain integer comparison with promotion
(2 answers)
Does it matter if I don't explicitly cast an int to a char before comparison with a char?
(2 answers)
Closed 3 months ago.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string codigo = "12345678917";
int remainder = 7;
if (remainder == codigo[10]) {
cout<<"equal" << endl;
}
return 0;
}
It's just a simple comparison. This code doesnt print "equal".
I changed the type of the int to char or string but it didnt work. I tried comparison methods and still nothing. Am missing something here?
It did work when i changed remainder to '7'. But idk why comparing with variable doesnt work
in your code you are comparing an int to a char, in this situation there will be an implicit conversion from char to int.
using
cout << "int value: " << (int)codigo[10] << endl;
you can see that the int value of the character is 55, as 7 does not equal 55 the condition will not be true.
It also won't work if you just change the type to char as this will cast 7 to a char which is not the character '7'.
Using single quotes around the 7 causes the value to be a character literal, as it is stored in an int its value will be 55. Since this is equal to the character value of codigo[10] the condition will be true.

Can't cast type int to char using function parameters C++ [duplicate]

This question already has answers here:
Convert an int to ASCII character
(11 answers)
Closed 6 months ago.
Hello im trying to understand why i can't cast type int
To a char using a user defined function with parameters.
Im following along with learncpp. And i am a beginner,
So please could i have the simplified versions.
If i create a user function, And try a return the value back it will just output the integer instead of an ASCII character.
Here is my following code.
int ascii(int y)
{
return static_cast<char>(y);
}
int main()
{
std::cout << ascii(5) << std::endl;
return 0;
}
The issue is the type of your return value. It should be a char. Not an int
char ascii(int y)
{
return static_cast<char>(y);
}

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 is my variable returning a value when I haven't set it to any yet? [duplicate]

This question already has answers here:
What happens when I print an uninitialized variable in C++? [duplicate]
(4 answers)
Closed 3 years ago.
I'm trying to learn C++, specifically how to declare and initialize variables. I wrote this code, and I don't know why the variable c is giving a value that I have not assigned it yet.
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!\n";
int a, b;
a = 1;
b = 2;
int d(4);
int result;
auto num = b;
decltype(b) c;
result = a + b - d;
cout << c;
}
The output is -2, but I didn't state c = -2 anywhere!
If you have not initialized the variable, it contains garbage value.
In C/C++, the values declared within a function represent some bytes of main memory on the cpu stack. Those bytes are usually dirty and need initialization. If you don't the values are undefined. That you're always getting '-2' is merely coincidence.

variable initialization with int() [duplicate]

This question already has an answer here:
default constructor for int [duplicate]
(1 answer)
Closed 8 years ago.
I was trying some different ways of initializing variables.
int a(0);
cout<<a;
for this code segment output is 0 .
in another way, I initialize a with 0
int a= int();
cout<<a;
output: 0
then I try this:
int a(int());
cout<<a;
this time output is 1
actually what does the value the int() function return ? 0 or 1
I think that your last attempt (int a(int())) is an example of the "most vexing parse". Thus, a is a function, not an int.
This:
#include <typeinfo>
std::cout << typeid(a).name() << std::endl;
Yields:
FiPFivEE
And putting this result here gives:
int ()(int (*)())
Doing int() creates a temporary integer and value initialize it.Value initialized integers without a specific value will have the value zero. So the declaration
int a = int();
value initializes a temporary integer, and copies it to a.
However for the third example, as many have pointed out, you actually declare a function named a.