file Operations inputting a file into program

I keep getting this error when trying to compile. Can anyone help?

error C2440: 'initializing' : cannot convert from 'void' to 'std::basic_ifstream<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> Expressions of type void cannot be converted to other types


Here is my entire program:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

// Function prototypes
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())
{
//std::cout << name << " " << sav_balance << " " << credit_balance << std::endl;
double sav_final=0;
double credit_final=0;
if(sav_balance > 0) // 1) a positive savings balance, pay them interest
sav_final = sav_balance + sav_balance*rate;
else if(sav_balance < 0) // 2) a negative savings balance, transfer it to credit (credit is positive)
credit_final = credit_balance - sav_balance;
if(credit_balance > 0) // 3) a credit balance, charge them interest
credit_final = credit_final + credit_balance*rate;
ofile << name << " " << sav_final << " " << credit_final << std::endl;
//std::cout << 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 <<"Name\tSavings\tCredit" << std::endl;
str >> name >> sav_balance >> credit_balance;
while(!str.eof())
{
std::cout << std::setprecision(2) << std::fixed << name << "\t$" << sav_balance <<"\t$"<< credit_balance << std::endl;
str >> name >> sav_balance >> credit_balance;
}
}

std::ifstream GetInputFile(std::string filename)
{
// Open the file named filename
// Make sure it exists
// Return it as an input file stream

// Put your code here
str.open(filename());
if(!str.is_open())
{
std::cout <<"unable to open file so Exiting .." << std::endl;
system("pause");
return;
}
}

std::ofstream GetOutputFile(std::string filename)
{
// Open the file named filename
// Make sure it exists
// Return it as an output file stream

// Put your code here
str.open(filename());
if(!str.is_open())
{
std::cout <<"unable to open file so Exiting .." << std::endl;
system("pause");
return;
}
}

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.seekg(pos, std::ios::beg);
}
Last edited on
str.open(filename()); <-- there's an extra set of parenthesis after filename. filename is a string with the file name, not a function.

The function also doesn't know what str is - you might be getting a different compiler error for that. And the return statement isn't returning anything when the function has been defined to return something.
Sorry Im a little new to c++ in your opinion how would fix the code?
Remove the extra set of parenthesis for that error.

str.open(filename()); --> str.open(filename);
Ok i did that and i tried to compile and now Im getting these errors
1> error C3861: 'GetInputFile': identifier not found
1> error C3861: 'GetOutputFile': identifier not found
1> error C3861: 'GetInputFile': identifier not found
1> error C2065: 'str' : undeclared identifier
1> error C2228: left of '.open' must have class/struct/union type is ''unknown-type''
1> error C2065: 'str' : undeclared identifier
1> error C2228: left of '.is_open' must have class/struct/union type is ''unknown-type''
1> error C2561: 'GetInputFile' : function must return a value
1> error C2065: 'str' : undeclared identifier
1> error C2228: left of '.open' must have class/struct/union type is ''unknown-type''
1> error C2065: 'str' : undeclared identifier
1> error C2228: left of '.is_open' must have class/struct/union type is ''unknown-type''
1> error C2561: 'GetOutputFile' : function must return a value
1> error C2039: 'seekg' : is not a member of 'std::basic_ofstream<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
Check for missing function prototypes of functions you call but don't define until after main.

You're using str in some functions where you haven't sent str as a parameter, so the function doesn't know what str is.

Function must return a value - if you define a function to return a value, but don't - you either need to return a value, or redefine the function to be void if nothing needs to be returned.

seekg is not a member - maybe you mean to use seekp? http://www.cplusplus.com/reference/ostream/ostream/seekp/
Ok i got some of them fixed but not all now Im getting these errors:
error C3861: 'GetInputFile': identifier not found
error C3861: 'GetOutputFile': identifier not found
error C3861: 'GetInputFile': identifier not found
error C2228: left of '.open' must have class/struct/union type is 'int'
error C2228: left of '.is_open' must have class/struct/union type is 'int'
error C2561: 'GetInputFile' : function must return a value
error C2228: left of '.open' must have class/struct/union type is 'int'
error C2228: left of '.is_open' must have class/struct/union type is 'int'
error C2561: 'GetOutputFile' : function must return a value
error C2039: 'seekp' : is not a member of 'std::basic_ifstream<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]

Here is my code now: Can you tell me where to change to make the necessary changes.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

