I am having a problem writing from a outside file. here is the assignment.

Write the program as an object-oriented C++ program that allows the user to select which way they want to calculate a mortgage: by input of the amount of the mortgage, the term of the mortgage, and the interest rate of the mortgage payment or by input of the amount of a mortgage and then select from a menu of mortgage loans: 7 year at 5.35%, 15 year at 5.5%, and 30 year at 5.75% (use an array for the mortgage information and read the interest rates to fill the array from a sequential file). In either case, display the mortgage payment amount. Then, list the loan balance and interest paid for each payment over the term of the loan. On longer term loans, the
list will scroll off the screen. Do not allow the list to scroll off the screen, but rather display a partial list and then allow the user to continue the list. Allow the user to loop back and enter a new amount and make a new selection, or quit. Insert comments in the program to document the program.

I am not sure if i am doing it right, of if i am just way off. Can someone help me?

Here is my main file:

//Jeremy Johnson
//PRG 411
//Instructor: Charles Ford

#include "mortgageCalc_JeremyJohnson.h"
#include <math.h>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main () {
	MortgagePayment totalMort;
	
	
		
	
	double interestRate[]= {5.35, 5.5, 5.75,};//*Annual interest rate
	double years[]= {7, 15, 30,}; //*Term of the loan
	double rate = 0;// Rates
	double term = 0;// Term
	double interestPaid; //Interest paid out
	double balance; //total Loan Balance
	double paidOut; //Amount Paid on Loan
	double NumOfPymts; //total Number of Payments
	int Monthly; //*Allows loop for loan balance and interest paid
	int listScroll = 0; //*Scroll List for loan balance and interest paid
	char choice; //User chooses to enter Int and term or select scenario
	
	bool doneEndProg = false;
	
	do {
		totalMort.openingHead();
		totalMort.enterPrincipal();
		
		cout<< "Would you like to me to provide options, or would you like to make the choices yourself??" << endl;
		cout<< "Press 1 for me to provide options, or 2 to make your own choices" <<endl;
		cin >> choice;
		
		
		
		
		if (choice == '1') {
			/*
			cout << "Your Terms and Rates are as followed.  Please make a choice: " << endl;
			cout << "1. 7 years at 5.35%" << endl;
			cout << "2. 15 years at 5.5%" << endl;
			cout << "3. 30 years at 5.75%" << endl;
			cout << "What is your selection:" << endl;
			*/
			
			int iarray[3] = {0};
			float farray[3] = {0.0F};
			
			ifstream in("cr15.txt");
			in >> iarray[0] >> iarray[1] >> iarray[2];
			in >> farray[0] >> farray[1] >> farray[2];
			// do the same with the float array			
			
			int select;
			cin >> select;
			
			switch (select) {
				case 1:
					cout << "You have selected 7 Years and 5.35%" << endl; 
					break;
				case 2: 
					cout << "You have selected 15 Years at 5.50%" << endl; 
					break;
				case 3:
					cout << "You have selected 30 Years at 5.75%" << endl; 
					break;
				default: 
					cout << "Invalid choise.  You have chosen poorly..." << endl;
					cout << "Please correct your choice" <<endl;
					cin >> select;
					return 0;
			}
			
			rate = interestRate[select - 1];
			term = years[select - 1];
		}
		else if (choice == '2') {
			cout << "Enter your desired rate: ";
			cin >> rate;
			
			cout << "Enter your desired term(in years): ";
			cin >> term;
		}
		
		//calculate monthly payment
		double tPayment = (totalMort.principal * ((rate/1200)/(1 - pow((1+(rate/1200)),-1*(term*12)))));
		
		cout << "These were your choices:" << endl;
		cout << "Interest Rate: "<< rate << "\n";
		cout << "Term: " << term << " Years\n" << endl;
		//Ouput payment
		cout << "Based on your answers above, here are the results of your answers: \n" << endl;
		cout << "Your Total Monthly Payment is: $" << tPayment << "\n" << endl;
		
		//Number of Payments, 
		NumOfPymts = term * 12;
		listScroll = 0;
		
		for (Monthly = 1; Monthly <= NumOfPymts && toupper(choice) != 'D'; ++Monthly)	{
		
			interestPaid = totalMort.principal * (rate / 1200);
			paidOut = tPayment - interestPaid;
			balance = totalMort.principal - paidOut;
			
			if (balance < 0) {
				balance = 0;
			}
			
			totalMort.principal = balance;
			
			//This will Scroll and seperate the loan balance and Interest paid
			if (listScroll == 0) {
				cout << "Balance" << "\t\t\tTotal Interest Paid" << endl;
			}
			
			cout << setprecision(2) << fixed << "$" << setw(5) << balance << "\t\t\t\t$"<< setw(5) << interestPaid << endl;
			++listScroll;
			
			/*Allows the user to enter a value to see the remaining information after 12 lines, 
			 to clear the list and enter new values, or to quit.
			 */
			
			if (listScroll == 12) {
				cout << "Would you like to play again?\n" << endl;
				cout << "Enter 'S' to scroll through the list, Enter 'C' to continue and try again, Enter 'E' to quit and be done.\n" << endl;
				cin >> choice;
				
				if (choice == 'S' || choice == 's')	{
					listScroll = 0;
				}
				else {
					if (choice == 'C' || choice == 'c')	{
						break;
					}
					else if (choice == 'E' || choice == 'e') {
						doneEndProg = true;
					}
				}
			}
			
		}
	}
	while ( choice != 'E' && choice != 'e' );
	
	return 0;
}

