### 2D array Hi guys,
In the following code, the variable adjmatrix doesn't take the value I want it to take. 'Procedure' is the name of a struc t and procedure[].process_node1 is of type char. procedure[].process_node2 is also of type char. procedure[].process_consomation is of type float.'Node' is the name of a struct and node[].milestone_nom is of type char.

For example: procedure.process_node1 = 'Start' and node.milestone_nom ='Start'. procedure.process_node2 = 'A' and node.milestone_nom.
As the value of procedure.process_consomation is equal to -3.88 ,
the values of adjmatrix and adjmatrix should be equal to -3,88. But this code outputs 0 as value for adjmatrix and adjmatrix

 ``1234567891011121314`` ``````float adjmatrix ; for (int a=0; a<16; a++){ for (int b=0; b<12; b++){ if (procedure[a].process_node1 == node[b].milestone_nom){ for (int c=0;c<12;c++){ if (procedure[a].process_node2 == node[c].milestone_nom) { adjmatrix[b][c]= procedure[a].process_consomation; adjmatrix[c][b]= procedure[a].process_consomation; } } } } }`````` process_node1 is a char you say? But you're trying to assign it the value 'Start'.
You need to turn on warnings, because you're probably getting a warning like
 warning: character constant too long for its type warning: overflow in implicit constant conversion [-Woverflow]

A character literal containing more than one character is implementation-defined behavior, which in this case means "don't do that".

If you need to store a word like "Start", use a string.
 ``123456789101112131415`` ``````// Example program #include #include int main() { std::string arr; arr = "Start"; // notice: double-quotes if (arr == "Start") { std::cout << "Start!\n"; } }``````
Last edited on > For example: procedure.process_node1 = 'Start' and node.milestone_nom ='Start'.
What type are those variables containing strings?

If you want to use ==, then they need to be std::string.

If this is C, or you're using plain-old char arrays, then you need to use strcmp()

Of course, you should be using the debugger and putting a breakpoint on line #7 to see if that code is ever executed.

Or put a breakpoint on #4 and start single-stepping the code until it does something you didn't expect.

Debuggers are marvellous tools. I defined both structures as :

 ``12345678910111213141516`` ``````struct milestone { int milestone_x; int milestone_y; char milestone_nom; }; struct process { char process_nom; float process_consomation; char process_node1; char process_node2; }; process procedure ``````

So 'start' should fit in char milestone_nom , isn't it?

Last edited on process_node1 is not a char, it's an array of chars. This is an important difference.

With this information, follow what salem c said and either use std::strings, or use strcmp to compare "c-strings" (null-terminated character arrays).
http://www.cplusplus.com/reference/cstring/strcmp/
Last edited on > So 'start' should fit in char milestone_nom , isn't it?
as ganado said, `'start'` is «implementation-defined behavior, which in this case means "don't do that".»
what you want is `"start"`
then I wonder how did you set the value, ¿strcpy()?

http://www.cplusplus.com/forum/general/259503/
following that, I doubt that you even need a char array, may simply translate "Start" and "End" to 1 and 2.

 ``123456`` ``````float adjmatrix ; for (int a=0; a<16; a++){ int b = find_index(node, procedure[a].process_node1); int c = find_index(node, procedure[a].process_node2); adjmatrix[b][c] = adjmatrix[c][b] = procedure[a].process_consomation; }``````
find_index() should be easier to debug.
Last edited on
Registered users can post here. Sign in or register to post.