Loading struct pre-compile error

Lines 82 and 83 are giving errors: No operator "[]" matches these operands operand types are: golf [ int ]. Doesn't like [count].

Thanks,
Bob

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
82
83
84
85
86
87
88
89
90
91
92
// golf.h -- for pe-9.cpp

const int Len = 40;
struct golf
{
	char fullname[Len];
	int handicap;
};
//non-interactive version:
//  function sets golf structure to provided name, handicap
//  using vlues passed as arguments to the function
void setgolf(golf & g, const char * name, int hc);

// interactive version:
// function name and handicap from user
// and sets the members of g to the values entered
// returns 1 if name is entered, 0 if name is empty string
int setgolf(golf & g);

// function resets handicap to new value
void handicap(golf & g, int hc);

// function displays contents of golf structure
void showgolf(const golf & g); 

// chapEx9-1

#include <iostream>
#include "golf.h"
using namespace std;

int main()
{
	golf ann;
	setgolf(ann, "Ann Birdfree", 24);
	showgolf(ann);
	cout << "Enter how many golfers name and handicap do you want to enter: ";
	static int ngolfers;
	cin >> ngolfers;
	while (cin.get() != '\n')
		continue;

	golf * ptr_golf = new golf[ngolfers];

	//int entered = getinfo
	int rint = 1;
	while (rint == 1)
		rint = setgolf(ann);
	showgolf(ann);
}

// golf.cpp load and show golf struc
#include<iostream>
#include "golf.h"

using namespace std;

void setgolf(golf & g, const char * name, int hc)
{
	strcpy_s(g.fullname, name);
	g.handicap = hc;
	// strlen(name)
}

int setgolf(golf & g)
{
	
	static int count = 0;
	static int pos = 0;

	char fname[40];
	cout << "Enter golfers name: ";
	cin.getline(fname, 40, '\n');
	int ihc = 0;
	cout << "Enter golfers handicap: ";
	cin >> ihc;
	
	strcpy(g[count].fullname, fname);
	g[count].handicap = ihc;
	
	++count;

	return 0;
}


void showgolf(const golf & g)
{
	cout << "\nFull name: " << g.fullname << "\n";
	cout << "Handicap: " << g.handicap << "\n";
}
I meant lines 78 and 79.
The type of "g" is a reference to object of type "golf". Type "golf" does not have operator[].


Pointers do support "pointer math":
1
2
3
4
5
T * p = ...
int c = ...

&(p[c]) == p + c;
  p[c]  == *(p + c);

Pointer is not a reference.
Sep 12, 2013 at 9:38pm

Hi, Line 76: cin.get(g.handicap+count);
Pre compile error dot in between cin and get. no instance of overload 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
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
82
83
84
85
86
87
88
89
90
91
92
93
// golf.h -- for pe-9.cpp
const int Len = 40;
struct golf
{
	char fullname[Len];
	int handicap;
};

//non-interactive version:
//  function sets golf structure to provided name, handicap
//  using vlues passed as arguments to the function
void setgolf(golf & g, const char * name, int hc);

// interactive version:
// function name and handicap from user
// and sets the members of g to the values entered
// returns 1 if name is entered, 0 if name is empty string
int setgolf(golf & g);

// function resets handicap to new value
void handicap(golf & g, int hc);

// function displays contents of golf structure
void showgolf(const golf & g);

// chapEx9-1

#include <iostream>
#include "golf.h"
using namespace std;

int main()
{
   golf ann;
	setgolf(ann, "Ann Birdfree", 24);
	showgolf(ann);
	cout << "Enter how many golfers name and handicap do you want to enter: ";
	int ngolfers;
	cin >> ngolfers;
	while (cin.get() != '\n')
		continue;

	golf * ptr_golf = new golf[ngolfers];

	//int entered = getinfo
	int rint = 1;
	while (rint == 1)
		rint = setgolf(ptr_golf);
	showgolf(ann);
}


// golf.cpp load and show golf struc
#include<iostream>
#include "golf.h"

using namespace std;

void setgolf(golf & g, const char * name, int hc)
{
	strcpy_s(g.fullname, name);
	g.handicap = hc;
	// strlen(name)
}

int setgolf(golf & g)
{
	
	static int count = 1;
	static int pos = 0;

	char fname[40];
	cout << "Enter golfers name: ";
	cin.getline(g.fullname+count, 40, '\n');
	cout << "Enter golfers handicap: ";
	cin.get(g.handicap+count);
	cin.get();
	
	++count;

	if (strlen(g.fullname) == 0)
		return 0;
	else
	return 1;
}


void showgolf(const golf & g)
{
	cout << "\nFull name: " << g.fullname << "\n";
	cout << "Handicap: " << g.handicap << "\n";
}
Topic archived. No new replies allowed.