Here is my other file that is part of the assignment:

//Jeremy Johnson
//PRG 411
//Instructor: Charles Ford



#include "mortgageCalc_JeremyJohnson.h"
#include <math.h>
#include <iostream>
#include <iomanip>
using namespace std;

void MortgagePayment::openingHead() //This is an opening for the program....nothing more...
{
	cout <<"\n\t\t Jeremy Johnson's Mortgage Calculator\n\n";
	cout << "\tWrite the program as an object-oriented C++\n";
	cout <<"\tprogram that allows the user to select which way\n";
	cout << "\tthey want to calculate a mortgage: by input of the amount\n" ;
	cout << "\tof the mortgage, the term of the mortgage, and the interest\n" ; 
	cout << "\trate of the mortgage payment or by input of the amount of a\n";
	cout << "\tmortgage and then select from a menu of mortgage loans:\n";
	cout << "\t\t- 7 year at 5.35%\n";
	cout << "\t\t- 15 year at 5.5%\n";
	cout << "\t\t- 30 year at 5.75%.\n";
	cout << "\tIn either case, display the mortgage payment amount. Then, list\n"; 
	cout << "\tthe loan balance and interest paid for each payment over the\n" ;
	cout << "\tterm of the loan. On longer term loans, the list will scroll off\n"; 
	cout << "\tthe screen. Do not allow the list to scroll off the screen, but\n" ;
	cout << "\trather display a partial list and then allow the user to continue\n"; 
	cout << "\tthe list. Allow the user to loop back and enter a new amount and\n" ;
	cout << "\tmake a new selection, or quit. Insert comments in the program to\n" ;
	cout << "\tdocument the program.";
	cout <<"\n____________________________________________________________________\n\n " <<endl;
}

void MortgagePayment::enterPrincipal() //This is the function for the user input of loan amount  
{
	cout << "\t\tEnter the total amount of the loan:$ "; //User input
	cin >> this->principal;
}

here is the header file

//Jeremy Johnson
//PRG 411
//Instructor: Charles Ford


class MortgagePayment { 
public: 
	void openingHead(); //Introduction to the program
	void enterPrincipal(); //This is where the principle is entered
	double principal; //principle
	double interest; //Interest
	int term; //Term
};

Here is the cr15.txt

7 5.35
15 5.5
30 5.75

Thank you everyone!! Any help from your guys would be great.

>>I am not sure if i am doing it right, of if i am just way off. Can someone help me?

Didn't you run the program? If not, why not ? Don't ask us if your program will work or not -- you can find that out by yourself by simply running it on your computer.

I am sorry about that. I did run the program. and it is not working. I should have said that i am not sure why it is not working. Can you help with that? I am not sure how to read in a txt file.

Thank you and sorry again.

What exactly do you mean by "it is not working"? Do you take your broken car to the auto repair shop and tell them that "it is broken -- please fix it"? (trying to get you to learn how to fix your own problems). Start out by learning how to use your compiler's debugger.

When i choose the option 1 to get interest rate and years, nothing comes up...it is blank. Then (with it blank) i can enter one of the options, then i gives me the answer. So i dont think that it is reading the .txt correctly.

