appending

Write your question here.
Add the ability to save data to a disk in one or more files. The menu(s) should give the user the option to save or retrieve data. Struggling to understand how to include this in my code. Thanks for the help in advance.
#include <iostream>
#include <cmath>
#include <fstream>


using namespace std;
//declaration of the prototypes
void showEntries();
void displayMenu();
int validateChoice(int);
int processChoices(int);




int main()
{
int c;

int choice;
displayMenu(); //displays the menu
cin >> choice;

//call the method named validateChoice for
//checking whether the valid choice is enterd or not
c = validateChoice(choice);
//call the method named processChoices
processChoices(c);

return 0;


}


void showEntries()
{
cout << "1. Add a customer/loan" << endl;
cout << "2. Delete a customerloan " << endl;
cout << "3. Exit the program " << endl;
}
void displayMenu()
{
cout << "------ Welcome to Loan Calculator ------" << endl;
cout << "Please choose from the following options." << endl;
showEntries();
}
int validateChoice(int c)
{
while (c != 1 && c != 2 && c != 3) {
cout << "Please enter a valid entry" << endl;
showEntries();
cin >> c;
}
return c;
}
int processChoices(int choice)
{
double interest_rate[5];
double loan_amount[5];
int length_loan[5];
double payment_amount[5];
int next_loan_index = 0;

while (true) {


if (choice == 1) {
cout << "Please enter the yearly interest rate: ";
cin >> interest_rate[next_loan_index];
cout << "Please enter the loan amount: ";
cin >> loan_amount[next_loan_index];
cout << "Please enter the length of loan in months: ";
cin >> length_loan[next_loan_index];

interest_rate[next_loan_index] = interest_rate[next_loan_index] / 100 / 12;


payment_amount[next_loan_index] = (interest_rate[next_loan_index] * loan_amount[next_loan_index]) / (1 - pow(1 + interest_rate[next_loan_index], -length_loan[next_loan_index]));


cout << "Loan amount: " << loan_amount[next_loan_index] << endl;
cout << "Yearly interest rate: " << interest_rate[next_loan_index] << endl;
cout << "Number of payments: " << length_loan[next_loan_index] << endl;
cout << "Payment amount: " << payment_amount[next_loan_index] << endl;

while (loan_amount[next_loan_index] <= 0)
{
cout << " Error " << endl;
cout << " Please start over: " << endl;
system("pause");
}

next_loan_index = next_loan_index + 1;


}
else if (choice == 2)
{
cout << " Delete customer loan ? " << endl;
}
else if (choice == 3)
{
break;
system("pause");
}
displayMenu(); //displays the menu
cin >> choice;

//call the method named validateChoice for
//checking whether the valid choice is enterd or not
choice = validateChoice(choice);
//call the method named processChoices
processChoices(choice);

ofstream outfile("loan_info.txt");

for (int i = 0; i<next_loan_index; i++)

{

outfile << "Customer Number: " << i << endl;
outfile << "Loan amount: " << loan_amount[i] << endl;
outfile << "Yearly interest rate: " << interest_rate[i] << endl;
outfile << "Number of payments: " << length_loan[i] << endl;
outfile << "Payment amount: " << payment_amount[i] << endl;

}
outfile.close();
}
system("pause");
return 0;

}
I assume by save and retrieve data that they mean through writing onto files?

Consider:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
	std::ofstream outFile;
	std::ifstream inFile;
	outFile.open("info.txt");
	inFile.open("info.txt");
	std::string inf;
	std::string userInput;

	//Tell user to input information to save onto text file
	getline(std::cin, userInput); //Get their Info
	
	outFile << userInput; //Save their input onto the text file
	outFile.close(); //Close outFile so we can use the text file to inFile

	getline(inFile, inf); //Grab the input saved to use again

	std::cout << inf; //Display Information saved
}


Simply grab user input, save it onto a file using ofstream, then retrieve it when needed using ifstream.
Last edited on
Topic archived. No new replies allowed.