I am trying to read data from a sequential file into an array. Here we are again at the old mortgage program. I have been doing some searching and can't seem to find the correct syntax to read the data into an array that can later be used.

So the sequential file is
7, 15, 30
5.35, 5.50, 5.75

which is the loan term and rates. If anyone can give me a direction on how to read these into an array from a text file it would be very helpful.
Thanks

Any attempt to post?

You need two arrays, the first is an array of integers that has the load terms and the second is an array of floats with the load amounts. There are other ways to do it but I doubt you have learned structores and c++ <vector> yet.

Exactly how to read the file depends on the exact instructions your teacher gave you. For example can you assume there will only be three loads in the file or do you have to consider a variable number of loans ?

I have been doing some searching and can't seem to find the correct syntax to read the data into an array that can later be used.

Post the code you have, we'll work it from there.

Just those three loans, 7 years at 5.35, 15 years at 5.5, and 30 years at 5.75. There are no special circumstances along with the assignment. Just have to load the data from the sequential file into the array. I can do the rest, the assignment is done I just need to figure how to load them into the array.

Well its pretty simple then, read the first three numbers into the first int array and the last three numbers into the float array. Beyond that hint you will have to post your code to get further assistance.

I want to read the array in void Mortgage::rateTerm() You can see in the code that I commented out the original array from the previous assignment. Now to read the array from the sequential file I have to declare the file, and read it in. I have tried several ways and can get it to read the first number of the first line but haven't figured out how to read in the entire line to the array. I tried getline, eof, and several other ways but I am unsure as to how to perform the call to read the numbers into the array properly. There isn't much out there to google that gives you examples. I even have several C++ books but they haven't been much help either in reading data into an array. I really just need an example, I don't want someone to do it for me. When I made the program in Java it was a bit simpler I was able to find an example, but haven't found one for C++.

My code is below.

