cin.fail - how to make it count as one symbol

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();
}
}
instead of cin.ignore() which just removes 1 char, you need to use something like:

 
cin.ignore(std::numeric_limits<std::streamsize>::max());


which removes all chars up to a '\n'.

NB. When posting code, please use code tags


[code]
code goes here
[/code]

NB. When posting code, please use code tags

Will do.

So I have tried pasting the line in and it does sort of work, but now it blocks the for loop and doesn't let it do another loop. What should I do differently?
Well post your latest code.
Doing "I only want numbers" console input right is not as easy as it seems at first.

http://lb-stuff.com/user-input
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
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
Topic archived. No new replies allowed.