Error:conversion from 'double' to 'int', possible loss of data

There is an error conversion:
Error message :
warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
warning C4244: '+=' : conversion from 'double' to 'int', possible loss of data
warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
warning C4244: '+=' : conversion from 'double' to 'int', possible loss of


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  #include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
	double a[10];
    int i;
	double const gravity=9.807;
	double const PI = 3.1415927;
	double dev=0,f4=0;
	double f1=2*PI;
	double f2,f3;

	cout<<"Enter the period measured for the following pendulum lengths:"<<endl;
	for(i=2.50; i<=4.75; i+=0.25)
	{
		cout<<fixed<<setprecision(2)<<i<<" m : " ;
		cin>>a[i];
	}
	cout<<left<<setw(10)<<"Length(m)"<<left<<setw(20)<<"Calculated Period(s) "<<left<<setw(20)<<"Measured Periods(s) "<<left<<setw(20)<<"Deviation % "<<endl;

	for(i=2.50; i<=4.75; i+=0.25)
	{
		f2=i/gravity;
		f3=sqrt(f2);
		f4=f1 * f3;
		dev=((f4-a[i])-f4)*0.1;
		cout<<left<<setw(10)<<fixed<<setprecision(2)<<i<<setw(20)<<fixed<<setprecision(4)<<f4<<setw(10)<<fixed<<setprecision(4)<<a[i]<<setw(10)<<fixed
			<<setprecision(4)<<dev<<endl;
	}
	system("Pause");
	return 0;
}
Last edited on
Type double can hold decimal places while int cannot (only whole numbers). You're trying to assign double to int. Which means the decimal places are lost.

Anyways, it's a warning not an error. Declare i as type double. Why were you using an int with floating point values (decimals) in the first place?
Those are not 'error'. They are 'warning'. Syntactically ok, but suspicious. In your code they are logically fatal.

The messages should indicate the line, where the issue occurs.

1
2
3
int foo = 42;
foo = 3.14; // first converts 3.14 into an integer (3)and then assigns result to foo
foo += 0.3; // first converts 0.3 into an integer (0) and then adds result to foo 


As general guideline, prefer integral loop counters. For example:
1
2
3
4
for ( int bar = 10; bar < 20; ++bar ) {
  double gaz = bar * 0.25;
  // use gaz
}



Furthermore: your lines 19 and 28-29:
1
2
3
T array[7];
double odd = 0.42;
std::cin >> array[ odd ]; // error: array indices must be integral 
@boost lexical cast. Thank you for the response. Because i want to show inside the for loop to print out the following :
2.50m : enter value
3.65m : enter value
and... so on till 4.75m.

@keskiverto Thank you for the response. If i wanted to loop with decimal points ? how is the structure should be done.
DesmondLee wrote:
Because i want to show inside the for loop to print out the following :
2.50m : enter value
3.65m : enter value
and... so on till 4.75m.

Yes, but int cannot store "decimal point" values
Last edited on
If i wanted to loop with decimal points ? how is the structure should be done.

Fake it:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <iomanip>

int main()
{
  std::cout << std::fixed << std::setprecision(2);
  for ( int foo=10; foo < 20; ++foo )	{
    std::cout << foo * 0.25 << " m : \n";
  }
  return 0;
}
@boost lexical cast @keskiverto thanks for the response. I managed to solve it. thanks for the advice.
Topic archived. No new replies allowed.