I need help with arrays and structures.

Hello cplusplus community. I was trying to solve a little problem from a tutorial to learn c++ and i've come to an impassable wall (for me of course :D).
In the assignment we had to create a small database using structures and arrays and then search through it.

My problem comes when i try to assign names to the arrays. I get this error line: incompatible types in assignment of 'const char [5]' to 'char [20]'
And it variates depending on the length of the name.

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
 struct persona
{
	char nombre[20];
	int telefono;
} lista[10];

int main ()
{
	// asignacion de valores a la estructura
	lista[1].nombre = "jose";
	lista[1].telefono = 12345678;
	lista[2].nombre = "juan";
	lista[2].telefono = 34265891;
	lista[3].nombre = "federico";
	lista[3].telefono = 64817592;
	lista[4].nombre = "facundo";
	lista[4].telefono = 12363852;
	lista[5].nombre = "asdfjklñ";
	lista[5].telefono = 58924719;
	lista[6].nombre = "gabriel";
	lista[6].telefono = 52758195;
	lista[7].nombre = "marcos";
	lista[7].telefono = 78298761;
	lista[8].nombre = "pelotudo";
	lista[8].telefono = 42402098;
	lista[9].nombre = "hola";
	lista[9].telefono = 39684489;
	lista[10].nombre = "lalala";
	lista[10].telefono = 64573892;


It is written in spanish but i don't think there is a problem with that.

Can you help me?
That is not the assignment that you are looking for.

What you try to do now is essentially:
1
2
3
const char * a;
char * b;
b = a;

What you should do is to copy elements from one array into an another. Check <cstring> for C-string copying functions. Better yet, replace char arrays with std::string. It has a copy assignment operator.
The differences between the data type "const char[5]" and the data type "char[20]" seem negligible to humans, but to the compiler, trying to set a variable of date type "const char[5]" to a variable of data type "char[20]" makes about as much sense as
int x='a';

"jose" is const char[5] because the string literal "jose" will always be "jose".

I ran into the same problem when I started programming and the explanation didn't make sense to me until much later, but fortunately the fix is simple.

Look at this function: http://www.cplusplus.com/reference/cstring/strcpy/
Correctly implementing it in your code would look like
1
2
3
strcpy(lista[1].nombre,"jose");
lista[1].telefono=1234567;
strcpy(lista[2].nombre,"juan");

etc...
Last edited on
Thanks guys for the help, that fixed my problems.

I have one question. Keskiverto said that i should replace char arrays with std::string. is that using the function strcpy?

I also have another problem with this little program and i can't find where it is.

After fixing the const char issue i ran the program but i failed to do so. When i search for any name or telephone number [except for the first one in the list(jose - 12345678)] i get an infinite amount of prints with the results.

Can you help me again?

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//creado por fedelway
#include <string.h> //para la funcion strcmp y strcpy
#include <stdlib.h> //para la funcion exit
#include <iostream> //para cout y cin
using namespace std;

//prototipos de funciones
void buscarN(char name[20]);
void buscarT(int t);

//creación de estructura.
struct persona
{
	char nombre[20];
	int telefono;
} lista[10];

int main ()
{
	// asignacion de valores a la estructura
	strcpy(lista[1].nombre,"jose");
	lista[1].telefono = 12345678;
	strcpy(lista[2].nombre,"juan");
	lista[2].telefono = 34265891;
	strcpy(lista[3].nombre,"federico");
	lista[3].telefono = 64817592;
	strcpy(lista[4].nombre,"facundo");
	lista[4].telefono = 12363852;
	strcpy(lista[5].nombre,"asdfjklñ");
	lista[5].telefono = 58924719;
	strcpy(lista[6].nombre,"gabriel");
	lista[6].telefono = 52758195;
	strcpy(lista[7].nombre,"marcos");
	lista[7].telefono = 78298761;
	strcpy(lista[8].nombre,"pelotudo");
	lista[8].telefono = 42402098;
	strcpy(lista[9].nombre,"hola");
	lista[9].telefono = 39684489;
	strcpy(lista[10].nombre,"lalala");
	lista[10].telefono = 64573892;
	
	//declaración de variables
	char l;
	char name[20];
	int t;
	
	//impresión de menú
	cout << "Como quiere buscar a las personas?" << endl;
	cout << "a) Buscar por nombre." << endl;
	cout << "b) Buscar por numero de telefono." << endl;
	cout << "c) Salir del programa." << endl;
	cout << endl;
	
	//elección de opción
	
	cin >> l;
	
	if (l == 'a'){
		cout << "Introduzca el nombre:";
		cin >> name;
		buscarN(name);
	}
	else if(l == 'b'){
		cout << "Introduzca el telefono:";
		cin >> t;
		buscarT(t);
	}
	else if(l == 'c'){
		exit(0);
	}
	else
	{
		cout << "introduzca una de las opciones mencionadas" << endl;
	}
	
	return 0;
}

//definiciones de funciones
void buscarN(char name[20])
{
	//variables necesarias para la función
	bool verdad[10] = {false};
	int n;
	
	for (n = 1; n<=10; n++)
	{
		if (strcmp(name,lista[n].nombre) == 0)
		{
			cout << "el telefono de la persona es: " << lista[n].telefono;
		}
		else verdad[n] = true; //para verificar que el nombre de la persona no existe
	}
	
	if (verdad[1] == verdad[2]){
		if (verdad[2] == verdad[3]){
			if (verdad[3] == verdad[4]){
				if (verdad[4] == verdad[5]){
					if (verdad[5] == verdad[6]){
						if (verdad[6] == verdad[7]){
							if (verdad[7] == verdad[8]){
								if (verdad[8] == verdad[9]){
									if (verdad[9] == verdad[10]){
										if (verdad[10] == true){
											cout << "el nombre ingresado no existe.";
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	
	exit(0);
}

void buscarT(int t)
{
	int n;
	bool verdad[10] = {false};
	
	for (n = 1; n<=10;n++)
	{
		if (t == lista[n].telefono)
		{
			cout << "el nombre de la persona es: " << lista[n].nombre;
		}
		else verdad[n] = true;
	}
	
	if (verdad[1] == verdad[2]){
		if (verdad[2] == verdad[3]){
			if (verdad[3] == verdad[4]){
				if (verdad[4] == verdad[5]){
					if (verdad[5] == verdad[6]){
						if (verdad[6] == verdad[7]){
							if (verdad[7] == verdad[8]){
								if (verdad[8] == verdad[9]){
									if (verdad[9] == verdad[10]){
										if (verdad[10] == true){
											cout << "el telefono ingresado no existe.";
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	
	exit(0);
}


I feel like i'm kinda useless, i need help with almost everything. Does everyone need so much help?
Last edited on
No, using std::string, you would just have to do
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <string>

struct persona
{
    std::string nombre; // Change this to std::string
    int telefono;
} lista[10];

int main()
{
    // asignacion de valores a la estructura
    lista[1].nombre = "jose"; // This works like you would expect -- no strcpy needed
    lista[1].telefono = 12345678;
    lista[2].nombre = "juan";
    lista[2].telefono = 34265891;
    // etc. 
Topic archived. No new replies allowed.