help with homework

hello, this homework is due and im already late.. for some reason my code is is compiling with some errors but it does execute. However, its not displaying the file contents on the console and its not writing to the output file specified..

PLEASE HELP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
 #include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <iomanip>
using namespace std;
// Function prototypes
std::ifstream GetInputFile(std::string);
std::ofstream GetOutputFile(std::string);
void PrintFileContents(std::ifstream& ifile);
void ProcessAccounts(std::ifstream& ifile, std::ofstream& ofile, double);
void SetInputStreamPos(std::ifstream &str, int pos);
void SetOutputStreamPos(std::ofstream &str, int pos);
//[BEGIN MAIN]
int main()
{
	std::string inputFileName;
	std::string outputFileName;
	double interestRate;
	std::cout << "Enter an input filename: ";
	std::cin >> inputFileName;
	std::cout << "Enter an output filename: ";
	std::cin >> outputFileName;
	std::cout << "Enter an interest rate (%): ";
	std::cin >> interestRate;
	std::cout << std::endl;
	std::ifstream ifile;
	GetInputFile(inputFileName);
	std::ofstream ofile;
	GetOutputFile(outputFileName);
	std::cout << "Current account status:" << std::endl;
	PrintFileContents(ifile);
	SetInputStreamPos(ifile, 0);
	ProcessAccounts(ifile, ofile, interestRate / 100);
	ifile.close();
	ofile.close();
	std::cout << std::endl;
	std::cout << "New account status:" << std::endl;
	GetInputFile(outputFileName);
	SetInputStreamPos(ifile, 0);
	PrintFileContents(ifile);
	ifile.close();
	std::cout << "Press ENTER";
	std::cin.ignore();
	std::cin.get();
	return 0;
}
//[END MAIN]
void ProcessAccounts(std::ifstream &ifile, std::ofstream &ofile, double rate)
{
	// Everyday the bank checks the status of the accounts and processes them
	// If the customer has:
	// 1) a positive savings balance, pay them interest
	// 2) a negative savings balance, transfer it to credit (credit is positive)
	// 3) a credit balance, charge them interest
	// Note: Unlike normal banks, the interest rate for savings and credit is the same
	// Read the info from the input file
	// Put the new info into the output file
	// The format for the input file and output file should be
	// [NAME] [SAVINGS BALANCE] [CREDIT BALANCE]
	// There is a single space between each value and a newline at the end
	// Put your code here
	std::string name;
	double sav_balance;
	double credit_balance;
	ifile >> name >> sav_balance >> credit_balance;
	while (!ifile.eof())
	{
		double sav_final = 0;
		double credit_final = 0;
		if (sav_balance > 0) //if positive savings balance, pay them interest
			sav_final = sav_balance + sav_balance*rate;
		else if (sav_balance < 0) // if a negative savings balance, transfer it to credit (credit is positive)
			credit_final = credit_balance - sav_balance;
		if (credit_balance > 0) // if a credit balance, charge  interest
			credit_final = credit_balance + credit_balance*rate;
		ofile << name << " " << sav_final << " " << credit_final << std::endl;
		ifile >> name >> sav_balance >> credit_balance;
	}
}
void PrintFileContents(std::ifstream &str)
{
	// Print the contents of the file
	// First, print the file headers
	// Then, print each line.
	// Make sure the text is properly formatted
	// Remember the functions available in iomanip?
	// EXAMPLE:
	// Name Savings Credit
	// Bob $23.56 $0.00
	// Joe $43.52 $0.00
	// Sally -$1.58 $0.00
	// Put your code here
	std::string name;
	double sav_balance;
	double credit_balance;
	std::cout << left << setw(10) << "Name" << setw(11) << "Savings" << setw(7) << "Credit" << std::endl;
	str >> name >> sav_balance >> credit_balance;
	while (!str.eof())
	{
		if (sav_balance < 0)
		{
			std::cout << left << setw(10) << name << setw(1) << "-$" << setw(9) << fixed << std::setprecision(2) << sav_balance*-1 << setw(1) << "$" << setw(7) << fixed << std::setprecision(2) << credit_balance << std::endl;
			str >> name >> sav_balance >> credit_balance;
		}
		else
		{
			std::cout << left << setw(11) << name << setw(1) << "$" << setw(9) << fixed << setprecision(2) << sav_balance << setw(1) << "$" << setw(7) << fixed << setprecision(2) << credit_balance << std::endl;
			str >> name >> sav_balance >> credit_balance;
		}
	}
}
std::ifstream GetInputFile(std::string filename)

