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
1 2 3 4 5 6 7 8 9 10 11
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
1 2
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?
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
@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.
#include <iostream>
usingnamespace 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!"<<endl;
return 0;
}
int checkBounds(int a){
if(r < a && a < R){
return 1;
}else{
return 0;
}
}
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!