'double' to 'float' warnings

I need a some help with my homework.
I can not figure out why I am getting these errors 'double' to 'float' warnings and double to int starting at line 45 (in bold). I just do not see what is wrong.

#include <iostream>
#define _USE_MATH_DEFINES
#include <cmath>
#include "win.h"

void main(void)
{

//int y =175;

AppWindow Win;

system("Pause");
Setup(Win, 0, 0, 600, 600);
SetText(Win, " ");

int x, y, rand;
x = 180;
y = 180;
rand =20;

double velocity = 0.0;
double userVelocity_x = 0.0;
double userVelocity_y = 0.0;
double x_position = 0.0;
double y_position = 0.0;
float gravity =9.8f;
double t = 0.0;
double delta_x = 0.0;
float delta_y = 0.0;
double pi = 3.141592654;

cout << "Please enter your velocity:";
cin >> velocity;
x_position = 180.0;
y_position = 180.0;

double radian = 360* pi/180.0;
double ttt = cos(radian);
ttt = velocity * ttt;
userVelocity_x = velocity * cos(radian);
userVelocity_y = velocity * sin(radian);

Circle(Win, x_position, y_position, 10);

while(t < 10)
{
delta_x = (userVelocity_x * t);
delta_y = (userVelocity_y * t) - (.5 * 9.8 * (t * t));
Circle(Win, userVelocity_x + delta_x, userVelocity_y - delta_y, 10);
t + =.1;
}

system ("Pause");


SetDrawColor(Win, 1.0, 0.0, 0.0);
Line(Win, 0, 200, 200, 200);

SetDrawColor(Win, 1.0, 0.0, 0.0);
Line(Win, 200, 200, 200, 500);

SetDrawColor(Win, 1.0, 0.0, 0.0);
Line(Win, 200, 500, 600, 500);

while (y < 485)
{

SetDrawColor(Win, 0.0, 1.0, 0.0);
y+= 20;
Circle(Win, 180, y, 10);

Sleep (100);

SetDrawColor(Win, 1.0, 1.0, 1.0);
Circle(Win, 180, y, 10);
//Sleep (100);

}

system("Pause");
}
Last edited on
In your variable declarations
1
2
3
4
5
float gravity =9.8f;
double t = 0.0;
double delta_x = 0.0;
float delta_y = 0.0; // Should be type double - not float??
double pi = 3.141592654;
Just like guestgulkan says, when delta_y is initialized the compiler automatically assumes you want 0.0 to be a double and then thinks your trying to put it into a float
Ok, I see what your saying. So how do I fix that? I need to change the float to double then? I tried that. I change float to double went to compile and got this these errors. Why does it ant to change everything to int?

\main.cpp(45) : warning C4244: 'argument' : conversion from 'double' to 'int', possible loss of data
main.cpp(45) : warning C4244: 'argument' : conversion from 'double' to 'int', possible loss of data
main.cpp(45) : warning C4244: 'argument' : conversion from 'double' to 'int', possible loss of data
\main.cpp(51) : warning C4244: 'argument' : conversion from 'double' to 'int', possible loss of data
main.cpp(51) : warning C4244: 'argument' : conversion from 'double' to 'int', possible loss of data
main.cpp(52) : error C2059: syntax error : '='
1>cannonball - 1 error(s), 5 warning(s)

if I do not change my float to double I always get
at line 50 warning C4244: '=' : conversion from 'double' to 'float', possible loss of data.

using the top down method of debugging my problem is at line 45 which is

Circle(Win, x_position, y_position, 10); I tried changing the 10 to 10.0. But I do not understand.
any and all help is welcome
The program is suppose to take the users entered Velocity and shoot a ball
Last edited on
You are getting confused. The compiler does not particularly 'want' to change anything, but sometimes it has to - when it is expecting one type of number and you are giving it another type. It will do the change(if it can) but it will warn you about possible data loss.

This is what it is telling you here.
The type of parameters values required by the Circle function are obviously integers - BUT YOU ARE PASSING IT DOUBLES.
When a double type is forced into an integer type, then the fraction part is lost - which means that data is lost. This is what the compiler warning is all about.

Do you understand the basic differences between, integers and floats and double types?
Last edited on
The problem is the declared types and what you want to do with them, your double userVelocity is being multiplied and crushed into your float delta_y, what you need to do is look over your code and make sure that no types get put into others.
so my error main.cpp(52) : error C2059: syntax error : '=' is caused by above.
No.
You haven't posted your code well - you should use code tags that way we see line numbers:

But I think this is line 52:
t + =.1; //error

The + and = should be next to each other to indicate the addition with assignment operator:
t += .1;
sorry, i do/did not know how to use code tags.

So I ended up static casting where I had my warnings. best thing right?
Last edited on
That would work, but if you aren't planning on doing anything else with the variables, you might as well make them all doubles or floats.

BTW: To use code tags...

[code]
//your code here
[/code]

Would give you:

 
//your code here 
Last edited on
Topic archived. No new replies allowed.