When i choose the option 1 to get interest rate and years, nothing comes up...it is blank. Then (with it blank) i can enter one of the options, then i gives me the answer. So i dont think that it is reading the .txt correctly.

    cout<< "Would you like to me to provide options, or would you like to make the choices yourself??" << endl;
    cout<< "Press 1 for me to provide options, or 2 to make your own choices" <<endl;
    cin >> choice;




    if (choice == '1') {
        /*
        cout << "Your Terms and Rates are as followed.  Please make a choice: " << endl;
        cout << "1. 7 years at 5.35%" << endl;
        cout << "2. 15 years at 5.5%" << endl;
        cout << "3. 30 years at 5.75%" << endl;
        cout << "What is your selection:" << endl;
        */

        int iarray[3] = {0};
        float farray[3] = {0.0F};

        ifstream in("cr15.txt");
        in >> iarray[0] >> iarray[1] >> iarray[2];
        in >> farray[0] >> farray[1] >> farray[2];
        // do the same with the float array         

end quote.

Because of the /* ... */. The rates and years are between those so it is commentary, so that code will not be executed => no output.

Because of the '/* ... */'. The rates and years are between those so it is commentary, so that code will not be executed => no output.

so if i take out the comment block, does that mean that built the program correctly, and it will function correctly?

Edit: After re-reading it over and over again I finally see what you actually meant to do. You want to read the text file to output

1. 7 years at 5.35%
2. 15 years at 5.5%
3. 30 years at 5.75%

Give me some minutes to re-answer that part.

Edit: After re-reading it over and over again I finally see what you actually meant to do. You want to read the text file to output

1. 7 years at 5.35%
2. 15 years at 5.5%
3. 30 years at 5.75%

Give me some minutes to re-answer that part.

Thank you....Yeah if you couldnt tell...i do not know much about C++

Thank you!!

When i choose the option 1 to get interest rate and years, nothing comes up...it is blank. Then (with it blank) i can enter one of the options, then i gives me the answer. So i dont think that it is reading the .txt correctly.

...
		if (choice == '1') {
			/*
			cout << "Your Terms and Rates are as followed.  Please make a choice: " << endl;
			cout << "1. 7 years at 5.35%" << endl;
			cout << "2. 15 years at 5.5%" << endl;
			cout << "3. 30 years at 5.75%" << endl;
			cout << "What is your selection:" << endl;
			*/
			
			int iarray[3] = {0};
			float farray[3] = {0.0F};
			
			ifstream in("cr15.txt");
			in >> iarray[0] >> iarray[1] >> iarray[2];// Replace this line
			in >> farray[0] >> farray[1] >> farray[2];// Replace this line
			// do the same with the float array			
			
			int select;
			cin >> select;
			
			switch (select) {
				case 1:
...

I marked the lines that you need to replace.

I suggest that you use the following code to read the textfile and output it:

string line;
if (in.is_open()){ // we need to be sure that the file is open to prevent unnecessary errors
	while (! in.eof() ) // repeat until we reach the end of the file
	{
		getline (in,line); // read line in the file
		cout << line << endl; // output that line
	}
}

The code isn't that special, but it does what you asks. Don't forget to include the header string ( #include <string> ).

If you want to read the first 3 lines ONLY (so assume that the file contains more than those 3 lines), then you could use this:

string line;
if (in.is_open()){
	for(int n = 0; 3 > n; n++) // read the first 3 lines
	{
		getline (in,line);
		cout << line << endl;
	}
}

EDIT: *sigh* I knew this would be too easy. When I looked at your code again, I saw the following:

rate = interestRate[select - 1];
term = years[select - 1];

while you actually meant

rate = iarray[select - 1];
term = farray[select - 1];

give me some minutes again lol.

Replace the code which I mentioned before with:

if(in) // be sure that the file is open
{
  for (int n = 0; n < 3; n++) // read first 3 lines
  {
    in >> iarray[n] >> farray[n]; // read integer and float value
    cout << iarray[n] << farray[n]  << endl; // output integer and float value
  }
}

In this way you can initialize and output the values in the same loop.

I hope this is what you meant.

So if i replace that code....where am i adding the code for the cr15.txt file. I see what you said to replace, but then after that i am confused.

I put that code in there...but still nothing...not sure what else could be wrong....

...
if (choice == '1') {
/*
cout << "Your Terms and Rates are as followed.  Please make a choice: " << endl;
cout << "1. 7 years at 5.35%" << endl;
cout << "2. 15 years at 5.5%" << endl;
cout << "3. 30 years at 5.75%" << endl;
cout << "What is your selection:" << endl;
*/
			
int iarray[3] = {0};
float farray[3] = {0.0F};
			
ifstream in("cr15.txt");
if(in) // be sure that the file is open
{
  for (int n = 0; n < 3; n++) // read first 3 lines
  {
    in >> iarray[n] >> farray[n]; // read integer and float value
    cout << iarray[n] << farray[n]  << endl; // output integer and float value
  }
}
int select;
cin >> select;
		
switch (select) {
	case 1:
...
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.