Nodes with char problem

I have a problem inserting char values in a node, i dont know the reason behind it but it doesnt save the characters at all, anyone know the reason?.

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
typedef struct nodo_actividad{	
	char nombre[25];
	struct nodo_actividad *siguiente, *anterior;
} Actividad;

typedef Actividad *Act;

void IngresoAct(char actividad_ingresada[25]){
        Act nueva_act;
	nueva_act = new(Actividad);
	nueva_act->nombre = actividad_ingresada;   //It doesnt save characters 
        cout << "nueva_act->nombre";
}

cout<<"\n\t\tActividades\n\n";
	cout<<" 1. Ingresar Actividades"<<endl;
        cout<<" 2. Salir	"<<endl;
 
    cout<<"\n Ingrese Opcion: ";

int main()
{
    int opcion_menu;		
    char ingresar_actividad[25];
    		
    do
    {
        menu_estructura();  cin>> opcion_menu;
 
        switch(opcion_menu)
        { 
		case 1:
                 cout<< "\n Actividad a Ingresar:" ; cin >> ingresar_actividad;
                 IngresoAct (ingresar_actividad);
		 break;
		}  
 
        cout<<endl<<endl;
        system("pause");  system("cls");
 
    }while(opcion_menu!=2);
  
   system("pause");
   return 0;
}
It shouldn't even be compiling.

You cannot assign arrays like that. You have 2 options:

1) Use strings instead of char arrays:

1
2
3
4
5
6
#include <string>
...
typedef struct nodo_actividad{	
	string nombre; //<- use a string instead of a char array.
	struct nodo_actividad *siguiente, *anterior;
} Actividad;


or

2) Use strcpy to copy the string data:

1
2
// nueva_act->nombre = actividad_ingresada;  <- works with strings, but not char arrays
strcpy( nueva_act->nombre, actividad_ingresada ); // <- works with char arrays, but not strings 





On a side note... you are doing weird C things (typedef struct) that are unnecessary in C++. But whatever.
So, in short:
1
2
3
4
T foo[7];
T bar[7];

foo = bar; // does not do what you expect 

There is no such assignment from plain array to plain array.

There are string copy functions: http://www.cplusplus.com/reference/cstring/
and std::copy http://www.cplusplus.com/reference/algorithm/

There is also std::string
I already use the strcpy function however I also create a delete function, so when I look for the node that has the activity I want to eliminate it run as nothing happened so thats why Im looking for some answers
You can't use == with arrays. If you have char arrays, you can use strcmp.

Or... again... you could just use strings.
Topic archived. No new replies allowed.