mortgage calculator

I'm trying to make a program that calculates a mortgage using user defined integers, but I keep on getting compiler issues. Here is my code:

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
#include <fstream> 
#include <iostream> 
#include <string> 
using namespace std; 
#include <iomanip>
#include <cmath>

int main() 
{ 

  int years = 30; 

  cout << endl << endl;
  double p;
  cout << "Enter Mortgage amount: ";
  cin >> p;
  cin.ignore(1000, 10);
  getline(cin, p);

  cout << endl << endl;
  int i;
  cout << "Enter in interest percentage: ";
  cin >> i;
  cin.ignore(1000, 10);


  float r = (0.01 * i) / 12; 
  float n = 30 * 12;
  int T = years * 12; 
  double S = p * ((pow(1 + r,n)) * r) / ((pow(1 + r,n)) - 1);

  // formatting output (see 4.2) 
  fout.setf(ios::fixed|ios::showpoint); 
  fout << setprecision(0); 

 

  
  // write the student's mort to classRoster.txt 
  ofstream fout; 
  fout.open("mortgagecalc.txt", ios::app); 
  if (!fout.good()) throw "I/O error"; 
  fout << endl << "Over the course of " << years << " years, the payments for a $"; 
  fout << p << " mortgage will be $"; 
  fout << setprecision(2) << S << " per month." << endl;  
  fout.close(); 
  
  return 0; 
}
closed account (o3hC5Di1)
Hi there,

Could you please post the compiler errors too?
That would help us identify the problem much more easily.

Thanks!

All the best,
NwN
I think you need to at least put using namespace std; below all of your includes. Could be wrong about that one.

cin.ignore() is a little strange, what is 10?
Oh, just use '\n'

throw looks odd. Replace that with return 1;
Last edited on
Also, "ofstream fout;" needs to be placed before the two preceding lines which access it.

After all this, using MinGW, I still get an error:

cmortgage.cpp:18: error: no matching function for call to `getline(std::istream&, double&)'

I don't know how to resolve that...
Doing what Gorlash said and then erasing getline(cin, p); solved all compiler problems for me...

I erased it because I didn't understand why you use it
Topic archived. No new replies allowed.