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[0].process_node1 = 'Start' and node[0].milestone_nom ='Start'. procedure[0].process_node2 = 'A' and node[1].milestone_nom.
As the value of procedure[0].process_consomation is equal to -3.88 ,
the values of adjmatrix[0][1] and adjmatrix[1][0] should be equal to -3,88. But this code outputs 0 as value for adjmatrix[0][1] and adjmatrix[1][0]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
float adjmatrix [12][12];
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Example program
#include <iostream>
#include <string>
int main()
{
std::string arr[5];
arr[3] = "Start"; // notice: double-quotes
if (arr[3] == "Start")
{
std::cout << "Start!\n";
}
}
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/
> So 'start' should fit in char milestone_nom[6] , 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()?
float adjmatrix [12][12];
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;
}