### Why does the code escapes the second loop

So I have a multidimensional array which takes in the users input, the data entered are x and y coordinates of a point .
heres the code
only the loop part
 ``1234567891011`` `````` int poss[2][N]; for(int i = 0; i < N ; i++){ int flag = 0; cout<<"Enter the x and y coordinates for obstace #"<<(i+1)<<" : "; cin>>poss[0][i]>>poss[1][i]; while((checkBounds(poss[0][i]) == 0) || checkBounds(poss[1][i]) == 0){ cout<<"ReEnter the x and y coordinates for obstace #"<<(i+1)<<" : "; cin>>poss[0][i]>>poss[1][i]; } }``````

N is number of points the user wants.
the while loop on line 7 checks for if the entered values are between certain bounds . If the values are not between bounds the
 ``12`` `````` checkBounds()``````

function returns 0 ;
the problem is that after checking once the whole for loop(line 3) is exited which doesn't allow the user to enter next values until i < N
why is this happening ? How can i solve this?
Last edited on
Can you show the definition of checkBounds?
Here it is
 ``1234567`` ``````int checkBounds(int a){ if(r < a && a < R){ return 1; }else{ return 0; } }``````

r and R are radii of two different circles (R > r) and the function checks that int a is in between the two radii
Does this only happen if you enter characters that are not digits? Does it work if you enter numbers that are inside/outside the bounds?
if the numbers are outside the bounds the loop works and asks for a valid input but once a valid input is entered it escapes both the while and for loop.
I tried using continue; but didn't work
Your code works for me. Is N constant?
Technically it is, N is number of points or obstacles user wants. So N is definitely a constant at this point of the porgram
Standart is forbidding thing like that:
 ``123`` ``````int N; cin >> N; int poss[2][N];``````

only this:
 ``12`` ``````const int N = 5; int poss[2][N]; ``````

is allowed. But GCC doesnt raise an error, only warning.
Try to use const variable and tell if it solves the error.

BTW, what compiler are you using?
@MiiNiPaa
That shouldn't cause this problem. If the compiler didn't have that extension it would give an error.

@nishantve1
Can you simplify the program as much as possible so that it still compiles and has the problem you describe. Then it would be much easier to help you. My guess is that the problem is caused by other parts of your program.
@MiiNiPaa
I am using the GNU GCC compiler and yes after declaring N as const the program works. Whats the deal with that?

@Peter87
It works on declaring N a constant . Do I still need to write the simpler version ?
nishantve1 wrote:
It works on declaring N a constant . Do I still need to write the simpler version ?
It's up to you. If that was the only change you made there is probably still an error lurking somewhere that can jump out an bite you later.
@Peter87

heres the simpler version

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657`` ``````#include using namespace std; //function prototypes int checkBounds(int a); int r,R,N; int main() { //The value of r and R is provided by the user but for the sake of simplicity //I am assigning it an inital value here R = 85; r = 25; //Now the program solicits the value of N which again for simplicity ///it cannot be a constant since it is defined by the user, how do I deal with that? N = 2; //Declaring a multidimensional array that holds values of x and y coordinates for N points int pos[2][N]; /* The values are stored in the empty slots below the row ___|_0_|_1_| _0_|___|___| _1_|___|___| _2_|___|___| . . _N_|___|___| */ //ask the user for input for(int i = 0 ; i < N ; i++){ cout<<"Enter the x and y coordinates for point "<<(i+1)<<" : "; cin>>pos[0][i]>>pos[1][i]; while((checkBounds(pos[0][i]) == 0 ) || (checkBounds(pos[1][i]) == 0)){ //the values entered are out of bounds, ask input for second time cout<<"Please reEnter the values for x and y : "; cin>>pos[0][i]>>pos[1][i]; } } cout<<"DONE!"<

This one works fine as it is supposed to . But the code is necessarily the same as previous, whats the deal?
So when you run this simplified version you still have the same problem you describe? Sorry, I can't see anything wrong with it. For me it asks for both coordinates.
 ```Enter the x and y coordinates for point 1 : 35 35 Enter the x and y coordinates for point 2 : 35 35 DONE!```
Like I mentioned above yes, it works fine but the code is necessarily the same as previous , why does this works and the previous one doesn't ?
Then the problem must be in the parts that you have removed.
Yeah , I guess thank you so much for all the help
Topic archived. No new replies allowed.