I have a 2d array with 5 strings
Ex: blueone greentwo redthree pinkfour yellowfive
Now I want to parse these strings and get the numbers. "one", "two", "three", "four", "five"
Then convert these strings to there numerical values. 1,2,3,4,5. How would I do this?
I using a struct for those numbers

``````struct example{
int numbers;
};

char array[5][2] = { "blueone", "greentwo", "redthree", "pinkfour", "yellowfive" };

example parse;
``````

EDIT: This what I'm working with:

``````for( int i = 0; i < 5; i++ )
{
deck[i].numbers = array[i][0];

}
``````

The output: 98 103 114 112 121
These are the ascii decimals for the letters b g r p y.

This makes no sense..

What do you mean:

Then convert these strings to there numerical values. 1,2,3,4,5. How would I do this?

Do you mean the the place where they are in the array? i.e "0", "1"....

Or do you mean:

`blueone` then becomes `blue`=>`one` and …

You should really use strings for this.

It would therefore be:

``````std::string v = "blueone";

std::string toFind = "one";

if(v.find(toFind) != string::npos)
{
std::cout << "Found";

/* you could then do something like:

if(toFind[i....] == "one")
{
int value = 1;
}
*/

}
``````

You'd need …

## All 5 Replies

This makes no sense..

What do you mean:

Then convert these strings to there numerical values. 1,2,3,4,5. How would I do this?

Do you mean the the place where they are in the array? i.e "0", "1"....

Or do you mean:

`blueone` then becomes `blue`=>`one` and then from there, store "1" and `green`=>`two` etc..?

I mean that blue(one) = 1 green(two) = 2 ...

You should really use strings for this.

It would therefore be:

``````std::string v = "blueone";

std::string toFind = "one";

if(v.find(toFind) != string::npos)
{
std::cout << "Found";

/* you could then do something like:

if(toFind[i....] == "one")
{
int value = 1;
}
*/

}
``````

You'd need to have two seperate arrays, then using recursion so that each element is checked with each of the array values you want to find.

Hope this helps

You declared you array wrong.

char array[5][2] = { "blueone", "greentwo", "redthree", "pinkfour", "yellowfive" };

This declaration says that there are 5 elements that themselves are arrays consisting 2 elements.

You can declare them like this:

`char (asterisk character) array[] = { "blueone", "greentwo", "redthree", "pinkfour", "yellowfive" };`

NOTE: I can't type a literal asterisk here. It is being interpreted as the italic formatting code.

NOTE: I can't type a literal asterisk here. It is being interpreted as the italic formatting code.

Use code or inline code tags and asterisks won't be interpreted as Markdown. If you want plain text, you can escape any markdown special character with a backslash.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.