-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
50 lines (38 loc) · 1.15 KB
/
main.cpp
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
#include <ios>
#include <iostream>
#include <limits>
#include "power_integer.hpp"
void clearStdin() {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
int getUserInput() {
bool done{false};
int result{};
while (!done) {
std::cout << "Type an integer: ";
std::cin >> result;
if (std::cin.eof()) {
std::cerr << "\nExiting the program ...\n";
std::exit(EXIT_FAILURE);
} else if (std::cin.fail()) {
std::cerr << "Invalid input! Please type a valid integer.\n";
} else if (std::cin.peek() != '\n') {
std::cerr << "Invalid input! Please type valid digits only.\n";
} else {
done = true;
}
clearStdin();
}
return result;
}
int main() {
std::cout << "Welcome to this simple exponent calculator!\n";
const int base{getUserInput()};
const int exponent{getUserInput()};
auto result{pow_int(base, exponent)};
if (result.has_value()) {
std::cout << base << " to the power of " << exponent << " is: " << result.value() << ".\n";
}
return EXIT_SUCCESS;
}