Can't figure out this warning (Resolved)

I've created a program that reduces damage, I want to use it in a game a characters defense. The program works fine, and I've got rid of all but one of the bugs.
Keeps telling me warning C4244: '=' : conversion from 'double' to 'float', possible loss of data (line 60), which I can't understand since they're all floats and none of the data is large enough to have the need to be converted to a double.

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* Simple program to reduce damage taken; represents the defense
   of a character. Reduce by maximum of 25%
   Due to problems arising, broken down into 2 functions */

#include <iostream.h>

// The first function finds out the amount to reduce the damage by.
// The second function reduced the damage done

float amountReduced(unsigned short defense,short damg);
short damageReduced(float toReduce,short dmg);

int main() {

	static short damage = 0;

	short defense = 0;
	short defendedDmg = 0;

	float reduceTemp = 0.0;

	cout << "Enter the damage to character\n";

	// Infinite loop used for testing purposes

	for (;;) {

		cout << "Defense: ";
		cin >> defense;
		cout << "Damage: ";
		cin >> damage;

	   	reduceTemp = amountReduced(defense,damage);
		defendedDmg = damageReduced(reduceTemp,damage);

		cout << "Damage = " << defendedDmg << endl;
	}
	return 0;
}

float amountReduced(unsigned short defense,short damg) {

	float def = (float)defense;

	short dmg = damg;
	float amount = 0.0F;

	amount = ((def/4)/100)*dmg;
	return amount;
}

short damageReduced(float toReduce,short damg) {

	unsigned short newDamage=0;
	float dmg = (float)damg;
	
	float reducedValue = toReduce;
	float fnewDamage = 0.0F;

	fnewDamage = (dmg - reducedValue) + 0.5;;

	newDamage = (int)fnewDamage;

	return newDamage;
}


If you have any ideas why it gives this message I'll be glad to know. It's not causing any ill effects at the moment but I don't wanna encounter errors at a later stage because of this.
Thanks for your help really appreciated.
Last edited on
Line 60 - you have two semicolons
Line 26 - wtf? while(true) is a much better option and looks much better then that for loop
Line 55/57 - you know you can just use the values that were passed to the function...they are local to the function so it won't edit the values after you leave the function...just put the (float)damg instead of dmg, it will be easier to follow then storing it to another value

About the warning, I don't know exactly, but I'm guessing it's because the 0.5 is created as a double, not a float.
Last edited on
That is wounderful!!! Your a real genius!!!
Line 60 -- two semicolons == silly != fatal
That lone "0.5" appears to be automatically considered a double --which is also my best guess as to that...

Line 26 --that is the quintessential C and C++ idiom for a forever loop.

while (1)
9 characters, plus it looks normal
for (;;)
8 characters, plus it looks pleasing to C-hackers
C hackers and old-time BCPL folk love playing with all the extra keys on their keyboard and the sense of power to obfuscate stuff, but the real pleasure is that the idiom is transparent to anyone who has used C while simultaneously bizarre to everyone else.

If that gets your juices going, take a look through the following (fun) links:
http://www.catb.org/~esr/jargon/html/S/space-cadet-keyboard.html
http://www.catb.org/~esr/jargon/html/O/Obfuscated-C-Contest.html
http://en.wikipedia.org/wiki/Obfuscated_code

:-)
Last edited on
Duoas,
while (0) 9 characters, plus it looks normal
Shouldn't it be while(1)?
Last edited on
I'm just kind of confused how in the world this reduces damage. I mean obviously this can't be the whole program if it actually works, unless this is programmed IN the actual game? Because if your trying to reduce damage on said game you would need to declare the offset for damage, but if this is the coding IN the actual game code then I can see how it would work but don't know why it would be in your main function.
I suppose he meant to try out the algorithm without changing the actual code. When a project starts getting larger you'll want to only add code that you fully understand or that's already been tested. This reduces the possible errors in the final version and you can know which parts may be causing trouble.
Re: QWERTYman Yoinks! Fixed.
Wow, yea... um I apologise for the infinite loop. I am deeply sorry to anyone I offended but I learnt it from a book and it seemed to work fine but I've changed the program and there are absolutely no more errors. THANKS!

Just outta curiosity though, wtf were those links? I was kinda baffled... does my code really look that bad and why does it look appealing to C-hackers?

What do you mean by the 8 and 9 character thing?

Yea helios is right about TheNoobie question.
Last edited on
Topic archived. No new replies allowed.