8 Replies - 2961 Views - Last Post: 11 September 2009 - 11:24 AM Rate Topic: -----

#1 Rayk8301  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 71
  • Joined: 09-January 09

Reading a sequential file

Posted 09 September 2009 - 12:05 PM

Could someone please take a look at the code and tell me why is it that I am having trouble reading the sequential file for this program. I would greatly apprecaite it. Please take a look at the attached sequential .txt file as well.

#include <cmath>				 
#include <cstdlib>			   
#include <iostream>			  
#include <iomanip> 
#include <limits>
#include "MortFile.txt"
using namespace std;

//starting a class called mortgage that would declare the grounds for loanAmount, Interest Rate, and Term
class mortgage
{	
public:
	double loanAmount, intRate;
	int Term;// Amount of the Loan
				
public:
	mortgage (double lAmt, double inRt, int Years):
	loanAmount(lAmt), intRate(inRt), Term(Years){}
};
//end of class

int main()
{
	//declaring the variable to read the sequential file
	ifstream InFile;
	//declaring variables for the math formula as well as the loops
	double intPaid, loanBalance, amountPaid, monthlyPayment, StartingBal;
	int paymentCounter, dividelist=0, numPayments;
	char listmore;
	char quit; // Prompt user to quit
		 quit = 'C'; 
	
	//the following array is commented out due to the use of sequential file	 
	//double InterestRT[3] = {5.35, 5.50, 5.75};
	//int choice, loanTerm[3] = {7, 15, 30};
		 InFile.Open("MortFile.txt", ios::in);
		
		//starting a while loop for the user to input the data required to out the results
		while (quit != 'Q' && quit != 'q')
			{
					
				StartingBal=0;

					cout << "			MORTGAGE CALCULATOR PROGRAM\n";
					cout<<"				By Ray Khano\n\n\n";
					
					do{
					cout<<"Please enter a loan Amount: ";
					//input validation to ensure that numeric numbers are being entered, if not, will prompt the user to try again.
					if (!(cin>>StartingBal) || (StartingBal<=0))
					{
						cout<<endl;
						cout << "PLEASE ENTER A NUMERIC VALUE GREATER THAN '0'. LET'S START OVER!\n";
						cout<<endl;
						StartingBal=0;
						cin.clear();
							cin.ignore(numeric_limits<streamsize>::max(), '\n');
						cout << endl;
					}
					}while (StartingBal==0);//end of input validation for the loan amount only.
					//Outputting the 3 loan options for the user to pick from.
					cout << endl << endl;
					cout << " please choose from the following menu:\n" << endl;
					cout << " 1.  7 Years at 5.35%\n " << endl; //setting the selection for the user.
					cout << " 2. 15 Years at 5.50%\n " << endl;  
					cout << " 3. 30 Years at 5.75%\n " << endl;
					cout << "Enter your choice of 1, 2, or 3: ";

						choice=0;
						do{
							//input validation for the loan option selections, that in case an invalid selection is entered.
							if (!(cin>>choice) || (cin<=0))
							{
								cout<<endl;
								cout<<"Please enter the choices listed below of either 1, 2, or 3.\n\n";
								cout<<"Enter choice: ";
								choice=0;
								cin.clear();
									cin.ignore(numeric_limits<streamsize>::max(), '\n');
							}
						  }while (choice==0);//end of input validation for the loan options.
						
						cout << endl << endl;
						//case break choices set out for the user to pick from at the assigned Interest Rate and Term based on the array.
						int i;
							for (i = 0; i < 1; i++)
							switch (choice)
							{
								case 0: cout << endl; 
								break;
								case 1:
								loanTerm[1] = 7;
								InterestRT[1] = 5.35; 
								cout << "\t	   You Selected 7 Years at 5.35% interest" << endl; 
								break;
								case 2: 
								loanTerm[2] = 15;
								InterestRT[2]= 5.50; 
								cout << "	  You Selected 15 Years at 5.50% interest" << endl; 
								break;
								case 3: 
								loanTerm[3] = 30;
								InterestRT[3] = 5.75; 
								cout << "	  You Selected 30 Years at 5.75% interest" << endl; 
								break;
								default: 
								cout << "	 Please choose either 1, 2, or 3" << endl;
							}
						//Here is the constructor calling the the mortgage class
						mortgage FirstMortgage (StartingBal, InterestRT[choice], loanTerm[choice]);//constructor
						//Math formula for the mortgage calculator
						monthlyPayment = StartingBal * (InterestRT[choice] / 1200) /
							 (1 - pow(1 + (InterestRT[choice] / 1200), - 1 * (loanTerm[choice] * 12)));
						//declaring number of payments for the proceeding for loop
						numPayments = loanTerm[choice] * 12;
						cout <<endl;
						cout << "	  Your Monthly Payments is: $" << monthlyPayment << endl << endl;

						dividelist = 0;
				//The proceeding for loop is to provide the breakdown of the loan balance and the interest paid for each payment throughout the loan
				for (paymentCounter = 1; paymentCounter <= numPayments; ++paymentCounter)
					{
						// Below is the math Formula for intPaid to set up value for amountPaid
						intPaid = StartingBal * (InterestRT[choice] / 1200);
						amountPaid = monthlyPayment - intPaid;
						loanBalance = StartingBal - amountPaid;

						if (loanBalance < 0)loanBalance = 0;
						StartingBal = loanBalance;
						
						if (dividelist == 0)
						{
							cout << "\t\tLoan Balance" << "\t\tInterest Paid" << endl;
							cout << "\t\t____________" << "\t\t_____________" << endl << endl;
						}
					
						cout << fixed << showpoint << setprecision(2) // Sets the number of digits printed to the right of the decimal point. 
						 << "\t\t$" << loanBalance 
						 << "\t\t$" << intPaid << endl;
						++dividelist;

						// User to Continue, enter New Data or Quit
						//dividelist to show 12 payments per screen shot and every time the user hits 'c && C' to continue.
						if (dividelist == 12)
						{
							cout << endl << endl;
							cout << "\tEnter 'C' to Continue, " << "'N' for New Data, " << "'Q' to Quit> ";
							cin >> listmore;
							if ((listmore == 'C')||(listmore == 'c'))dividelist = 0;
							else if ((listmore == 'N')||(listmore == 'n'))
								break; // Halt Current Program if New Data is entered, verify and restart
							else if ((listmore == 'Q')||(listmore == 'q'))
								return 0; // End Program
						}
					}

			
					{
						cout << "\tEnter 'C' to Continue, 'Q' to Quit> ";
						cin  >> quit;
						cout << "\n";
					}
				
				while ((quit != 'q') &&
					(quit != 'Q') &&
					(quit != 'c') &&
					(quit != 'C'));
			}
		
		cout << endl;
		system("PAUSE");			 // Pause program prior to exit
		return EXIT_SUCCESS;		 // Exit

}



