Apr 2, 2022 at 11:07am Apr 2, 2022 at 11:07am UTC
Basically, I am trying to do a very simple ATM.
A user can input any symbol on the keyboard, but as int op is an integer, I only want to allow them to input a number and nothing else.
Everything works, except for one thing - when user inputs more than one symbol (for example, 4 letters), cmd types out "cout << endl << endl << "Wrong symbol. " << endl;" for all 4 of the symbols.
How do I make them count as one symbol so it types out the line above only one time?
Sorry if I phrased it pretty harshly.
Thanks in advance!
P.S. this for loop is not a finished result, it is just a small part.
exitloop = 1;
int op;
for (int i = 0; exitloop == 1; i++) {
cout << "Galimi operaciju variantai: ";
cin >> op;
if (cin.fail()) {
cout << endl << endl << "Wrong symbol. " << endl;
cin.clear();
cin.ignore();
}
}
Apr 2, 2022 at 12:05pm Apr 2, 2022 at 12:05pm UTC
Well post your latest code.
Apr 2, 2022 at 12:48pm Apr 2, 2022 at 12:48pm UTC
Try something like this:
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
#include <iostream>
#include <string>
#include <type_traits>
#include <typeinfo>
#include <cctype>
#include <limits>
template <typename T = int >
typename std::enable_if_t< std::is_arithmetic_v<T>, T>
getInp(const std::string& prm) {
const auto notsp {[&]() {while (std::isspace(static_cast <unsigned char >(std::cin.peek())) && std::cin.peek() != '\n' ) std::cin.ignore(); return std::cin.peek() != '\n' ; }};
T n {};
while ((std::cout << prm) && (!(std::cin >> n) || notsp())) {
std::cout << "Invalid input. Not a(n) " << typeid (T).name() << '\n' ;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n' );
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n' );
return n;
}
int main() {
const auto op {getInp("Possible operation options: " )};
std::cout << "Entered " << op << '\n' ;
}
Last edited on Apr 2, 2022 at 1:43pm Apr 2, 2022 at 1:43pm UTC
Apr 2, 2022 at 1:32pm Apr 2, 2022 at 1:32pm UTC
Or as an alternative, possibly:
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
#include <iostream>
#include <string>
#include <type_traits>
#include <typeinfo>
#include <cctype>
#include <charconv>
#include <algorithm>
template <typename T = int >
typename std::enable_if_t< std::is_arithmetic_v<T>, T>
getInp1(const std::string& prm) {
T num {};
for (std::string inp;;) {
std::cin.clear();
std::cout << prm;
if (std::getline(std::cin, inp) && !inp.empty()) {
const auto first {std::find_if(inp.begin(), inp.end(), [](unsigned ch) {return !std::isspace(ch); })};
const auto end {std::find_if(inp.rbegin(), inp.rend(), [](unsigned ch) {return !std::isspace(ch); }).base()};
const auto res {std::from_chars(std::to_address(first), std::to_address(end), num)};
if (res.ec == std::errc {} && res.ptr == std::to_address(end))
return num;
}
std::cout << "Invalid input. Not a(n) " << typeid (T).name() << '\n' ;
}
}
int main() {
const auto op {getInp1("Possible operation options: " )};
std::cout << "Entered " << op << '\n' ;
}
Last edited on Apr 2, 2022 at 1:43pm Apr 2, 2022 at 1:43pm UTC