confusion with loop

I am new to programming and am trying to write a simple program that calculates the number of days it takes for a salary to earn 100,000 dollars. The pay rate doubles each day and is 0.01 the first day and 0.02 the second, and 0.04 the next..and so on. I was able to get the loop to 100,000, but i do not know how to keep up with how many times it took to reach >= 100000. any help would be great!
This is pretty easy solution. You've already done all of the hard work. You just need to declare a counter variable. You'll want to initialize this to 0, and place at the end of your loop. You could do something such as...

1
2
3
4
5
counter = counter + 1;

//or

counter++;


Every pass through the loop will increment the value of counter by 1 every time. When you've reached your desired amount, you can then create an output using this variable.

UAT Student
Last edited on
I'm not so sure on the algorithm of your program but I tried it anyway.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
int main()
{
    double r=0.01,total,salary,x,y=0;

    cout<< "Enter the salary: \n";
    cin>> salary;
    total = 0;
    for (x=1;total<100000;x++)
    {
        r=r*2;
        total= total + (salary * r);
        y++;
    }

    cout<< "Total salary = "<<total <<endl;
    cout<< "Days: "<<y<<endl;
    return 0;
}

Last edited on
Topic archived. No new replies allowed.