I am learning C++ in 11th standard. I don’t have much knowledge about C++
In my college, there is visual studio 2005. I have a project to convert celsius to farenheit. This code works fine in my college in visual studio 2005:
#include <iostream>
int main()
{
float f,cs;
cin>>cs;
cout<<"\n celsius = "<<cs;
f=1.8*cs+32.0;
cout<<"f = "<<f;
}
But when I run this code in visual studio 2012 after modifying:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
float f,cs;
cin>>cs;
cout<<"\n cs = "<<cs;
f=1.8*cs+32.0;
cout<<"f = "<<f;
}
It gives me following errors:
conversion from double to float possible
You are getting a warning, as you multiply 1.8 (which is a
doubleliteral) withcs, yielding adouble. In C++, if you have an expression with two different types, a type promotion is performed to the type with the largest value range; fordoubleandfloatthis means thatcsis converted todouble. This is then assigned to afloat, which might not be able to represent the full value range ofdouble(floatis a 32-bit IEEE floating point number,doubleis 64-bit.) Hence the warning.You can fix this by writing:
or by changing
fto be of typedouble.Note: Your code is compiling, this is just a warning, not an error.