Hello LuffyD,
In addition to what
salem c has said and your last post this is what I see.
If you want to start at (0) zero than say so. I would use:
1 2 3 4 5 6
|
listType() //constructor of the class
{
arr = nullptr;
j = 0;
size= 4;
}
| |
Setting "size" to 4 is because of the 4 "infile.ignore()" statements. You need to account for these numbers if you want "size" to be the size of the file.
Line 29 should open the file, but how do you know? You never check. Consider this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
bool readDataFile()
{
const std::string inFileName{ "dataFile1.txt" };
int num{};
std::ifstream inFile(inFileName);
if (!inFile)
{
std::cout << "\n File " << std::quoted(inFileName) << " did not open" << std::endl;
//std::this_thread::sleep_for(std::chrono::seconds(3)); // <--- Needs header files chrono" and "thread". Optional.
return true;
}
// <-- The last statement before the closing } of the function should be:
return false;
}
| |
The "std::quoted" comes from the "<iomanip>" file and I think it makes a nicer output, but this is your choice. The "int num{};" I will get to shortly.
On line 30 you use "new" to create dynamic memory for you array, but you never use "delete" to free that memory. If I understand correctly this is a memory leak.
I would use the default dtor of the class to delete the memory.
My first use of "num" would be
inFile >> num >> num >> num >> num;
. this would read the first 4 numbers in the file.
If you insist on using the ".eof()" function in the while loop then you need to learn how to us it properly. What you are doing is not working the way that you think it is. The first 4 reads did not set the "eof" bit, so you enter the while loop and read the next number into the array, maybe, that would depend on the value of "j". When you finish the while loop you go back to the while condition where is finds that you have not set the "eof" bit and enters the while loop again. The read now tries to read, but finds that you are at the end of the file and sets the "eof" bit, but continues to process what is in the while loop making "j" and "size" one larger than they should be. And since nothing was read from the file there is no idea what was stored in the array, but the array has a size of 1, so this second time through the loop you are storing the unknown value to some other bit of memory that is outside the array boundaries. Of course that would depend on "j" having a proper value to start with.
To use what you have you should do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
inFile >> num >> num >> num >> num;
inFile >> num; // <--- Could be put at the end of the above line. I did it this way to make a point.
while (!inFile.eof())
{
arr[j] = num; //getting the data from the file
j++;
size++; //getting the size of the file
inFile >> num;
}
| |
The first read is before the while loop so that the condition can work properly.
Reading the file at the end of the while loop will allow the while condition to check for "eof" at the proper time and the loop fail at the proper time.
The simpler solution is what
salem c
has suggested.
In the "print" function if you use "size" the way that you want it would cause the for loop to iterate more times than it should. The size of the array is 1, so the for loop should not loop more than one time.
Since I said that the "read" function should return a bool "main" would work like this:
1 2 3 4 5 6 7 8 9 10
|
int main()
{
listType<int>a; //specifying the type for the template
if (a.readDataFile() //calling read file
return 1;
a.print(); // printing
}
| |
Zero is considered a normal or successful return any number greater than zero is considered a problem.
The if statement is working off the return value of the function.
Andy