I Need Help with the Display Function!

I am trying to read files then display them on the Display function. Can anyone please tell me what is wrong with this code? I am super confused right now. I have structs as well.

1
2
3
4
5
6
  struct Record
{
	string username;
	string fileName;
	long timeStamp;
};


The problem with the code is that, for some reason, it is not looping through the file then not displaying the file that was read. Please help me!

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
int readFile(const char fileName[], Record records[])
{
	ifstream fin(fileName);
	if (fin.fail())
		return 0;

	int numRecords = 0;

	while (fin >> records[numRecords].fileName
                      >> records[numRecords].username
	              >> records[numRecords++].timeStamp)
  
	fin.close();
	cout << endl;
	return numRecords;
}

void displayRecord(const Record accessRecords[], const long times[],
	const int count)
{
  cout << "The following records match your criteria:" << endl << endl;
  cout << setw(15) << "Timestamp" 
         << setw(20) << "File" 
         << setw(20) << "User" << endl;
  cout << "--------------- ------------------- -------------------"
         << endl;
  
	for (int i = 0; i < count; i++)
	{
		  if (accessRecords[i].timeStamp >= times[0]
			&& accessRecords[i].timeStamp >= times[1])
		  {
			cout << records[i].timeStamp;
                  }
	}
}

Last edited on
1
2
3
4
5
	while (fin >> records[numRecords].fileName
                      >> records[numRecords].username
	              >> records[numRecords++].timeStamp)
            fin.close();
  


You forgot to terminate the while loop that's all.

1
2
3
4
	while (fin >> records[numRecords].fileName
                      >> records[numRecords].username
	              >> records[numRecords++].timeStamp);
  

int readFile(const char fileName[], Record records[])

Also you forgot to pass records by reference.

int readFile(const char fileName[], &Record records[])
Last edited on
Also you forgot to pass records by reference.

Not quite. Recall that the function parameter is a pointer already, so reference-ness isn't required (the pointer does the job.)

The type of function parameters that are arrays of any type T or some function type F are replaced with the corresponding pointer type T* or pointer-to-function type F*.

That is, the declaration
 
int readFile(const char fileName[], Record records[])

Is generally the same as
 
int readFile(const char* fileName, Record* records)


It is not possible to create an array-of-references, so the declaration &Record records[] is a syntax error. It is possible to create a reference-to-array, which looks like T(&records)[N].

See: https://en.cppreference.com/w/cpp/language/function
Last edited on
Ah thanks a lot mbozzi.
Thanks for telling me about the while loop problem. It feels super small but it makes so much difference. However, the display Function is still not working. I just don't get what is wrong with it.
Lines 30-31: It's not entirely clear where times[0] and times[1] come from.
If you're asking the user to enter two times and you looking for the records that are between those two times, then your if statement is wrong. As written, you're checking if the timestamp of a record is >= the first time and also >= second time. If your records are in ascending time order, this condition will be true for all records that are greater than the second time.

What I think you meant is this:
30
31
32
33
34
		  if (accessRecords[i].timeStamp >= times[0]
			&& accessRecords[i].timeStamp <= times[1])
		  {
			cout << records[i].timeStamp;
                  }


BTW, line 33 is also wrong. It should be accessRecords[i].
Last edited on
Topic archived. No new replies allowed.