Error checking

I'm looking to error check some code based off of user input, the code uses user input to calculate the distance according to user input. The only error checking that I am looking to do would be something like a negative number that they would enter. I don't want the error checking to look like a copy and paste statement, so I'm looking for something that would be a little cleaner but still have the same outcome.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  //Header files
#include <iostream>
#include <conio.h>
#include <math.h>
#include <string>

using namespace std;

int main() {
	//Variable declarations
	int x1, x2, y1, y2;
	double distance;
	string name;

	cout << "Hello user" << endl;
	cout << "Before we begin, what is your name?" << endl;
	//Grabs all of users input
	getline(cin, name);
	cout << "Today " << name << " We are going to be calculating distance based off of your input" << endl;
	cout << "What is the value for x1?" << endl;
	cin >> x1;
	cout << "What is the value for x2?" << endl;
	cin >> x2;
	cout << "What is the value for y1?" << endl;
	cin >> y1;
	cout << "What is the value for y2?" << endl;
	cin >> y2;

	cout << "We are now going to calculate your distance according to the numbers you have entered. " << endl;
	//Calculation for distance
	distance = sqrt(pow(x1-x2, 2) + pow(y1-y2, 2));
	//Displays distance to user
	cout << "Your distance is: " << distance << endl;
	//Keeps cmd open until input
	_getch();
	return 0;
}


I'm thinking of using a while statement, but is there any way that I could do it so that I don't have to do it for each variable?
Also what are the advantages of instead of using cout << using std::? I know that doing it the way that I have it now is easier. But is this something that I should switch to for the long run?
Thank you for your input.
Last edited on
Let functions be your friends.

I'll try that out, and post what I come up with. Thank you
What do you think of this so far? Is there any way to make it into one while statement to check all four values? Then only thing I'm looking for is to make sure there's no negative numbers entered in.

Edit: I guess I would have to set num1 = x1 at the end of the while loop to change the value of it, is it better to call the function just once after all of the user input or each time so that if the user enters in something incorrect they're prompted to enter in a correct value as soon as a number like -1 is entered?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//Header files
#include <iostream>
#include <conio.h>
#include <math.h>
#include <string>

using namespace std;

int error_check(int num1, int num2, int num3, int num4);
	
int main() {
	//Variable declarations
	int x1, x2, y1, y2;
	
	double distance;
	string name;

	cout << "Hello user" << endl;
	cout << "Before we begin, what is your name?" << endl;
	//Grabs all of users input
	getline(cin, name);
	cout << "Today " << name << " We are going to be calculating distance based off of your input" << endl;
	cout << "What is the value for x1?" << endl;
	cin >> x1;
	cout << "What is the value for x2?" << endl;
	cin >> x2;
	cout << "What is the value for y1?" << endl;
	cin >> y1;
	cout << "What is the value for y2?" << endl;
	cin >> y2;
	error_check(x1, x2, y1, y2);

	cout << "We are now going to calculate your distance according to the numbers you have entered. " << endl;
	//Calculation for distance
	distance = sqrt(pow(x1-x2, 2) + pow(y1-y2, 2));
	//Displays distance to user
	cout << "Your distance is: " << distance << endl;
	//Keeps cmd open until input
	_getch();
	return 0;
}

int error_check(int num1, int num2, int num3, int num4) {
	while (num1 < 0) {

		cout << "Please enter in a valid number for x1" << endl;
		cin >> num1;
		
	}
	while (num2 < 0) {

		cout << "Please enter in a valid number for x2" << endl;
		cin >> num2;
		
	}

	return num1;
}


	



Last edited on
Here is what I came up with, it changes the values in main if the values were incorrect and if they were correct it keeps the values.

But my question is, is there a way to simplify and condense this code? Essentially my while statements are copy and pastes. I just started using functions again so I had to watch a video on passing by value again as a refresher as well as reading up on it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//Header files
#include <iostream>
#include <conio.h>
#include <math.h>
#include <string>

using namespace std;
	
int error_check(int& num1, int& num2, int& num3, int& num4);

int main() {
	//Variable declarations
	int x1, x2, y1, y2;
	
	double distance;
	string name;

	cout << "Hello user" << endl;
	cout << "Before we begin, what is your name?" << endl;
	//Grabs all of users input
	getline(cin, name);
	cout << "Today " << name << " We are going to be calculating distance based off of your input" << endl;
	cout << "What is the value for x1?" << endl;
	cin >> x1;
	cout << "What is the value for x2?" << endl;
	cin >> x2;
	cout << "What is the value for y1?" << endl;
	cin >> y1;
	cout << "What is the value for y2?" << endl;
	cin >> y2;
	//Function call to check values
	error_check(x1, x2, y1, y2);

	cout << "We are now going to calculate your distance according to the numbers you have entered. " << endl;
	//Calculation for distance
	distance = sqrt(pow(x1-x2, 2) + pow(y1-y2, 2));
	//Displays distance to user
	cout << "Your distance is: " << distance << endl;
	//Keeps cmd open until input
	_getch();
	return 0;
}

int error_check(int& num1, int& num2, int& num3, int& num4) {
	int check = 0;
	while (num1 < 0) {

		cout << "Please enter in a valid number for x1" << endl;
		cin >> num1;
		
	}
	while (num2 < 0) {

		cout << "Please enter in a valid number for x2" << endl;
		cin >> num2;
	}

	while (num3 < 0) {

		cout << "Please enter in a valid number for y1" << endl;
		cin >> num3;
	}

	while (num4 < 0) {

		cout << "Please enter in a valid number for y2" << endl;
		cin >> num4;
	}
	return check;
}


	



closed account (ybf3AqkS)
Here is a way you could keep prompting until you get the input you want

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>
using namespace std;

int get_positive_int(const string& prompt)
{
    int res;

    while(true)
    {
        cout << prompt << '\n';
        cin >> res;
        
        if(!cin) //maybe a character instead of a number was entered
        {
            cin.clear(); //set the state to good
            cin.ignore(99999, '\n'); //remove whatever is in the stream
            continue; //and go to the start of the loop
        }

        cin.ignore(99999, '\n'); //remove stuff typed after the number (if any)

        if(res >= 0) //if non negative number then break and return the value
            break; //exit the loop
    }

    return res;
}

int main()
{
    int x1 = get_positive_int("What is the value for x1?");
    int x2 = get_positive_int("What is the value for x2?");
    int y1 = get_positive_int("What is the value for y1?");
    int y2 = get_positive_int("What is the value for y2?");
}

Last edited on
Hm, that's a lot better than what I came up with. And while mine does work, I think the issue with what I end up writing ends up becoming a copy and paste kinda deal. I'm hoping that will change with more experience.
Topic archived. No new replies allowed.