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;
}
|