Sorry about that .txt file as it looks like it's got 2 extensions. Here is the content in the file though.

30 0.0575
15 0.055
7 0.0535

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Reading a sequential file

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Reading a sequential file

Posted 09 September 2009 - 12:28 PM

Why are you #include-ing the text file???

Does this:
InFile.Open("MortFile.txt", ios::in);
even compile?

And you never even use InFile anyway? :blink:
Was This Post Helpful? 0
  • +
  • -

#3 Rayk8301  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 71
  • Joined: 09-January 09

Re: Reading a sequential file

Posted 09 September 2009 - 12:35 PM

Would it be possible to give me some guidance on how to get this problem resolved though? I would appreciate any help. Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,422
  • Joined: 15-July 09

Re: Reading a sequential file

Posted 09 September 2009 - 03:50 PM

As JackOfAllTrades said why are you openning the file?
you are not using it....
InFile.open("MortFile.txt)

Tell us what you want to do with the file and take it of the include.
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Reading a sequential file

Posted 09 September 2009 - 04:11 PM

I dont have time to look over someones complete source code, but to read a file
ifstream file("blah.txt");
string line = "";
while(getline(file,line))
{
   cout<<"line"<<endl;

}


Was This Post Helpful? 0
  • +
  • -

#6 Rayk8301  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 71
  • Joined: 09-January 09

Re: Reading a sequential file

Posted 11 September 2009 - 08:22 AM

Sorry about this guys, I just want the program to be able to read this out of the sequential file called MortFile.txt

30 0.0575
15 0.055
7 0.0535


Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Reading a sequential file

Posted 11 September 2009 - 09:22 AM

You've still not answered the questions which have already been posed to you.
Was This Post Helpful? 0
  • +
  • -

#8 Rayk8301  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 71
  • Joined: 09-January 09

Re: Reading a sequential file

Posted 11 September 2009 - 09:37 AM

Ok I've gone ahead and took out the include that you wanted me to take out. I've updated the code to the following:

#include <cmath>				 
#include <cstdlib>			   
#include <iostream>			  
#include <iomanip> 
#include <limits>  
#include <fstream>
using namespace std;

//starting a class called mortgage that would declare the grounds for loanAmount, Interest Rate, and Term
class mortgage
{	
public:
	double loanAmount, intRate;
	int Term;// Amount of the Loan
				
public:
	mortgage (double lAmt, double inRt, int Years):
	loanAmount(lAmt), intRate(inRt), Term(Years){}
};
//end of class

int main()
{
	//custome variables
	double cstmIntRate = -1;
	int cstmYears=0;

	//declaring variables for the math formula as well as the loops
	double intPaid, loanBalance, amountPaid, monthlyPayment, StartingBal;
	int paymentCounter, dividelist=0, numPayments;
	char listmore;
	char quit; // Prompt user to quit
		 quit = 'C'; 
	int choice;
	//delclaring variables for the aarays to operate the sequential file
	int loanTerm[4] = {0};
	double InterestRT[4] = {0.0};
	//reading the sequential file called MortFile that would containt the the preset mortgage loan and interest rates.
	ifstream in ("MortFile.txt");
	in >> loanTerm[1] >> loanTerm[2], loanTerm[3];
	in >> InterestRT[1] >> InterestRT[2] >> InterestRT[3];
	


		//starting a while loop for the user to input the data required to out the results
		while (quit != 'Q' && quit != 'q')
			{
					
				StartingBal=0;

					cout << "			MORTGAGE CALCULATOR PROGRAM\n";
					cout<<"				By Ray Khano\n\n\n";
					
					do{
					cout<<"Please enter a loan Amount: ";
					//input validation to ensure that numeric numbers are being entered, if not, will prompt the user to try again.
					if (!(cin>>StartingBal) || (StartingBal<=0))
					{
						cout<<endl;
						cout << "PLEASE ENTER A NUMERIC VALUE GREATER THAN '0'. LET'S START OVER!\n";
						cout<<endl;
						StartingBal=0;
						cin.clear();
							cin.ignore(numeric_limits<streamsize>::max(), '\n');
						cout << endl;
					}
					}while (StartingBal==0);//end of input validation for the loan amount only.
					//Outputting the 3 loan options for the user to pick from.
					cout << endl << endl;
					cout << " please choose from the following menu:\n" << endl;
					cout << " 1.  7 Years at 5.35%\n " << endl; //setting the selection for the user.
					cout << " 2. 15 Years at 5.50%\n " << endl;  
					cout << " 3. 30 Years at 5.75%\n " << endl;
					cout << " 4. Enter your preferred interest rate and term\n" << endl;
					cout << "Enter your choice of 1, 2, or 3: ";

						choice=0;
						do{
							//input validation for the loan option selections, that in case an invalid selection is entered.
							if (!(cin>>choice) || (cin<=0))
							{
								cout<<endl;
								cout<<"Please enter the choices listed below of either 1, 2, or 3.\n\n";
								cout<<"Enter choice: ";
								choice=0;
								cin.clear();
									cin.ignore(numeric_limits<streamsize>::max(), '\n');
							}
						  }while (choice==0);//end of input validation for the loan options.
						
						cout << endl << endl;
						//case break choices set out for the user to pick from at the assigned Interest Rate and Term based on the array.
						int i;
							for (i = 0; i < 1; i++)
							switch (choice)
							{
								case 0: cout << endl; 
								break;
								case 1:
								loanTerm[1];
								InterestRT[1]; 
								cout << "\t	   You Selected 7 Years at 5.35% interest" << endl; 
								break;
								case 2: 
								loanTerm[2];
								InterestRT[2]; 
								cout << "	  You Selected 15 Years at 5.50% interest" << endl; 
								break;
								case 3: 
								loanTerm[3];
								InterestRT[3]; 
								cout << "	  You Selected 30 Years at 5.75% interest" << endl; 
								break;
								case 4:
								cout << " Please Enter an interest rate (i.e 5.75):";
								cin >> cstmIntRate;
								cout<<endl;
								cout << " Please Enter the term in years:";
								cin >> cstmYears;
								break;
								default: 
								cout << "	 Please choose either 1, 2, or 3" << endl;
							}

							cout <<endl;

							if (cstmIntRate > 0)
							{
								mortgage FirstMortgage (StartingBal, InterestRT[choice], loanTerm[choice]);//constructor
								monthlyPayment = StartingBal * (cstmIntRate / 1200) /
										(1 - pow(1 + (cstmIntRate / 1200), - 1 * (cstmYears * 12)));
								cout << "	  Monthly Payments: $" << monthlyPayment << endl << endl;
								dividelist = 0;

								numPayments = cstmYears * 12;

								for (paymentCounter = 1; paymentCounter <= numPayments; ++paymentCounter)
									{
									
										// Below is the math Formula for intPaid to set up value for amountPaid
										intPaid = StartingBal * (cstmIntRate / 1200);
										amountPaid = monthlyPayment - intPaid;
										loanBalance = StartingBal - amountPaid;

										if (loanBalance < 0)loanBalance = 0;
										StartingBal = loanBalance;

										if (dividelist == 0)
										
										{
											cout << "\t\tLoan Balance" << "\t\tInterest Paid" << endl;
											cout << "\t\t____________" << "\t\t_____________" << endl << endl;
										}
					
										cout << fixed << showpoint << setprecision(2) // Sets the number of digits printed to the right of the decimal point. 
										<< "\t\t$" << loanBalance 
										<< "\t\t$" << intPaid << endl;
										++dividelist;

										// User to Continue, enter New Data or Quit
										if (dividelist == 12)
										
										{
											cout << endl << endl;
											cout << "\tEnter 'C' to Continue, " << "'N' for New Data, " << "'Q' to Quit> ";
											cin >> listmore;
											if ((listmore == 'C')||(listmore == 'c'))dividelist = 0;
											else if ((listmore == 'N')||(listmore == 'n'))
											break; // Halt Current Program if New Data is entered, verify and restart
											else if ((listmore == 'Q')||(listmore == 'q'))
											return 0; // End Program
										}
									}

									{
										cout << "\tEnter 'C' to Continue, 'Q' to Quit> ";
										cin  >> quit;
										cout << "\n";
				
				
										while ((quit != 'q') &&
										(quit != 'Q') &&
										(quit != 'c') &&
										(quit != 'C'));
									}
			
					}
							else

						//Here is the constructor calling the the mortgage class
						mortgage FirstMortgage (StartingBal, InterestRT[choice], loanTerm[choice]);//constructor
						//Math formula for the mortgage calculator
						monthlyPayment = StartingBal * (InterestRT[choice] / 1200) /
							 (1 - pow(1 + (InterestRT[choice] / 1200), - 1 * (loanTerm[choice] * 12)));
						//declaring number of payments for the proceeding for loop
						numPayments = loanTerm[choice] * 12;
						cout <<endl;
						cout << "	  Your Monthly Payments is: $" << monthlyPayment << endl << endl;

						dividelist = 0;
				//The proceeding for loop is to provide the breakdown of the loan balance and the interest paid for each payment throughout the loan
				for (paymentCounter = 1; paymentCounter <= numPayments; ++paymentCounter)
					{
						// Below is the math Formula for intPaid to set up value for amountPaid
						intPaid = StartingBal * (InterestRT[choice] / 1200);
						amountPaid = monthlyPayment - intPaid;
						loanBalance = StartingBal - amountPaid;

						if (loanBalance < 0)loanBalance = 0;
						StartingBal = loanBalance;
						
						if (dividelist == 0)
						{
							cout << "\t\tLoan Balance" << "\t\tInterest Paid" << endl;
							cout << "\t\t____________" << "\t\t_____________" << endl << endl;
						}
					
						cout << fixed << showpoint << setprecision(2) // Sets the number of digits printed to the right of the decimal point. 
						 << "\t\t$" << loanBalance 
						 << "\t\t$" << intPaid << endl;
						++dividelist;

						// User to Continue, enter New Data or Quit
						//dividelist to show 12 payments per screen shot and every time the user hits 'c && C' to continue.
						if (dividelist == 12)
						{
							cout << endl << endl;
							cout << "\tEnter 'C' to Continue, " << "'N' for New Data, " << "'Q' to Quit> ";
							cin >> listmore;
							if ((listmore == 'C')||(listmore == 'c'))dividelist = 0;
							else if ((listmore == 'N')||(listmore == 'n'))
								break; // Halt Current Program if New Data is entered, verify and restart
							else if ((listmore == 'Q')||(listmore == 'q'))
								return 0; // End Program
						}
					}

			
					{
						cout << "\tEnter 'C' to Continue, 'Q' to Quit> ";
						cin  >> quit;
						cout << "\n";
					}
				
				while ((quit != 'q') &&
					(quit != 'Q') &&
					(quit != 'c') &&
					(quit != 'C'));
			}
		
		cout << endl;
		system("PAUSE");			 // Pause program prior to exit
		return EXIT_SUCCESS;		 // Exit

}



I've also changed the text file which would include these rate terms as such:

0 7 15 30
0 5.35 5.50 5.75



I think the code now looks a lot cleaner and the it does compile but does not give me the output. I want it to read the interest rate and term from the text file. Hope this answers your question. Sorry about any confusion.
Was This Post Helpful? 0
  • +
  • -

#9 Rayk8301  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 71
  • Joined: 09-January 09

Re: Reading a sequential file

Posted 11 September 2009 - 11:24 AM

anyone?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1