Assign string to char array of struct problem

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
#include <iostream>
using namespace std;

struct box{
    char maker[40];
    float height;
    float weight;
    float length;
    float volume;
};
void display(box *);
void display(box * b){
    cout << b->maker  << endl;
    cout << b->height << endl;
    cout << b->weight << endl;
    cout << b->length << endl;
    cout << b->volume << endl;
}
int main(){
    
    box *b = new box;
    
    b->maker  = "JoJo Adventure"; // i cannot assign string to array
    b->height = 60.5;
    b->weight = 103.4;
    b->length = 200.0;
    b->volume = 100;
    
    display(b);
    
    return 0;    
}


C++Dev.cpp:23: error: incompatible types in assignment of ‘const char [15]’ to ‘char [40]’
change char maker[40]; to std::string maker; and the assignment will work as expected.

(also, you have a memory leak in main. Just write box b; to make a new box)
are there any methods to input string to char array on the case
Last edited on
Topic archived. No new replies allowed.