Question:
Can you use the same variable twice for different values in c++?
?
2013-01-07 22:31:59 UTC
Because I need to make a program for school where I need to re-calculate the interest on something.
If my program looked like the following, provided everything else was there, would it work?

(Saved + (monthlyputaway*12)) + ((Saved + (monthlyputaway*12)*interest)) = newsum;
newsum+ (monthlyputaway*12)*interest = newsum;
newsum + (monthlyputaway*12)*interest = newsum;

Basically, could I take a variable, use it, then reclaim this same variable with a new outcome?

Thanks to anyone who helps me. I desperately need it. /:
Three answers:
?
2013-01-07 22:43:05 UTC
Yes, a variable can have different values as the program runs. That's why they are called 'variables'.



The hardest thing for me to learn in programming was the following:



X = X + 1



This statement freaked me out when I first saw it and it took me a while to figure it out. What it does is take the value of X and increase it by one. In other words, X has a value, then the command is executed and 1 is added to this value and placed in the memory location specified by X. These variables are really places in memory, so we can do whatever we like with these locations, even things that make no sense in your math class. Once I realized how X = X + 1 worked, that was the last time anything was hard for me in programming.
BriteLite
2013-01-08 23:31:15 UTC
This would not compile - you would get errors.



note that in c++



= means is assigned

== means does it equal



if you have a line: x = a + b; the math a + b will be computed and then that result will be assigned to x overwriting whatever used to be in x. so x = x + 1 means: compute x + 1 and then assign that result to x overwriting the previous value.





You need to write: newsum = newsum + monthlyputaway * 12 * interest; //etc

i.e. there is no need to have both a newsum and a saved variable (also if you make Saved have a capital 'S' people will think it is a type not a variable - that's just convention)



For what you are trying to do - computing the future value of an annuity, it is better to use an iterator and put it in a 'for' loop:



#include

#include



static void main() {



double monthlyputaway = 100.0; //the monthly investment

double interest = 0.03; //the annual interest rate

double saved = 0.0; //how much is saved - starts at zero

int periods = 12; // how many periods of time you will save for





//Step 1: Calculate monthly interest.



interest = (1 + interest) ^ (1/12); // note that this is 1 + monthly interest so calling it interest is misleading but convienient.



//Step 2: Compute the savings over the periods:



for (int i = 0; i < periods; i++) saved = saved * interest + monthlyputaway;

//this assumes you earn interest in a month on your minimum monthly balance



cout << "Total savings after " << period << " periods is: $";

cout << setprecision(2) << saved << endl;



}
Don't sue me!
2013-01-08 08:33:42 UTC
Yes, that's the whole point of being variables. Constants are the opposite however; their value must remain the same the whole time the program runs or really bad things will happen.



PS. most languages (all that I know) place the assignee on the left.



Edit: I didn't notice this was about C++.

The assignee must be placed left of the assigned or it won't compile.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...