It does read the first number, but then it overwrites it with the second number.
Let's go through your for loop manually.
n = 0, i = 0, str[0] == '5'
str is between '0' and '9', so xy[0] = 5
n = 0, i = 1, str[1] = ' '
str is a space, so not between '0' and '9'
n = 0, i = 2, str[2] = '7'
str is between '0' and '9', so xy[0] = 5
n = 0, i = 3 --> end of inner for loop
n = 1, i = 3 --.> nothing
end for both for loops.
I would re-format code as
1 2 3 4 5 6 7 8 9
|
int xy[2];
int n = 0;
for (int i = 0; i < str.length(); i++)
if(str[i] >= '0' && str[i] <= '9')
{
xy[n] = str[i] - 48;
n++; // increment n because we found a valid number
}
return Address(xy[0], xy[1]);
|
But... you know this can be much easier right?
1 2 3 4 5
|
int x;
int y;
std::cin >> x;
std::cin >> y;
Address A(x, y);
|
Edit: Oh wait, if you want an input of "57" to become {5, 7}, it won't be too much simpler than your code. But I think just using >> operator is better because it allows you to have address numbers greater than 9.