Compare double array with (struct) string

Hi,

I have a double array, and would like to give each member of the array the int (ASCII) value of a string, which is part of a struct. This is how I did it:

1
2
3
4
5
double array[1000];
for(int i = 0; i < blabla; i++)
{
array[i] = string[i].struct;
}


For some reason, this doesn't work. It says the std::string can't be converted to double. However, if I replace the string that's part of a struct with a regular string, it appears to work fine. I have no idea what I'm doing wrong here, and would really appreciate some help.
Anyone?
What type is string? You can't have variables named struct because there is already a keyword with that name.
It's a regular string. The variable isn't actually named struct, sorry, I just put that up there to show it's the name of the struct. But I just realized I didn't write it the right way, this is what I meant:

1
2
3
4
5
double array[1000];
for(int i = 0; i < blabla; i++)
{
array[i] = struct[i].string;
}


It underlines the struct and says there's no suitable conversion function from std::string to double.
Last edited on
Did you mean
1
2
3
4
5
double array[1000];
for(int i = 0; i < blabla; i++)
{
array[i] = Struct.string[i];
}

?
(assuming Struct is the name of your struct and string is the name of your std::string)
Last edited on
Yes. That's what I put in the program, I just misspelled it in the opening post. Any idea what the reason for the error message might be?
Last edited on
Oh, I see what you mean now. No, I have several variables inside the struct, so I'm using an array of structs rather than an array of strings.
Oh, well then I don't exactly understand what you want to do.

You can get the ASCII value of a character, but not a string.
If you want the ASCII value of just the first character of each string:

1
2
3
4
5
double array[1000];
for(int i = 0; i < blabla; i++)
{
array[i] = Struct[i].string[0];
}
Last edited on
I see, that does work. Thanks.

But the purpose of all this is to be able to compare entire strings (for sorting). So I would need more than just the first character, but I'm not sure how to achieve that...
Well, if you're using std::string, you can compare them using the comparison operators:
http://www.cplusplus.com/reference/string/operators/

So you don't even need to mess with ASCII values.
Thanks.
Topic archived. No new replies allowed.