Help fixing program

How would I fix this program to make it run correctly? I know I would need to add || where the if statement is located but I'm confused because I get an error stating "otherwise" does not get a type.


// file fixit1.cpp
#include <iostream>
#include <iomanip>
#include <math.h>
#include <string>
using namespace std;

int main(void){
int n = 30 * 12; // totalNumberOfPayments;
double yrp, P, mrd, numerator, denominator, answer, estimate;

cout << "Enter the yearly interest rate. Ex: for 4.5% enter 4.5 ";
cin >> yrp;

cout << "Enter the loan amount: Ex: 500000 ";
cin >> P;

cout << "Enter the monthly payment you think you can make Ex: 1000 ";
cin >> estimate;

mrd = (yrp / 12) / 100.0;
numerator = P*mrd;
denominator = 1.0 - (1.0 / pow(1.0 + mrd, double(n)));
answer = numerator / denominator;

if (answer >= estimate - 250 ! answer <= estimate + 250)
cout << "The monthly payment of $ " << fixed << setprecision(2) << answer;
cout << " may be a tight squeeze\n";
}
else if (answer > estimate - 250)
cout << "You can easily afford the monthly payment of $"
<< fixed << setprecision(2) << answer << endl;
otherwise
cout << "You probably cannot afford the monthly payment of $"
<< fixed << setprecision(2) << answer << endl;

return 0;
}
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 <iomanip>
#include <math.h>
#include <string>
using namespace std;

int main(void){
int n = 30 * 12; // totalNumberOfPayments;
double yrp, P, mrd, numerator, denominator, answer, estimate;

cout << "Enter the yearly interest rate. Ex: for 4.5% enter 4.5 ";
cin >> yrp;

cout << "Enter the loan amount: Ex: 500000 ";
cin >> P;

cout << "Enter the monthly payment you think you can make Ex: 1000 ";
cin >> estimate;

mrd = (yrp / 12) / 100.0;
numerator = P*mrd;
denominator = 1.0 - (1.0 / pow(1.0 + mrd, double(n)));
answer = numerator / denominator;

if (answer >= estimate - 250 || answer <= estimate + 250) // <-- THIS
cout << "The monthly payment of $ " << fixed << setprecision(2) << answer;
cout << " may be a tight squeeze\n";
}
else if (answer > estimate - 250)
cout << "You can easily afford the monthly payment of $"
<< fixed << setprecision(2) << answer << endl;
otherwise
cout << "You probably cannot afford the monthly payment of $"
<< fixed << setprecision(2) << answer << endl;

return 0;
}
Below is the code you posted.
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 <iomanip>
#include <math.h>
#include <string>
using namespace std;

int main(void){
    int n = 30 * 12; // totalNumberOfPayments;
    double yrp, P, mrd, numerator, denominator, answer, estimate;

    cout << "Enter the yearly interest rate. Ex: for 4.5% enter 4.5 ";
    cin >> yrp;

    cout << "Enter the loan amount: Ex: 500000 ";
    cin >> P;

    cout << "Enter the monthly payment you think you can make Ex: 1000 ";
    cin >> estimate;

    mrd = (yrp / 12) / 100.0;
    numerator = P*mrd;
    denominator = 1.0 - (1.0 / pow(1.0 + mrd, double(n)));
    answer = numerator / denominator;

    if (answer >= estimate - 250 !answer <= estimate + 250)
	   cout << "The monthly payment of $ " << fixed << setprecision(2) << answer;
    cout << " may be a tight squeeze\n";
}
else if (answer > estimate - 250)
cout << "You can easily afford the monthly payment of $"
<< fixed << setprecision(2) << answer << endl;
otherwise
cout << "You probably cannot afford the monthly payment of $"
<< fixed << setprecision(2) << answer << endl;

return 0;
}