{

	std::ifstream inFile(filename, std::ios::in);
	if (!inFile)
	std::cout << "Wrong file name !" << std::endl;

	else
	return inFile;
}

std::ofstream GetOutputFile(std::string filename)

{
	std::ofstream outFile(filename, std::ios::out);
	if (!outFile)
		std::cout << "enter a file name !" << std::endl;
	else
		return outFile;

}

void SetInputStreamPos(std::ifstream &str, int pos)
{
	// Set the 'g'et cursor to the desired byte in the stream
	// Use the beginning of the file as the reference point
	// Put your code here
	str.clear();
	str.seekg(pos, std::ios::beg);
}
void SetOutputStreamPos(std::ofstream &str, int pos)
{
	// Set the 'p'ut cursor to the desired byte in the stream
	// Use the beginning of the file as the reference point
	// Put your code here
	str.clear();
	str.seekp(pos, std::ios::beg);
}



my output


Enter an input filename: a1.txt
Enter an output filename: a2.txt
Enter an interest rate (%): 6

Current account status:
Name Savings Credit
Last edited on
File name needs to be in "" as far as i know.
Have you checked if it's even creating the files? (if they doesn't exist already).
Last edited on
thanks for your quick response..
re: "" what line number do you mean ?

and yes the output files are being created however they are blank.

Never mind about "", i missed something.

But don't loop on eof() it can break with even minor changes in program.


