Getting warnings in my C++ variables

Trying to understand my warnings on 'double' to 'const int' possible loss of data and truncation from 'double' to 'float'.

Not sure where I went wrong.........Need help understanding.

Warning 1 warning C4244: 'initializing' : conversion from 'double' to 'const int', possible loss of data
Warning 2 warning C4305: 'initializing' : truncation from 'double' to 'float'

This is what I have for my variables:

//input variables

float SLICES = 0.0;
int const PI = 3.14;
float SLICE_AREA = 14.13;
float radius;

Please help me understand where I went wrong. I am a beginner.
Truncation is when the fractional part of a floating-point literal/value is removed so it's compatible with a integral type.

Edit:

Here's what's happening: The int type can only hold whole numbers. The float/double types can hold whole & fractional numbers (numbers such as 4.3). When a floating point number (a number with a fractional counterpart) is assigned to a variable of type int, the compiler will notice and truncate the fractional counterpart. Here's an example:

1
2
3
4
int Integer(0);
float Real(1.2F); // The 'F' denotes the literal as a 4-byte float.

Integer = Real; // Truncation here. 

In the above code, when Integer is assigned to Real, the .2 of Real's value is removed (truncation). The resulting value is compatible with int.

Wazzak
Last edited on
In other words, you just defined PI to be equal to 3.
can we declare double and float at the same time?
guys need help in running this code.dont know why it is getting struck??
//program to insert data in a sorted file.....
#include<fstream.h>
#include<stdio.h>
#include<conio.h>

class stu
{ int rollno;
char name[20];
int Class;
float marks;
char grade;

public:
void getdata()
{ cout<<"rollno:";
cin>>rollno;
cout<<"name:";
cin>>name;
cout<<"Class:";
cin>>Class;
cout<<"marks:";
cin>>marks;
if(marks>=75) grade='A';
else if(marks>=60) grade='B';
else if(marks>=50) grade='C';
else if(marks>=40) grade='D';
else grade='E';
}

void putdata()
{ cout<<"rollno"<<rollno<<"\t name:"<<name
<<"\n marks:"<<marks<<"\t Grade:"<<grade<<endl;
}
int getrno()
{return rollno;}

}s1,stud;


void main()
{ clrscr();
ifstream fi("stu.dat",ios::in);
ofstream fo("temp.dat",ios::out);
char last ='y';
cout<<"enter details of student whose record is to be inserted\n";
s1.getdata();
while(!fi.eof())
{ fi.read((char*)&stud,sizeof(stud));
if(s1.getrno()<=stud.getrno())
{fo.write((char*)&s1,sizeof(s1)) ;
last='n';
break;
}
else
fo.write((char*)&stud,sizeof(stud));
}

if(last=='y')
fo.write((char*)&s1,sizeof(s1));

else if(!fi.eof())
{
while(!fi.eof())
{ fi.read((char*)&stud,sizeof(stud));
fo.write((char*)&stud,sizeof(stud));
}
}


fi.close();
fo.close();
remove("stu.dat");
rename("temp.dat","stu.dat");
fi.open("stu.dat",ios::in);
cout<<"file now contains\n";
while(!fi.eof())
{fi.read((char*)&stud,sizeof(stud));
if(fi.eof())break;
stud.putdata();
}

fi.close();
getch();
}
Start a new thread with your question, use [code] tags, and get a compiler that's not 15 years old.
Topic archived. No new replies allowed.