Having (mulitple) issues with my code

Full disclosure: this is a homework project, so please do not give me complete answers on how to fix something, however please tell me whats wrong and hint at a fix please. Thank you all in advance for giving this a once over, I really appreciate it.
I am having major issues with vectors and getting the data to be displayed correctly. The top row of the table is supposed to be the monthly rate (starting at 3% increasing by 0.5% to 5.5%), and the row headings the years (starting at 15 ending at 30, increasing by 5year increments). However my program just displays the loan amount and nothing else. Any hints/help you could give would be greatly appreciated, because at the moment I am completely stuck.



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
54
55
56
57
58
59
60
61
62
63
64
65
66
 using namespace std;
#include <cmath>
#include <iostream>
#include <iomanip>

int main(){
    
    //Heading and getting data from user
    cout << "Monthly Payment Calculator" << endl;
    cout << endl;
    
    //Variables and calculations
    double monthlyPayment;
    double monthlyRate;
    int months = 12;
    int loanAmount = 0.0;
    char answer = 'y';
    monthlyRate = 0.03;
   
    //Body Loop
    while (answer == 'y' || 'Y'){
        cout << "Please enter a valid loan amount (between 1000 - 500000): ";
        cin >> loanAmount;
        //Input Validation
        if (loanAmount < 1000 || loanAmount > 500000){
            cout << "Please enter a valid amount, between 1000 and 500000";
            cin >> loanAmount;
            cout << endl;
        }
        cout << "" << loanAmount;
        //Calculating results
        monthlyPayment = loanAmount * monthlyRate / (1 - 1 / pow((1 + monthlyRate), months));

        int grid [4][6] = {0};
        
        for (int iCol = 3; iCol < 6; iCol += 0.5){
            for (int iRow = 15; iRow < 33; iRow +=5 ){
                grid[iRow][iCol] = monthlyPayment;
            }
        }
        
        //displaying data
        cout << "      ";
        for( int iCol = 3; iCol < 6; iCol += 0.5){
            cout << setw(5) << iCol << "   ";
        }
        cout << endl;
        for(int iRow = 15; iRow < 33; iRow +=5 ){
            cout << setw(3) << iRow << "  ";
            for (int iCol = 3; iCol < 6; iCol += 0.5){
                cout << setw(8) << grid[iRow][iCol];
            }
            cout << endl;
        }
        
        //Asking user if they would like to run again
        cout << "Would you like to enter in another amount?: ";
        cin >> answer;
        if (answer == 'n' || 'N'){
            cout << "Bye" << endl;
            break;
        }
        break;
    }

}
If this helps any, this is what my program should look like. Sorry for bad formatting, the forum won't keep the original (underscores are spaces).

Monthly Payment Calculator

Loan amount: 500000.00

Year _ 3.0% _ 3.5% ____ 4.0% ___ 4.5% ___ 5.0% _____ 5.5%
15 _ 3452.91 _ 3574.41 _ 3698.44 _ 3824.97 _ 3953.97 _ 4085.42
20 _ 2772.99 _ 2899.80 _ 3029.90 _ 3163.25 _ 3299.78 _ 3439.44
25 _ 2371.06 _ 2503.12 _ 2639.18 _ 2779.16 _ 2922.95 _ 3070.44
30 _ 2108.02 _ 2245.22 _ 2387.08 _ 2533.43 _ 2684.11 _ 2838.95

Loan amount: 0

Bye!
Last edited on
>while (answer == 'y' || 'Y')
You wrote
while (answer == 'y' || 'Y' != 0)

1
2
3
4
5
6
7
        int grid [4][6] = {0};
        
        for (int iCol = 3; iCol < 6; iCol += 0.5){
            for (int iRow = 15; iRow < 33; iRow +=5 ){
                grid[iRow][iCol] = monthlyPayment;
            }
        }

You might be going round the loops 4 and 6 times respectively, but those aren't the indices you're writing to.
You're trashing memory way outside your array.

> for (int iCol = 3; iCol < 6; iCol += 0.5)
Adding .5 to an int is likely to leave it unmoved.
3 + 0.5 is still 3 (as an integer)

> grid[iRow][iCol] = monthlyPayment;
You probably want some variable result, say
monthlyPayment = loanAmount * monthlyRate / (1 - 1 / pow((1 + monthlyRate), months));

Not fill the array with the same answer.
Furthermore, your code:
1
2
3
4
5
double monthlyRate; // uninitialized variable; value is unknown

// monthlyRate is not touched

monthlyPayment = loanAmount * monthlyRate / (1 - 1 / pow((1 + monthlyRate), months));

You can't use montlyRate's value before you set it. salem c hinted that it might be different on each column.


A detail, not an error, is that C/C++ are row-major. That is, if there is 2D array:
abc
def

then it is actually in continuous block of memory:
abcdef

A column-major language would put data:
adbecf

On large arrays in some hardware there can be performance difference between:
1
2
3
4
5
        for (int iCol = 3; iCol < 6; iCol += 0.5){
            for (int iRow = 15; iRow < 33; iRow +=5 ){
                grid[iRow][iCol] = monthlyPayment;
            }
        }

and
1
2
3
4
5
        for (int iRow = 15; iRow < 33; iRow +=5 ){
            for (int iCol = 3; iCol < 6; iCol += 0.5){
                grid[iRow][iCol] = monthlyPayment;
            }
        }

But as already said, indices are calculated wrong in these.
Topic archived. No new replies allowed.