The following are the syntax error:
Error 1 error C2059: syntax error : 'else' Line: 29
Error 2 error C2146: syntax error : missing ';' before identifier 'cout' Line:33
Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int Line:33
Error 4 error C2143: syntax error : missing ';' before '<<' Line:33
Error 5 error C2059: syntax error : 'return' Line: 36
Error 6 error C2059: syntax error : '}' Line: 37
Error 7 error C2143: syntax error : missing ';' before '}' Line: 37

Some notes...
On line 25, you are already tracking what you need.
Take a look at line 28. The closing bracket that you placed there, it is for the main function.
Therefore, you would get a syntax error: 'else'
Take a look at line 32. Is 'otherwise' a variable? If so, what is the type(i.e. int, double char...)?
Take a look at line 37. You have a closing bracket without an opening bracket.

Below is an example code of what I mean by fixing those syntax errors.
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 <iomanip>
#include <math.h>
#include <string>
using namespace std;

int main () /*(void)*/{ // http://en.cppreference.com/w/cpp/language/main_function
    int n = 30 * 12; // totalNumberOfPayments;
    double yrp, P, mrd, numerator, denominator, answer, estimate;

    cout << "Enter the yearly interest rate. Ex: for 4.5% enter 4.5 ";
    cin >> yrp;

    cout << "Enter the loan amount: Ex: 500000 ";
    cin >> P;

    cout << "Enter the monthly payment you think you can make Ex: 1000 ";
    cin >> estimate;

    mrd = (yrp / 12) / 100.0;
    numerator = P*mrd;
    denominator = 1.0 - (1.0 / pow(1.0 + mrd, double(n)));
    answer = numerator / denominator;

    if (answer >= estimate - 250 || answer <= estimate + 250){// You sre tracking. Also, add an opening bracket.
	   cout << "The monthly payment of $ " << fixed << setprecision(2) << answer;
    cout << " may be a tight squeeze\n";
}
else if (answer > estimate - 250)
cout << "You can easily afford the monthly payment of $"
<< fixed << setprecision(2) << answer << endl;
//otherwise . No clue why is otherwise is here.
cout << "You probably cannot afford the monthly payment of $"
<< fixed << setprecision(2) << answer << endl;

return 0;
}
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
#include <iostream>
#include <iomanip>
#include <cmath> // <math.h>

int main()
{
    // using names which carry meaning makes it easier to read the code
    const int number_of_months = 30 * 12; // totalNumberOfloan_amountayments;

    // postpone declaring other variables till thay are required
    double yearly_rate, loan_amount, estimate ;

    std::cout << "Enter the yearly interest rate. Ex: for 4.5% enter 4.5 ";
    std::cin >> yearly_rate;

    std::cout << "Enter the loan amount: Ex: 500000 ";
    std::cin >> loan_amount;

    std::cout << "Enter the monthly payment you think you can make Ex: 1000 ";
    std::cin >> estimate;

    // good idea to pro-actively use const (we don't intend to modify this, ever).
    const double monthly_rate = (yearly_rate / 12) / 100.0;
    std::cout << std::fixed << std::setprecision(2)
              << "the monthly interest rate is " << monthly_rate*100 << "%\n" ;

    const double numerator = loan_amount*monthly_rate;
    const double denominator = 1.0 - (1.0 / std::pow( 1.0 + monthly_rate, number_of_months ) );
    const double monthly_payment = numerator / denominator;
    std::cout << "expected monthly payment for " << number_of_months
              << " months is $" << monthly_payment << '\n' ;

    const double difference = estimate - monthly_payment ;

    // if the difference is within + or - $250
    if( std::abs(difference) <= 250 ) // simpler to use std::abs for this
    {
        std::cout << "The monthly payment of $ "  << monthly_payment
                  << " may be a tight squeeze\n";
    }

    // otherwise, if the estimate is more than $250 above the monthly payment
    else if( difference > 250 )
    {
        std::cout << "You can easily afford the monthly payment of $"
                  << monthly_payment << '\n' ;
    }

    else // otherwise (if the estimate is more than $250 below the monthly payment)
    {
        std::cout << "You probably cannot afford the monthly payment of $"
                  << monthly_payment << '\n' ;
    }
}
Topic archived. No new replies allowed.