Adding the results of an incremented value

The point of this program is to calculate how many cents the user would have after a certain number of days. The user is given .01 cent on day one, and this amount is doubled with each passing day. I cannot seem to figure out how to add all of the results. Currently, my program simply outputs them to the screen. How do I tell it to add all of the results and store the sum in one variable?

#include <iostream>
#include <cmath>
#include <iomanip>

int main()

{
using namespace std;

float totalF = 0;//total amount earned after number of days specified by user have passed
float sum;
int days;

cout << "Enter a number of days between 1 and 30.\n\n";
cin >> days;

//in case user enters an invalid number
while(days < 1 || days > 30)
{
cout << "Please enter a number between 1 and 30.\n\n";
cin >> days;
}

for (float time = 0; time >= 0 && time <=days; time++)
{
sum = .01*2* pow (2, time);//doubles .01 every day
cout << sum << " ";
}

cout << "After " << days << "days, you will have " << sum << "dollars and cents. ";
cin >> days;

return 0;
}

pardon the formatting. it is still rough.
Thanks!
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
#include <iostream>
#include <cmath>
#include <iomanip>

int main()

{
	using namespace std;
	
	float totalF = 0;//total amount earned after number of days specified by user have passed
	float sum;
	float all_result_in_one_variable = 0;
	int days;
	
	cout << "Enter a number of days between 1 and 30.\n\n";
	cin >> days;
	
	//in case user enters an invalid number
	while(days < 1 || days > 30)
	{
		cout << "Please enter a number between 1 and 30.\n\n";
		cin >> days;
	}
	
	for (float time = 0; time >= 0 && time <=days - 1; time++)//***HERE WAS A Problem as well
	{
		sum = .01*2* pow (2, time);//doubles .01 every day
		all_result_in_one_variable+=sum;
		cout << sum << " ";
	}
	
	cout << "After " << days << "days, you will have " << sum << "dollars and cents. ";
	cout << "all_result_in_one_variable: " << all_result_in_one_variable << endl;
	cin >> days;
	
	return 0;
}
This code is incorrect. I really do not understand what is being added to the variable "all_results_in_one_variable," but it is not all of the results of the equation

sum = .01*2* pow (2, time)

from 0 to the user specified number of days. I did try this before. I suppose the point at which my understanding fails is when attempting to figure out exactly what is contained in the variable "sum" at this point in the program:

sum = .01*2* pow (2, time);//doubles .01 every day
all_result_in_one_variable+=sum;

What value is being added to "all_results_in_one_variable"?
I think this is the crux of the calculation
1
2
for(int i=0;i<days-1;i++)
 val*=2;

//for 7 days prints 6.4.
how many cents the user would have after a certain number of days = all_results_in_one_variable

sum has the amount of one day to be added in the previous result of total cents user has one day before it.

For day 1: sum = 0.01
For day 2: sum = 0.02
For day 3: sum = 0.04
I was expecting you to correct logical errors in your code.

Error corrected: sum = .01* pow (2, time);//doubles .01 every day
Error corrected: cout << "After " << days << "days, you will have " << all_result_in_one_variable << "dollars and cents. ";
ah. I see...
why is it that my equation:

sum = .01*2* pow (2, time)

outputs the correct incremented numbers, despite my Math error?
It's not outputing corrent incremented number.

It should be giving:
For day 1: sum = 0.01
For day 2: sum = 0.02
For day 3: sum = 0.04

where as it gives:
For day 1: sum = 0.02
For day 2: sum = 0.04
For day 3: sum = 0.08
Oh okay, this makes sense. the 2 makes every day off. interesting. Thanks for your help.
You're welcome!
Topic archived. No new replies allowed.