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()
{
usingnamespace 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 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. ";