Reputation: 11
#include <iostream>
#include <ostream>
#include <istream>
#include <ostream>
#include <fstream>
#include <sstream>
#include <string>
#include <iomanip>
void GetOutputFileStream(std::ofstream * fout, std::string filename);
void PrintStatistics(std::ostream & fout,
int numUsed,
int numNew,
double newTotalPrice,
double newTotalMileage,
double usedTotalPrice,
double usedTotalMileage);
int main()
{
double newTotalPrice = 33333;
double newTotalMileage = 44444;
double usedTotalPrice = 22222;
double usedTotalMileage = 99999;
int numUsed = 2;
int numNew = 3;
std::ofstream fout; // 'f'ile out - fout
std::string filename = "statistics.txt";
GetOutputFileStream(&fout, filename);
// Print to screen
PrintStatistics(std::cout,
numUsed,
numNew,
newTotalPrice,
newTotalMileage,
usedTotalPrice,
usedTotalMileage);
// Print to file
PrintStatistics(fout,
numUsed,
numNew,
newTotalPrice,
newTotalMileage,
usedTotalPrice,
usedTotalMileage);
std::cout << "Press ENTER to continue";
std::cin.get();
return 0;
}
void GetOutputFileStream(std::ofstream * fout, std::string filename)
{
fout->open(filename, std::ios::out);
}
void PrintStatistics(std::ostream & fout,
int numUsed,
int numNew,
double newTotalPrice,
double newTotalMileage,
double usedTotalPrice,
double usedTotalMileage)
{
}
PrintStatistics is empty because I want to fix this error before beginning to write the function.
I keep receiving : error C2065: 'filename' : undeclared identifier
However, whenever I try testing GetOutputFileStream(&fout, filename); to make sure its functional using sample mechanics in int main() as shown below:
std::ofstream fout; // 'f'ile out - fout
std::string filename = "newFile.txt";
GetOutputFileStream(&fout, filename);
fout << "This is my new file!\n";
fout << "This is on a new line!";
fout.close();
I do not receive any errors and the function behaves as its suppose to. Can anyone point me in the right direction? Thank you.
Upvotes: 0
Views: 1443
Reputation: 33864
It is not filename
that is causing you trouble. You dont define the following functions before you use them:
void GetOutputFileStream(std::ofstream * fout, std::string filename);
void PrintStatistics( ... );
You need to prototype them, or define them before they are used. See here for more info.
Here are your actual compiler errors.
And here is the same function with one way of fixing them.
Upvotes: 1