Copying data file to array

I need to copy the data from the following file to a structure array:

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

I need to copy the data to the following structure:

1
2
3
4
5
6
7
8
9
10
struct RECORD
{
	char Name[15];
	int Age;
	float Gpa;
};

RECORD p[N]; float GpaAve;

int main()


Here is the following code I have. My program only outputs the first data line;

Martin Smith/ 22 2.2

but then the rest is just blank

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


using namespace std;

void CopyRecords(string Fname);
void Display();

const int N = 5;

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

RECORD p[N]; float GpaAve;

int main()
{


	// Copy data from file to structure array p 
	CopyRecords("data2.txt");
	
	Display();


	system("PAUSE");
	return 0;
}

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 >> p[i].Gpa;
		cin.ignore(1);
		
	}

	f.close();
}


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;
	}
	cout << endl;

}


I also am experiencing trouble with the toupper function

Currently my program outputs:
Martin Smith 22/2.2
Austin Clinto 0/0 0/0 0/0 0/0 0/0

I urgently need help!

Also, how do I make p.Name all uppercase?
Last edited on
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
78
79
80
81
#include <iostream>
#include <string>
#include <iterator>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>

using namespace std;

// This is our record
struct Record
{
    string name;
    int age;
    float gpa;
};

// How do we read each record
istream& operator >> (istream &s, Record &r)
{
    string line;
    
    if (getline(s, line))
    {
        stringstream ss(line);
        
        // read the name
        getline(ss, r.name, '/');
        // convert to uppercase
        std::transform(r.name.begin(), r.name.end(),
                       r.name.begin(),
                       (int (*)(int))toupper);
        // rest of the data
        ss >> r.age >> r.gpa;
    }
    
    return s;
}

bool copyRecords(vector<Record> &records, const string &fname)
{
    records.clear();
    
    fstream fs(fname.c_str());
    if (fs)
    {
        // copy all the records from file
        copy(istream_iterator<Record>(fs), istream_iterator<Record>(),
             back_inserter(records));
        
        return true;
    }
    
    return false;
}

// How do we print the record
ostream& operator << (ostream &s, const Record &r)
{
    cout << r.name << " " << r.age << "/" << r.gpa;
    return s;
}

void display(const vector<Record> &records)
{
    // copy all the records to screena
    copy(records.begin(), records.end(), ostream_iterator<Record>(cout, "\n"));
}

int main(void)
{
    vector<Record> records;
    
    if (copyRecords(records, "record.txt"))
    {
        display(records);
    }
    
    return 0;
}
Sorry your code contains too many functions and member types which my class has not gone into yet.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 >> p[i].Gpa;
		
		
	}

	f.close();
}


Why does this only copy the first line of data and nothing else?



Also, why does this not work?

1
2
3
4
5
6
7
8
9
10
void Toupper()
{
	for (int i = 0; i < N; i++)
	{
		p[i].Name = toupper(p[i].Name);
	}
	
	//Display the data all uppercase
	Display();
}


It says that argument char is incompatible with parameter type int
The toupper function is used to convert one character to its uppercase form. You can't use it to convert the whole string. You can use a loop or the transform method to convert all the characters.

1
2
3
4
5
6
for (int i = 0; i < N; i++)
{
    int len = strlen(p[i].Name);
    for (int k = 0; k < len; k++)
        p[i].Name = toupper(p[i].Name);
}
Last edited on
I see, but .Name is a char though?
I see, but .Name is a char though?
The Name is a char array. It's not a char.
name is an array of char . Equivalent to a string.
Ah I see. So a normal char array of

char c[4];

this is considered to be a string?

If it's a string can't I use strupr?

1
2
3
4
for (int i = 0; i < N; i++)
	{
		 strupr(p[i].Name);
	}



however it says that:

error C4996: 'strupr': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _strupr.

What does this mean
Just do what it tell you to.
Use _strupr instead of strupr

How to use strupr:
http://www.thinkage.ca/english/gcos/expl/c/lib/strupr.html
Topic archived. No new replies allowed.