Public Group

# Double Number wont multiply [Solved]

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

## 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 on other sites
you need to do something like this,

doublenumber = doublenumber * whatever
or
doublenumber *= whatever

##### Share on other sites
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 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 on other sites
Okay ill have a bit more fiddling with it see what works and doesn't work.

Thanks

##### Share on other sites
Moved to the For Beginners forum.

##### Share on other sites
Quote:
 Original post by thefollowerOh 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 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.

  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 on other sites
Quote:
 Original post by alvaroThis 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 on other sites
Quote:
 Original post by alvaroThis 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.

1. 1
2. 2
3. 3
Rutin
13
4. 4
5. 5

• 26
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633696
• Total Posts
3013378
×