5 Replies - 5017 Views - Last Post: 17 August 2008 - 04:10 PM Rate Topic: -----

#1 opus1965   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-August 08

take a file name as input

Posted 14 August 2008 - 09:11 AM

Quote

Here is my next class assignment. It is based on the mortgage calculator program. Modify your program to take a file name as an input to get all necessary values variables (e.g. interest rate, term, etc.). Allow the user to loop back and input another file with different values of variables. I know I have to prompt the user to enter a file name, how do I do that? I have to create several files so my question is: Is there a format I have to follow? After the user enters the file name I have to extract from that file the variables and assign them to other variables.

pseudocode:
     // ask user to input file name from a list
     // I need to open the file using inFile.open(list1.list, ios::in) I think ??
char inputFilename[] = "list1.list"; // this is the file I have to create
     double loanAmt [8];
     double yr[2];
     double rate[5]; // allows for decimal
     // inFile.close(); // I need to close the file
dbLoanAmt = loanAmt;
dbTermYr = yr;
dbIntRate = rate;


Quote

Any guidance is greatly appreciated. :wub:


Is This A Good Question/Topic? 0
  • +

Replies To: take a file name as input

#2 homemade-jam   User is offline

  • Gabe's Nemesis
  • member icon

Reputation: 11
  • View blog
  • Posts: 1,300
  • Joined: 17-March 08

Re: take a file name as input

Posted 14 August 2008 - 09:46 AM

Only quotes go in quotes....
Was This Post Helpful? 0
  • +
  • -

#3 opus1965   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-August 08

Re: take a file name as input

Posted 15 August 2008 - 01:05 PM

My bad. I'm a begginer.
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: take a file name as input

Posted 15 August 2008 - 02:22 PM

The rules say that we can't do your homework for you. Or even give you code until you have show some effort. We can help you understand but you have to get it started on your own. Once you get something if it doesn't work we would love to help you get it working.

[rules][/rules]
Was This Post Helpful? 0
  • +
  • -

#5 opus1965   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-August 08

Re: take a file name as input

Posted 17 August 2008 - 04:04 PM

Attached File  menu2.txt (13bytes)
Number of downloads: 89
Attached File  menu1.txt (14bytes)
Number of downloads: 104
OK, here is what I have.
#include "stdafx.h"

#include <iostream>
#include <fstream>
#include <iomanip>
#include <math.h>

using std::cin;
using std::cout;
using std::endl;
using std::fixed;
using std::setprecision;
using std::setw;

class Cmortgage
{
	private:
		double dbIntYr;				// interest rate per year	
		double dbLoanAmt;			// amount of the loan
		double dbTermYr;			// term in years
		double dbTermMonth;			// term converted to months
		double dbIntMonth;			// interest converted to months
		double dbPayment;			// variable for monthly payment
		double dbBalance;			// running total
		double dbPrinciplePaid;		// amount towards loan
		double dbMortgageAmt;		// total amount to be paid back

	public:
	void Get_File()
	{
		char chMenu = 0;
		
		cout << endl << "Please enter the number associated";
		cout << endl << "with the following available files:";
		cout << "1. menu1.txt";
		cout << "2. menu2.txt";
		cout << endl << "Your choice: ";
		cin  >> chMenu;
		if (chMenu == 1)
		{
			char inputFileName [] = "menu1.txt";
		} 
		else 
		{
			char inputFileName [] = "menu2.txt";
		}
		ifstream inFile;
		double loanAmt[6]; //
		double yearAmt[2];
		double rateAmt[4]; // allows for decimal

		inFile.open(inputFileName, ios::in)
			if (!inFile)
			{
				cerr << "Can't open input file " << inputFileName << endl;
			}
		while (inFile >> loanAmt >> YearAmt >> rateAmt)
		{
			dbLoanAmt = loanAmt;
			dbTermYr = yearAmt;
			dbIntYr = rateAmt;
		}
	} // Get_File()
	
