linking a variable to a part of the program

Hello, this is my first post on these boards, and any help you could give me would be great. I was wanting to be able to set a variable so if entered would link it to a earlier part of the program.

My program atm is:

#include <iostream>
#include<string>
#include <cmath>
#include <iomanip>
#include<fstream>

using namespace std;

int points;
int x[100];
int y[100];
int i;

main()
{
cout << "enter the number of coordinates that you want" << endl;
cin >> points;



for (i=0; i<points; i++)
{
cout << "enter the x" << i+1 << " co-ordinate" << endl;
cin >> x[i] ;
cout << "enter the y" << i+1 << " co-ordinate" << endl;
cin >> y[i] ;

cout << endl << "you have just entered the co-ordinate [" << x[i] << ", " << y[i] << "]" << endl;
}

return 0;
}

I would like to make it so that I can put the option of whether they would like to re-enter the point if they put it in wrong, however I have no idea how to go about doing this.

Any help would be very much appreciated.
put it into a while loop, and loop it until they put in valid information. so for example
1
2
3
4
5
int userInput = -1;
while (/* put your bound in here, between 2 different points, can be whatever you want*/)
{
     // user input information
}


do something along that lines.
This is probable the simplest way:
1
2
3
4
5
6
7
8
9
10
11
12
char check;
for(i=0; i<points; i++){
	do{
	cout << "enter the x" << i+1 << " co-ordinate" << endl;
	cin >> x[i] ;
	cout << "enter the y" << i+1 << " co-ordinate" << endl;
	cin >> y[i] ;
	cout << endl << "you have just entered the co-ordinate [" << x[i] << ", " << y[i] << "]" << endl;
	cout<<"Is that correct? ";
	cin>>check;
	}while(check=='y' || check =='Y');
}
Last edited on
ah i didn't even think of a do while loop. i've always just used a full out while loop, but do while makes sense.

actually what i was thinking for mine was off, lol.

1
2
3
4
5
6
7
8
9
10
char check = 'n';
while (check == 'n' || check == 'N')
{
 	cout << "enter the x" << i+1 << " co-ordinate" << endl;
	cin >> x[i] ;
	cout << "enter the y" << i+1 << " co-ordinate" << endl;
	cin >> y[i] ;
	cout << endl << "you have just entered the co-ordinate [" << x[i] << ", " << y[i] << "]" << endl;
	cout<<"Is that correct (y or n)? "; // want to make sure they know what to input to get out of the loop
}

cheers, that really helped, although confussed me a little when i tested it as you had

"}while(check=='y' || check =='Y');"

and i wanted

}while(check=='n' || check =='N');

:)



actually yeah that is how it is supposed to be, because you want to keep looping if the user types in a n or a N. Make sure that the user knows what to type in, or else s/he might be confused, just put in the (y or n) just so they no to type that. Or you could use a string and instead of y or n is would be yes or no. That's up to you.
Topic archived. No new replies allowed.