Hi,
I'm having trouble in one part of my code. My code is supposed to write/use a single function that opens a single file (and checks for a successful open) but, you must ask the user for the file name, as opposed to using fixed names.
I'm writing a void function called OpenCheck to do this but i am not sure on how to get the user-inputed filename to open in my function. Can anyone help me out?
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void OpenCheck(ifstream&, string);
int ReadFirstFile(ifstream&,int[],string[]);
int main()
{
ifstream inFile1, inFile2;
int num, count;
string name;
string file1, file2;
cout << "Enter a name for first input file:";
cin >> file1;
cout << "\n Enter a name for the second input file:";
OpenCheck(inFile1, file1);
OpenCheck(inFile2, file2);
return 0;
system("Pause");
}
//functioin definition for OpenCheck
void OpenCheck(ifstream&, string file)
{
inFile.open(file.c_str());
if(inFile.fail())
{
cout << "Error Opening "
<< filename
<< ". Program Terminated. \n";
system("Pause")
exit(1);
}
}
//function definition for ReadFirstFile
int ReadFirstFile(ifstream&,int[],string[]);
{
count = 0;
// priming read
inFile1 >> num;
getline(inFile1,name);
while (!inFile1.eof())
{
// store data in the integer and string arrays
integerArray[count] = num; // come up with better variable names
stringArray[count] = name; // than integerArray & stringArray
count++;
// repeat priming read
inFile1 >> num;
getline(inFile1,name);
}
}