// Function prototypes
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())
{
//std::cout << name << " " << sav_balance << " " << credit_balance << std::endl;
double sav_final=0;
double credit_final=0;
if(sav_balance > 0) // 1) a positive savings balance, pay them interest
sav_final = sav_balance + sav_balance*rate;
else if(sav_balance < 0) // 2) a negative savings balance, transfer it to credit (credit is positive)
credit_final = credit_balance - sav_balance;
if(credit_balance > 0) // 3) a credit balance, charge them interest
credit_final = credit_final + credit_balance*rate;
ofile << name << " " << sav_final << " " << credit_final << std::endl;
//std::cout << 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 <<"Name\tSavings\tCredit" << std::endl;
str >> name >> sav_balance >> credit_balance;
while(!str.eof())
{
std::cout << std::setprecision(2) << std::fixed << name << "\t$" << sav_balance <<"\t$"<< credit_balance << std::endl;
str >> name >> sav_balance >> credit_balance;
}
}

std::ifstream GetInputFile(std::string filename)
{
// Open the file named filename
// Make sure it exists
// Return it as an input file stream

// Put your code here
int str;
str.open(filename);
if(!str.is_open())
{
std::cout <<"unable to open file so Exiting .." << std::endl;
system("pause");
return;
}
}

std::ofstream GetOutputFile(std::string filename)
{
// Open the file named filename
// Make sure it exists
// Return it as an output file stream

// Put your code here
int str;
str.open(filename);
if(!str.is_open())
{
std::cout <<"unable to open file so Exiting .." << std::endl;
system("pause");
return;
}
}

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.seekp(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);
}
Check for missing function prototypes of functions you call but don't define until after main. You have 6 functions defined besides main, but only 4 function prototypes are listed before main.

Function must return a value - if you define a function to return a value, but don't - you either need to return a value, or redefine the function to be void if nothing needs to be returned. You're expecting something to be returned from the functions and assigned to ifile or ofile.

1
2
std::ifstream ifile = GetInputFile(inputFileName);
std::ofstream ofile = GetOutputFile(outputFileName);


seekg is not a member error - maybe you mean to use seekp? http://www.cplusplus.com/reference/ostream/ostream/seekp/

Edit: Why are you making str an int? It's not an integer value.
1
2
int str;
str.open(filename);


Code tags will make the code easier to read. (The <> button on the right side of the post)

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
159
160
161
162
163
164
165
166
167
168
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

// Function prototypes
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())
{
//std::cout << name << " " << sav_balance << " " << credit_balance << std::endl;
double sav_final=0;
double credit_final=0;
if(sav_balance > 0) // 1) a positive savings balance, pay them interest
sav_final = sav_balance + sav_balance*rate;
else if(sav_balance < 0) // 2) a negative savings balance, transfer it to credit (credit is positive)
credit_final = credit_balance - sav_balance;
if(credit_balance > 0) // 3) a credit balance, charge them interest
credit_final = credit_final + credit_balance*rate;
ofile << name << " " << sav_final << " " << credit_final << std::endl;
//std::cout << 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 <<"Name\tSavings\tCredit" << std::endl;
str >> name >> sav_balance >> credit_balance;
while(!str.eof())
{
std::cout << std::setprecision(2) << std::fixed << name << "\t$" << sav_balance <<"\t$"<< credit_balance << std::endl;
str >> name >> sav_balance >> credit_balance;
}
}

std::ifstream GetInputFile(std::string filename)
{
// Open the file named filename
// Make sure it exists
// Return it as an input file stream

// Put your code here
int str;
str.open(filename);
if(!str.is_open())
{
std::cout <<"unable to open file so Exiting .." << std::endl;
system("pause");
return;
}
}

std::ofstream GetOutputFile(std::string filename)
{
// Open the file named filename
// Make sure it exists
// Return it as an output file stream

// Put your code here
int str;
str.open(filename);
if(!str.is_open())
{
std::cout <<"unable to open file so Exiting .." << std::endl;
system("pause");
return;
}
}

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.seekp(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);
}
Last edited on
First off I appreciate all the help and I got most off the errors to go away.I am sorry if I am not understanding but Im still getting the undeclared functions. And its saying str is undefined. and error C2228: left of '.open' must have class/struct/union type is ''unknown-type''

#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>

