Loading struc with data - fail compile err

Having trouble with this line of code: g.fullname = name; in golf.cpp
Error 1 error C2440: '=' : cannot convert from 'const char *' to 'char [40]'
2 IntelliSense: expression must be a modifiable lvalue

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

// 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);

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

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

// chapEx9-1.cpp

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

int main()
{
	golf ann;
	setgolf(ann, "Ann Birdfree", 24);
}
You probably want to use strcpy() on line line 34. However I'd recommend eschewing C-style strings entirely and just using C++ std::strings.
Thanks with your help. That is exactly what I needed to do.

Bob
Topic archived. No new replies allowed.