Last edited on
made these changes to main and i have better results.. its creating an output file however its not writing to it or printing to screen ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <iomanip>
using namespace std;
// Function prototypes
std::ifstream GetInputFile(std::string);
std::ofstream GetOutputFile(std::string);
void PrintFileContents(std::ifstream& ifile);
void ProcessAccounts(std::ifstream& ifile, std::ofstream& ofile, double);
void SetInputStreamPos(std::ifstream &str, int pos);
void SetOutputStreamPos(std::ofstream &str, int pos);
//[BEGIN MAIN]
int main()
{
	std::string inputFileName;
	std::string outputFileName;
	double interestRate;

	std::cout << "Enter an input filename: ";
	std::cin >> inputFileName;
	std::cout << "Enter an output filename: ";
	std::cin >> outputFileName;
	std::cout << "Enter an interest rate (%): ";
	std::cin >> interestRate;

	std::cout << std::endl;
	std::ifstream ifile = GetInputFile(inputFileName);
	std::ofstream ofile = GetOutputFile(outputFileName);

	std::cout << "Current account status:" << std::endl;
	PrintFileContents(ifile);

	ProcessAccounts(ifile, ofile, interestRate);

	ifile.close();
	ofile.close();

	std::cout << std::endl;
	std::cout << "New account status:" << std::endl;
	ifile = GetInputFile(outputFileName);

	PrintFileContents(ifile);

	ifile.close();

	std::cout << "Press ENTER";
	std::cin.ignore();
	std::cin.get();
	return 0;
}
//[END MAIN]
void ProcessAccounts(std::ifstream &ifile, std::ofstream &ofile, double rate)
{
	// Everyday the bank checks the status of the accounts and processes them
	// If the customer has:
	// 1) a positive savings balance, pay them interest
	// 2) a negative savings balance, transfer it to credit (credit is positive)
	// 3) a credit balance, charge them interest
	// Note: Unlike normal banks, the interest rate for savings and credit is the same
	// Read the info from the input file
	// Put the new info into the output file
	// The format for the input file and output file should be
	// [NAME] [SAVINGS BALANCE] [CREDIT BALANCE]
	// There is a single space between each value and a newline at the end
	// Put your code here
	std::string name;
	double sav_balance;
	double credit_balance;
	ifile >> name >> sav_balance >> credit_balance;
	while (!ifile.eof())
	{
		double sav_final = 0;
		double credit_final = 0;
		if (sav_balance > 0) //if positive savings balance, pay them interest
			sav_final = sav_balance + sav_balance*rate;
		else if (sav_balance < 0) // if a negative savings balance, transfer it to credit (credit is positive)
			credit_final = credit_balance - sav_balance;
		if (credit_balance > 0) // if a credit balance, charge  interest
			credit_final = credit_balance + credit_balance*rate;
		ofile << name << " " << sav_final << " " << credit_final << std::endl;
		ifile >> name >> sav_balance >> credit_balance;
	}
}
void PrintFileContents(std::ifstream &str)
{
	// Print the contents of the file
	// First, print the file headers
	// Then, print each line.
	// Make sure the text is properly formatted
	// Remember the functions available in iomanip?
	// EXAMPLE:
	// Name Savings Credit
	// Bob $23.56 $0.00
	// Joe $43.52 $0.00
	// Sally -$1.58 $0.00
	// Put your code here
	std::string name;
	double sav_balance;
	double credit_balance;
	std::cout << left << setw(10) << "Name" << setw(11) << "Savings" << setw(7) << "Credit" << std::endl;
	str >> name >> sav_balance >> credit_balance;
	while (!str.eof())
	{
		if (sav_balance < 0)
		{
			std::cout << left << setw(10) << name << setw(1) << "-$" << setw(9) << fixed << std::setprecision(2) << sav_balance*-1 << setw(1) << "$" << setw(7) << fixed << std::setprecision(2) << credit_balance << std::endl;
			str >> name >> sav_balance >> credit_balance;
		}
		else
		{
			std::cout << left << setw(11) << name << setw(1) << "$" << setw(9) << fixed << setprecision(2) << sav_balance << setw(1) << "$" << setw(7) << fixed << setprecision(2) << credit_balance << std::endl;
			str >> name >> sav_balance >> credit_balance;
		}
	}
}
std::ifstream GetInputFile(std::string filename)

{

	std::ifstream inFile(filename, std::ios::in);
	if (!inFile)
	std::cout << "Wrong file name !" << std::endl;

	else
	return inFile;
}

std::ofstream GetOutputFile(std::string filename)

{
	std::ofstream outFile(filename, std::ios::out);
	if (!outFile)
		std::cout << "enter a file name !" << std::endl;
	else
		return outFile;

}

void SetInputStreamPos(std::ifstream &str, int pos)
{
	// Set the 'g'et cursor to the desired byte in the stream
	// Use the beginning of the file as the reference point
	// Put your code here
	str.clear();
	str.seekg(pos, std::ios::beg);
}
void SetOutputStreamPos(std::ofstream &str, int pos)
{
	// Set the 'p'ut cursor to the desired byte in the stream
	// Use the beginning of the file as the reference point
	// Put your code here
	str.clear();
	str.seekp(pos, std::ios::beg);
}


output

Enter an input filename: a1.txt
Enter an output filename: a6.txt
Enter an interest rate (%): 8

Current account status:
Name Savings Credit
Jim $23.40 $0.00
Bob $58.32 $0.00
Sally -$1.34 $0.00

New account status:
Name Savings Credit
Press ENTER
can you specify line number?
You're trying to write variables that are not initialized to a file?

Last edited on
I have not worked that much with files, but i doubt you can read variables from text files.
Unless you store something in them.
It's in the function at line 88.
Last edited on
Topic archived. No new replies allowed.