// Function prototypes
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())
{
//std::cout << name << " " << sav_balance << " " << credit_balance << std::endl;
double sav_final=0;
double credit_final=0;
if(sav_balance > 0) // 1) a positive savings balance, pay them interest
sav_final = sav_balance + sav_balance*rate;
else if(sav_balance < 0) // 2) a negative savings balance, transfer it to credit (credit is positive)
credit_final = credit_balance - sav_balance;
if(credit_balance > 0) // 3) a credit balance, charge them interest
credit_final = credit_final + credit_balance*rate;
ofile << name << " " << sav_final << " " << credit_final << std::endl;
//std::cout << 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 <<"Name\tSavings\tCredit" << std::endl;
str >> name >> sav_balance >> credit_balance;
while(!str.eof())
{
std::cout << std::setprecision(2) << std::fixed << name << "\t$" << sav_balance <<"\t$"<< credit_balance << std::endl;
str >> name >> sav_balance >> credit_balance;
}
}

std::ifstream GetInputFile(std::string filename)
{
// Open the file named filename
// Make sure it exists
// Return it as an input file stream

// Put your code here
str.open(filename);
if(!str.is_open())
{
std::cout <<"unable to open file so Exiting .." << std::endl;
system("pause");
}
}

std::ofstream GetOutputFile(std::string filename)
{
// Open the file named filename
// Make sure it exists
// Return it as an output file stream

// Put your code here
str.open(filename);
if(!str.is_open())
{
std::cout <<"unable to open file so Exiting .." << std::endl;
system("pause");
}
}

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);
}
You've been asked once already - please use code tags to make your code more readable:

http://www.cplusplus.com/articles/z13hAqkS/
Line 26: No forward prototype for GetInputFile.
Line 27: No forward prototype for GetOutputFile.
Line 122: No stream named str.
Line 127: stream is not returned.
Line 135: No stream named str.
Line 140: stream is not returned.

If you don't know what lines these line numbers refer to, I suggest you edit your post and apply code tags as has already been requested twice.


Here is the file using code tags. It is giving more errors now
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

// Function prototypes
	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);
	std::ifstream GetInputFile(std::string filename);
    //std::ofstream GetOutputFile(std::string filename);

//[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())
	{
		//std::cout << name << " " << sav_balance << " " << credit_balance << std::endl;
		double sav_final=0;
		double credit_final=0;

		if(sav_balance > 0) // 1) a positive savings balance, pay them interest
		sav_final = sav_balance + sav_balance*rate;

		else if(sav_balance < 0) // 2) a negative savings balance, transfer it to credit (credit is positive)
		credit_final = credit_balance - sav_balance;

		if(credit_balance > 0) // 3) a credit balance, charge them interest
		credit_final = credit_final + credit_balance*rate;
		ofile << name << " " << sav_final << " " << credit_final << std::endl;
		//std::cout << 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 <<"Name\tSavings\tCredit" << std::endl;
	str >> name >> sav_balance >> credit_balance;

	while(!str.eof())
	{
		std::cout << std::setprecision(2) << std::fixed << name << "\t$" << sav_balance <<"\t$"<< credit_balance << std::endl;
		str >> name >> sav_balance >> credit_balance;
	}
}

std::ifstream GetInputFile(std::string filename)
{
	// Open the file named filename
	// Make sure it exists
	// Return it as an input file stream

	// Put your code here
	std::ifstream ifile;

	ifile.open(filename);

	if(!ifile.is_open())
	{
		ifile.close();
	}
	else;
	{
		std::cout <<"unable to open file so Exiting .." << std::endl;
	}
	return ifile;
}

std::ofstream GetOutputFile(std::string filename)
{
	// Open the file named filename
	// Make sure it exists
	// Return it as an output file stream

	// Put your code here

	std::ofstream ofile;

	ofile.open(filename);

	if(!ofile.is_open())
	{
		ofile.close();
	}
	else
	{
		std::cout <<"unable to open file so Exiting .." << std::endl;
	}
	return ofile;
}

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);
}
Here is the file using code tags. It is giving more errors now


Is there any particular reason you're withholding the details of what those errors are?
You cannot copy a stream, hence GetInputFile(...)/GetOutputFile(...) are both invalid.

Instead pass the stream as a reference like to the other function.

Don't forget to apply the changes to the prototypes as well.
Topic archived. No new replies allowed.