	void Amort_Formula()
	{
		dbTermMonth = dbTermYr * 12;	// Converts years to months
		dbIntMonth = dbIntYr/1200;		// Converts annual rate to monthly rate
		dbPayment = (dbLoanAmt*dbIntMonth)/
				(1-pow(1+dbIntMonth,-dbTermMonth));// Amortization formula
	} // end Amort_Formula()

	void Print_Output()
	{
		system("cls");						// clears the screen
		cout << endl
			 << "Thank You!" << endl;
		cout << "Based on your input..." << endl
			 << endl << "Your Monthly Mortgage Payment Amount will be: $ "
			 << fixed << setprecision(2)	// Displays amount to two decimal places
			 << dbPayment << endl;			// Displays the monthly payment
	} // end Print_Output()

	void Print_List()
	{
		cout << endl
		     << "The following is the breakdown of what is applied to the "
		     << "interest along " << endl;
		cout << "with the remaining balance of the loan:  ";
			 system ( "PAUSE" );
		cout << endl << endl;			// headings
		cout << " Payment" << setw(15) << "Interest"
			 << endl;
		cout << " Number " << setw(15) << "  Paid  "
			 << setw(14) << "Balance " << endl;
		cout << " _______" << setw(15) << "________"
			 << setw(14) << "________" << endl;

		int intPymtNum = 1;				// counter for payments
		int max = 0;					// displays maximum lines on screen
		double dbIntRate = 0.0;			// calculated loan interest
		double dbIntPymt = 0.0;			// interest amount applied to loan
		double dbPrinciplePymt = 0.0;	// principle amount applied to loan
		double dbLoanBalance = 0.0;		// running balance of loan
		dbLoanBalance = dbLoanAmt;		// used for running total

		for(int i = 1, max = 1; intPymtNum <= dbTermMonth; i++)
		{	// controls the loop of monthly payments
			dbIntRate =dbIntMonth;
			dbIntPymt = dbLoanBalance * dbIntRate;
			dbPrinciplePymt = dbPayment - (dbLoanBalance * dbIntRate);
			dbLoanBalance = dbLoanBalance - dbPrinciplePymt;

			if (dbLoanBalance <= 0)
			{
				dbLoanBalance = 0;	// to avoid negative numbers
			}
			cout << endl
				 << setw(5) << intPymtNum << setw(17)
				 << dbIntPymt << setw(15) << dbLoanBalance;
			intPymtNum++;			// adds 1 to payment number
			max++;					// adds 1 to line count

			if (max == 20)
				{					// loop pauses program every 20 lines
					cout << endl;
					system ( "PAUSE" );
					max = 1;				// resets line count
					cout << endl << endl;	// prints headings again
					cout << " Payment" << setw(15) << "Interest"
						 << endl;
					cout << " Number " << setw(15) << "  Paid  "
						 << setw(14) << "Balance " << endl;
					cout << " _______" << setw(15) << "________"
						 << setw(14) << "________" << endl;
				} // end if loop
	    } // end for loop
	} // end Print_List()
}; // end class Cmortgage

int main()
{
	Cmortgage mortCalc;		// an object of the class Cmortgage

	char chIndicator = 'q';	// to quit or continue

	do{
		system("cls");			// clears the screen

		cout << "M O R T G A G E   C A L C U L A T O R" << endl;

		mortCalc.Get_File();		// calls function to get filename from user
		mortCalc.Amort_Formula();	// calls function for amortization formula
		mortCalc.Print_Output();	// calls function to print payment
		mortCalc.Print_List();		// calls function to print list

		cout << endl;				// Next line
		cout << "Press 'q' to quit or any key to start over: ";
		cin  >> chIndicator;		// Reads input from user
		cout << endl;

	}while ((chIndicator !='q') && (chIndicator != 'Q')); // end do loop

	return 0;
} // end main()

Was This Post Helpful? 0
  • +
  • -

#6 opus1965   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-August 08

Re: take a file name as input

Posted 17 August 2008 - 04:10 PM

Attached File  BuildLog.htm (8.33K)
Number of downloads: 66Here is the error log attached
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1