如何使用未知输入类型的cin?

时间:2022-09-06 20:10:53

I have a C++ program which needs to take user input. The user input will either be two ints (for example: 1 3) or it will be a char (for example: s).

我有一个需要用户输入的C ++程序。用户输入将是两个整数(例如:1 3)或它将是一个字符(例如:s)。

I know I can get the twos ints like this:

我知道我可以像这样得到两个这样的:

cin >> x >> y;

But how do I go about getting the value of the cin if a char is input instead? I know cin.fail() will be called but when I call cin.get(), it does not retrieve the character that was input.

但是,如果输入字符,我该如何获取cin的值呢?我知道cin.fail()会被调用但是当我调用cin.get()时,它不会检索输入的字符。

Thanks for the help!

谢谢您的帮助!

2 个解决方案

#1


3  

Use std::getline to read the input into a string, then use std::istringstream to parse the values out.

使用std :: getline将输入读入字符串,然后使用std :: istringstream解析值。

#2


1  

You can do this in c++11. This solution is robust, will ignore spaces.

你可以在c ++ 11中做到这一点。这个解决方案很健壮,会忽略空间。

This is compiled with clang++-libc++ in ubuntu 13.10. Note that gcc doesn't have a full regex implementation yet, but you could use Boost.Regex as an alternative.

这是使用ungntu 13.10中的clang ++ - libc ++编译的。请注意,gcc还没有完整的正则表达式实现,但您可以使用Boost.Regex作为替代方案。

EDIT: Added negative numbers handling.

编辑:添加负数处理。

#include <regex>
#include <iostream>
#include <string>
#include <utility>


using namespace std;

int main() {
   regex pattern(R"(\s*(-?\d+)\s+(-?\d+)\s*|\s*([[:alpha:]])\s*)");

   string input;
   smatch match;

   char a_char;
   pair<int, int> two_ints;
   while (getline(cin, input)) {
      if (regex_match(input, match, pattern)) {
         if (match[3].matched) {
            cout << match[3] << endl;
            a_char = match[3].str()[0];
         }
         else {
            cout << match[1] << " " << match[2] << endl;
            two_ints = {stoi(match[1]), stoi(match[2])};
         }
      }
   }
}

#1


3  

Use std::getline to read the input into a string, then use std::istringstream to parse the values out.

使用std :: getline将输入读入字符串,然后使用std :: istringstream解析值。

#2


1  

You can do this in c++11. This solution is robust, will ignore spaces.

你可以在c ++ 11中做到这一点。这个解决方案很健壮,会忽略空间。

This is compiled with clang++-libc++ in ubuntu 13.10. Note that gcc doesn't have a full regex implementation yet, but you could use Boost.Regex as an alternative.

这是使用ungntu 13.10中的clang ++ - libc ++编译的。请注意,gcc还没有完整的正则表达式实现,但您可以使用Boost.Regex作为替代方案。

EDIT: Added negative numbers handling.

编辑:添加负数处理。

#include <regex>
#include <iostream>
#include <string>
#include <utility>


using namespace std;

int main() {
   regex pattern(R"(\s*(-?\d+)\s+(-?\d+)\s*|\s*([[:alpha:]])\s*)");

   string input;
   smatch match;

   char a_char;
   pair<int, int> two_ints;
   while (getline(cin, input)) {
      if (regex_match(input, match, pattern)) {
         if (match[3].matched) {
            cout << match[3] << endl;
            a_char = match[3].str()[0];
         }
         else {
            cout << match[1] << " " << match[2] << endl;
            two_ints = {stoi(match[1]), stoi(match[2])};
         }
      }
   }
}