Passing structure array to function

For my homework, I need to create a function called CopyRecords which serves to take the data from a text file and copy it into a structure.

I'm just confused as to what is the data type required to pass by the structure array p?

in my void CopyData function, I need to pass by my RECORD p array, RECORD is a structure, how do I do this?


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
#include <iostream>
#include <array>
#include <fstream>


using namespace std;

const int N = 5;

struct RECORD
{
	char Name[15];
	int Age;
	float Gpa;
};

RECORD p[N]; float GpaAve;

int main()
{
	

	CopyRecords("data2.txt", p);
}

void CopyRecords(string Fname, 
{
	fstream f;
	f.open(Fname, ios::in);

	for (int i = 0; i < 5; i++)
	{
		f >>
	}


}
f>>p[i].name
I dont think you need to pass your structure to your function, just use the one paramater for the file name then as shadowCODE said do f >> p[i].Name
Ok I am having an issue now:

It is saying that .Age and .Name must have a class type structure

The problem is found in the void CopyData function


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
#include <iostream>
#include <array>
#include <fstream>


using namespace std;

void CopyRecords(string Fname);

const int N = 5;

struct RECORD
{
	char Name[15];
	int Age;
	float Gpa;
};

RECORD p[N]; float GpaAve;

int main()
{
	

	CopyRecords("data2.txt");
}

void CopyRecords(string Fname)
{
	fstream f;
	f.open(Fname, ios::in);

	for (int i = 0; i < 5; i++)
	{
		f.getline(p.Name, 15, '/');
		f >> p.Age;
	}


}



Did you forget to put [i] ?
Ah yes! such a simple semantic error, thank you!


I have another problem.

I need to create a Display function which displays the structure array RECORD p

what data type do I pass by? this is incorrect:

Display(p);


void Display()
{

}

Since p and N is declared as global variable, I think its fine to have no parameter
I see, I have another problem haha

I need to make all the Names from the structure variable char Name[15]


it is saying that it cannot convert an argument of char to int, the error is found in the line;

p[i].Name = toupper(p[i].Name);



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Display()
{
	// Make all the names uppercase
	
	for (int i = 0; i < 5; i++)
	{
		p[i].Name = toupper(p[i].Name);
	}

	for (int i = 0; i < 5; i++)
	{
		cout << p[i].Name << " " << p[i].Age << "/" << p[i].Gpa << endl;
	}


}
toupper() doesn't take string as parameter, if you want to make all character uppercase, use loop and process each letter 1 by 1
Last edited on
.Name is a char data type though, and toupper is only used for char data types correct?

What do you mean you have to use a loop to process each letter? if I use a for loop from 0 - 5, doesn't that take;

p[0] of Name = uppercase of p[0] of Name
p[1] of Name = uppercase of p[1] of Name

and so forth


Also, when I try to display each record, it shows the first line of data from the text file correctly, then it just shows a bunch of 0's
Yes you are correct but to upper only take a single char, ii always got an error when trying to use uppercase() with array of char
Yes I think
So .Age and .Gpa doesn't show up ?
I think there is a problem in how I copied the file to my array.

the data file looks like this:

Martin Smith/ 22 2.2
Austin Clinton/18 3.1
Johnson/ 19 2.9
Maggie Jones/ 23 2.3
Tyler W Brown/ 16 3.4


I get the name, age and gpa of only Martin Smith, but the rest does not show.

and my uppercase function is incorrect, i think the problem stems from my copy function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void CopyRecords(string Fname)
{


	fstream f;
	f.open(Fname, ios::in);

	for (int i = 0; i < 5; i++)
	{

		f.getline(p[i].Name, 15, '/');
		f >> p[i].Age;
		
	}

	f.close();
}


^ is this correct to retrieve all the information from the text file?
Last edited on
Ooh use cin.ignore(numeric_limits<streamsize>::max(),'\n') after cin>>p[i].Age;
Sorry but I cannot use that, as we have not studied numeric_limits, <streamsize> and so forth.

I can use cin.ignore but I need different parameters
Just use some number
Topic archived. No new replies allowed.