### for for if

Hello, I found an error in the code and I do not know how to solve it, I hope for your goodwill.

____________________________

#includes...

{
{
IsActive = true;
X = x;
Y = y;
}

int X,Y;
bool IsActive;
};

{
int xy[2];
for(int n = 0, i = 0; n < 2; n++)
for(; i < str.length(); i++)
if(str[i] >= '0' && str[i] <= '9')
xy[n] = str[i] - 48;

}

int main()
{
string str;
getline(cin,str);

cout << A.X << ' ' << A.Y << endl;

system("PAUSE");
}
____________________________

Ex:
input: "5 7"
output: "7 0" / "7 10" - it depends on the compiler

The final question: why the function does not read the first number that is found as it should.

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
 ``123456789`` ``````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?
 ``12345`` ``````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.
Last edited on
Thanks for the answer to such an idiotic question, as it turned out!
Don't be too hard on yourself lol.