#pragma once
#include <cmath>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
#include <iomanip>
using namespace std;
/********************************************************************
 Class Name: Mortgage
 Class Description: Class to take user input, cin the data from the user
					and give the option for three loans based on an array.
					Calculate the payment and amortize the loan for the user.
 Class Members:
 Public:
	void menu ();
	void getAmount ();
	void rateTerm ();
	void amort ();
	void setrateTerm ();
	void Payments ();
 Private:
	Amount, Term, Interest, Payment, monthTerm, Interestpaid
**********************************************************************/
class Mortgage
{
private:
	int Amount, Term, monthTerm;
	double Interest, Payment, Interestpaid, setInterest;
public:
	void getAmount ();	//Get Loan Amount
	void rateTerm ();	//Get Rate and Term from Array and calculate payment
	void setrateTerm ();//Get Rate and Term from user Input
	void amort ();		//Output Amoritization of loan
	void Payments ();	//Calculate Payment
};
/********************************************************************************
Function Name: getAmount
    
Function Description:	Gets the loan amount from user Input.
*********************************************************************************/
void Mortgage::getAmount()
{
	system("cls");
	cout<< "McBride Financial Services Mortgage Calculator\n";
	cout<<"_______________________________________________";
	cout<<"\n\n";
	cout<<"Enter Loan Amount (Dollars):$";
	cin >>Amount;
}
/********************************************************************************
Function Name: setrateTerm
    
Function Description:	Gets the rate and term from user input.
*********************************************************************************/
void Mortgage::setrateTerm()
{
	cout<<"\nEnter Term Length in years:";
	cin >>Term;
	cout<<"\n";
	cout<<"Enter Interest Rate:";
	cin >>Interest;	
}
/********************************************************************************
Function Name: rateTerm
    
Function Description:	Gets rate and term based on the users selection from an
						array.
*********************************************************************************/
void Mortgage::rateTerm()
{
	//Local Variables
	int checklist = 0;						//Check for if loop to make sure proper entry
	int choice=0;							//Verify choice of loan and send to array
	int ary;								//Choice of array from if loop
	//int Termyr[3]={7, 15, 30};				//Term for array
	//double Interestyr[3]={5.25, 5.5, 5.75};	//Interest for array

	int Termyr[3];
	double Interestyr[3];

	do
	{
		//Setup menu for loan to choose which term and interest rate
		cout<<"\n--------Loan Menu--------";
		cout<< "\n1) 7 years at 5.25%  \n2) 15 years at 5.5%  \n3) 30 years at 5.75%\n-------------------------\nEnter (1-3)";
		cin >> choice;
		checklist = 0;						//Reset checklist to 0 on loop

		if(choice <= 0)						//Error if user chooses 0 on loop
		{
			system("cls");
			cout<< "McBride Financial Services Mortgage Calculator\n";
			cout<<"_______________________________________________";
			cout<<"\n\n\n";
			cout<<"Loan Amount:$"<<Amount;
			cout<<"\n\nError! Please enter only 1, 2, or 3 to select the term and rate\n";
			checklist = 1;
		}	   
		else if(choice == 1)				//Send to array
		{
			ary=0;
		}
		else if(choice == 2)				//Send to array
		{
			ary=1;
		}
		else if (choice ==3)				//Send to array
		{
			ary=2;
		}
		else if (choice > 3)				//Error if user chooses a number greater then 3
		{
			system("cls");
			cout<< "McBride Financial Services Mortgage Calculator\n";
			cout<<"_______________________________________________";
			cout<<"\n\n\n";
			cout<<"Loan Amount:$"<<Amount;
			cout<<"\n\nError! Please enter only 1, 2, or 3 to select the term and rate\n";
			checklist = 1;
		}
	}
	while (checklist == 1);
	Term = Termyr[ary];						//Set Term for Mortgage Calculation
	Interest = Interestyr[ary];				//Set Interest for Mortgage Calculation
}
/********************************************************************************
Function Name: Payments
    
Function Description:	Calculates the users payment and sets monthTerm and
						Interestpaid for Amoritization loop.
*********************************************************************************/
void Mortgage::Payments()
{
	system("cls");
	//Calculate Payment
	Payment = Amount * (((Interest/100)/12)/(1-pow(1+((Interest/100)/12), - Term*12)));
	//Calculate Term of the loan in months for loan table
	monthTerm = (Term*12);
	//Calculate Interestpaid for loan table
	Interestpaid = ((Interest/100)/12);
}
/********************************************************************************
Function Name: amort
    
Function Description:	The amort function amoritizes the loan displaying the
						payment number, interest paid, and new balance of the loan.
						It also keeps the table from scrolling off the screen.
*********************************************************************************/
void Mortgage::amort()
{
//Local Variables
	int Month = 1;							//Month counter for Amoritization
	int pauselist = 5;						//declare variable for pauselist loop
	double Interestp;						//Calculate interestpaid for each month
	double Newbalance;						//Determine New Balance
	double Endingbalance;					//Determine Ending Balance
	const char TAB='\t';					//Tabs for columns
	Newbalance = Amount;					//Determine Newbalance starting value
	
	//Column Headers
	cout<<"\n--------------------------------------";
	cout<<"\nWith a loan of $"<<Amount<<" for "<<Term<<" years\nat a "<<Interest<<"% Interest rate your \nmonthly payment will be $"<<Payment;
	cout<<"\n--------------------------------------";
	cout<<"\nPayment" "\tInterest Paid" "\tNew Balance\n";
	while (monthTerm > 0)
	{
		// Calculate Interest Paid
		Interestp = Interestpaid * Newbalance;
		//Establish ending balance
		Endingbalance = Newbalance - Newbalance;

		//Loop for amoritization table to output payment number, interest paid, and new balance
		if (Newbalance > Payment)
		{
			//Output for Amoritization table
			cout.setf(ios::fixed);
			cout<<"\n"<< Month<<TAB<<"$"<<setprecision (2) <<Interestp<<TAB<<TAB<<"$"<<setprecision (2) <<Newbalance - Payment + Interestp;
				
			//Loop to Pause List after 15 lines
			if (pauselist < 19)
			{
				++pauselist;
			}
			else
			{
				cout<<"\n";
				system ("PAUSE");
				pauselist = 0;
				system("cls");
				cout<<"Payment" "\tInterest Paid" "\tNew Balance\n";
			}
		}
		else if (Newbalance <= Payment)
		{
			cout.setf(ios::fixed);
			cout<<"\n"<< Month<<TAB<<"$"<<setprecision (2) <<Interestp<<TAB<<TAB<<"$"<<Endingbalance;
			pauselist = 0;
		}			
		Newbalance = Newbalance - Payment + Interestp;      //Reset Newbalance value for next loop
		Month = ++Month;									//Count Month for each loop
		monthTerm= --monthTerm;								//Countdown number of months for loop
	}

}
//Begin Main
char ch = 'y';
int menu = 0;
int main()
{
	Mortgage loan;
	do
	{
		system("cls");
		cout<< "McBride Financial Services Mortgage Calculator\n";
		cout<<"_______________________________________________";
		cout << "\n\n-------- Main Menu ---------";
		cout << "\n  1. Enter rate and term   ";
		cout << "\n  2. Select loan from menu ";
		cout << "\n----------------------------\n";
		cout << "Press 1 or 2: ";
		cin >>menu;

		loan.getAmount();
		//Loop after selecting type of loan
		if (menu == 1)
		{
			loan.setrateTerm();
		}
		else if (menu == 2)
		{
			loan.rateTerm();
		}

		loan.Payments();
		loan.amort();
		cout<< "\nEnter another loan (y)es or (n)o):";
		cin>>ch;
	}
	while(ch=='y');
	return 0;
}

Depending on the file format you would normally read the data one element at a time. The example file you posted in your original post -- does the file really contain those commas ? If not then it would look like this when viewed by notepad

7 15 30
5.35 5.50 5.75

If the above is correct, then

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

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

Or you could use loops in the above. Either will work for your program.

That was pretty simple, I guess I was making it to hard. Thanks a lot for the help.

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.