Hello JM567,
How do I properly define inFile1 and inFile2? |
"inFile1" and "infile2" are already defined on lines 5 and 6. Lines 15 and 16 only need to open the file streams. The "inFile1 = " is not needed. The same for line 16.
Your original post was lacking some important information, so I guessed for now.
With everything that has been said and using keskiverto's suggestion this is what I have come up with:
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
|
#include <iostream>
#include <string>
#include <fstream>
#include <chrono>
#include <thread>
struct dataRecord
{
std::string s_fName;
std::string s_lName;
std::string s_mI;
};
// Proto Types
int readHeader(std::ifstream& inFile);
int main()
{
// Declare variables
int numRecordsInFile1{};
int numRecordsInFile2{};
int totalRecord{};
dataRecord record;
// Open files for input
std::ifstream inFile1;
std::ifstream inFile2;
inFile1.open("datafile1.txt"); // <--- Moved open here so the if statements would work.
inFile2.open("datafile2.txt");
if (!inFile1.is_open())
{
std::cout << "Sorry, could not open file." << std::endl;
// <--- Added next line or you may not be able to see the message.
std::this_thread::sleep_for(std::chrono::seconds(3)); // Requires header files "chrono" and "thread"
exit(0);
}
if (!inFile2.is_open())
{
std::cout << "Sorry, could not open file." << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3)); // Requires header files "chrono" and "thread"
exit(0);
}
// Make function calls to get size to use in new statement to create dynamic array
numRecordsInFile1 = readHeader(inFile1);
numRecordsInFile2 = readHeader(inFile2);
// Get dynamic memory
totalRecord = numRecordsInFile1 + numRecordsInFile2;
dataRecord* ArrayPtr = new dataRecord[totalRecord]; // <---Changed. Needs the name of the struct not the variable name.
// <--- Added. Used for testing.
std::cout << "\n Number of records in file1 = " << numRecordsInFile1 << "\n Number of records in file2 = " << numRecordsInFile2 << std::endl;
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue";
std::cin.get();
delete[] ArrayPtr; // <--- need to delete what you created.
return 0;
}
int readHeader(std::ifstream& inFile) // <--- Works with one file stream only.
{
int NumRecordsInFile{};
inFile >> NumRecordsInFile;
return NumRecordsInFile;
}
|
In the above code I had to guess at the header files you might have used plus the others I added for code I added.
I do not know if you used a class or a struct, so I used a struck for now The variables are just a guess for now.
The section for "Proto Types" is needed when the function follows "main". If the function was before "main" this section would not be needed.
It may be my preference, but I like to put the variable definitions at the beginning of a function so I know where they are. Defining variables just before they are needed can make them hard to find.
You will see that I moved the open statements before the if statements that check to see if they are open. This allows the if statements to have something to check. I also corrected the open statements to the way they should be used.
As explained by dhayden lines 48 and 49 each call the function which returns the number of records in the file. In the future I would write a function that reads these files that gets the information.
Line 54 is the proper way to do this. The comments explains why.
Lines 57 to 66 I added. You may not need all of this and line 57 can be commented out when finished using it.
It would be helpful to have the full code so everyone can be working with the same information. The same is true for the data files.
Hope that helps,
Andy