Help me fix this

I want to fix this code. I want to know how to use user's input as a condition in the for loop.

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
#include <iostream>
#include <cmath>

int main()
{
    double origin, rate, result;
    int year;

    std::cout << "Compound interest calculator";

    std::cout << "Investment amount:  $";
    std::cin >> origin;

    std::cout << "Annual interest rate:  ";
    std::cin >> rate;

    std::cout << "Number of years:  ";
    std::cin >> year;

    for (int x=1; x<=year; x++;){
        result = pow(1+rate, year);
        std::cout << x << " ----- " << "$" << result;
    }
    return 0;
}
remove the last semicolon in the for loop.
for (int x=1; x<=year; x++;)

should be
for (int x=1; x<=year; x++).
You have an extra semi-colon at line 20:

for (int x=1; x<=year; x++;) should be for (int x=1; x<=year; x++)
thanks!
And how to print out a list that shows every year's interest?
ex.
1----$110
2----$121
3----$133.1
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <cmath>

int main()
{
    double origin, rate, result;
    int year;
    char y;

    std::cout << "Compound interest calculator";

    std::cout << "Investment amount:  $";
    std::cin >> origin;//input origin amount

    std::cout << "Annual interest rate:  ";
    std::cin >> rate;//input rate

    std::cout << "Number of years:  ";
    std::cin >> year;//input number of years

    for (int x=1; x<=year; x++){
        result = origin*pow(1+rate, year);
        std::cout << x << " --- " << year << " ----- " << "$" << result << "\n";
    }//calculation process
    
    std::cout << "Another calculation?  Y/N\n";
    std::cin >> y;//two exit
    
    while(y='y' || 'Y'){
    std::cout << "Investment amount:  $";
    std::cin >> origin;//input origin amount

    std::cout << "Annual interest rate:  ";
    std::cin >> rate;//input rate

    std::cout << "Number of years:  ";
    std::cin >> year;//input number of years

    for (int x=1; x<=year; x++){
        result = origin*pow(1+rate, year);
        std::cout << x << " --- " << year << " ----- " << "$" << result << "\n";
    }//calculation process
    
    std::cout << "Another calculation?  Y/N\n";
    std::cin >> y;//two exit
    
    if (y='n' || 'N'){
        std::cout << "Thank you for using!\n";
        return 0;
        }
    }
    return 0;
}
Last edited on
Topic archived. No new replies allowed.