Jump to content
  • Advertisement
Sign in to follow this  
CelticSir

Double Number wont multiply [Solved]

This topic is 2949 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey

Have been learning some basic maths with C++ yet when i have a double number and multiply it - it simply won't multiply it just stays at its set value.

But i didn't set it as a constant so im confused why it won't work... this is my current code:


#include <iostream>

int main(){
double dNumber = 1.5;
dNumber * 5.7;
std::cout << dNumber << std::endl;
return 0;
}


When i display dNumber it gives 1.5 instead of the answer of the multiplication. Im sure i got something wrong but i don't know what it is.

[Edited by - thefollower on August 24, 2010 6:31:28 PM]

Share this post


Link to post
Share on other sites
Advertisement
Oh i see - so how come with basic addition that does not apply such as :


int iNumber = 3;
iNumber + 5;
std::cout << iNumber << std::endl;


That works for me and shows 8.

Share this post


Link to post
Share on other sites
It shouldn't work, it doesn't for me, but this could be a compiler thing, which are you using?

EDIT: Also something like this works as well,

cout << number * 5 << endl;

but ultimatily number will remain what it was originally set to.

Share this post


Link to post
Share on other sites
Quote:
Original post by thefollower
Oh i see - so how come with basic addition that does not apply such as :


int iNumber = 3;
iNumber + 5;
std::cout << iNumber << std::endl;


That works for me and shows 8.


That shouldn't work. What compiler are you using?

I strongly recommend you use iNumber+=5; (or (iNumber=iNumber+5; same thing) otherwise if you switch compiler later your programs will likely not function!

Share this post


Link to post
Share on other sites
This piece of advice may not help you much at this stage, but I'll give it out anyway: Try to never change the value of a variable. Most of the time you can give a variable a good name, initialize it at the time where it's declared and never change its value again. Of course there are things you do in loops where a variable has to change value, but it's a minority of the cases.

So for instance, instead of
  double temperature = get_temperature_in_Fahrenheit();
temperature = (temperature-32.0)/1.8;


I would write
  double temperature_in_Fahrenheit = get_temperature_in_Fahrenheit();
double temperature_in_Celsius = (temperature_in_Fahrenheit-32.0)/1.8;


I am considering making all those local variables `const', but I am hesitant because it's not common in our code base and the verbosity might make code less clear.

Share this post


Link to post
Share on other sites
Quote:
Original post by alvaro
This piece of advice may not help you much at this stage, but I'll give it out anyway: Try to never change the value of a variable.


Why?

Share this post


Link to post
Share on other sites
Quote:
Original post by alvaro
This piece of advice may not help you much at this stage, but I'll give it out anyway: Try to never change the value of a variable.


While I agree that there are many merits to this, I think the advice is too wide-sweeping and not generally accepted to be in place in a For Beginner's forum. Furthermore, in the case of games, variables changing values is the most obvious and straight-forward means to model a simulation over time. Functional programming techniques can apply, but it would be difficult for a beginner to find helpful articles and material in the context of making games (unless you've got some recommendations).

I do quite like your example of not reusing the same variable name for two distinct concepts.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!