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 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
#include <iostream>
#include <string>
#include <iomanip>
#include <cctype>
using namespace std;
//overloaded input functions
char input(const std::string& prompt, const std::string& error);
double input(double max_num, double min_num, const string& prompt, const string& error_text);
//void input(int pointx, int pointy, string prompt);
//overloaded distance functions
//int distance(char a, char b);
//double distance(double num1, double num2);
//double distance(double x1, double y1, double x2, double y2);
//Overloaded display functions
//void display(const string& msg, char ch1, char ch2);
//void display(const string& msg, double num1, double num2);
//void display(const string& msg, double x1, double y1, double x2, double y2);
int main() {
cout << "Display the function between two items: letters, numbers, points.\n";
char options {};
do {
cout << "Options: l)etter; n)umber; p)oint; q)uit: ";
cin >> options;
options = static_cast<char>(std::tolower(static_cast<unsigned char>(options)));
switch (options) {
case 'l':
{
const string error {"Sorry, out of range. Try again."};
const auto letter1 {input("Enter first letter (a to z): ", error)};
const auto letter2 {input("Enter second letter(a to z): ", error)};
std::cout << letter1 << ' ' << letter2 << '\n';
}
break;
case 'n':
{
const string error_text {"Sorry, out of range. Try again."};
const auto num1 {input(100, -100, "Enter first number (100 to -100): ", error_text)};
const auto num2 {input(100, -100, "Enter second number (100 to -100): ", error_text)};
std::cout << num1 << ' ' << num2 << '\n';
}
break;
case 'q':
break;
default:
std::cout << "Invalid option\n";
break;
}
} while (options != 'q');
}
char input(const std::string& prompt, const std::string& error_message) {
char letter {};
while ((std::cout << prompt) && (!(std::cin >> letter) || !std::isalpha(letter))) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << error_message << '\n';
}
return letter;
}
double input(double max_num, double min_num, const string& prompt, const std::string& error_text) {
double num {};
do {
std::cout << prompt;
if ((std::cin >> num) && (num >= min_num) && (num <= max_num) && std::isspace(std::cin.peek())) {
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return num;
}
std::cout << error_text << '\n';
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
} while (true);
}
|