检查 C++ 中用户输入的正确值

发布于 2024-10-17 14:11:01 字数 320 浏览 6 评论 0原文

我有一个接受用户输入值的要求。它可以是整数或字符串。接受用户输入如下。

string strArg;
cout << "Enter price value "  << endl;
std::getline(std::cin, strArg);
int input;
std::stringstream(strArg) >> input;

问题是我如何检查用户是否输入了整数而不是字符串,即检查是否正确输入。我们如何在 C++ 中以可移植的方式做到这一点?正如在我的项目中不允许使用 Boost,因为我的经理不高兴使用它:)

I have a requirment that accepts values from user input. It can be integers or strings. User input is accepted as below.

string strArg;
cout << "Enter price value "  << endl;
std::getline(std::cin, strArg);
int input;
std::stringstream(strArg) >> input;

Question is how do i check user has entered integer, rather than string, i.e., check for right input. How can we do this in portable way in C++? As in my project Boost is not allowed as my manager is not happy in using it:)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

凉栀 2024-10-24 14:11:01

没有真正的理由要通过字符串流,除非如果用户输入无效的内容,您不需要清除它,而且您可以将其回显给他们。

int arg;
while(! (cin >> arg) )
{
   cin.clear();
   cin.ignore(numeric_limits<streamsize>::max(), '\n');

   // tell the user their value was invalid
}

// now we have a valid number

如果您允许用户输入整数或字符串,但只是在输入时表现不同是一个字符串,你当然可以使用 istringstream。

请注意,您可能希望在读取数字后检查流是否为空,以防用户输入 123XYZ,如果您只是流式传输,则将被视为“有效”,因为它以整数开头。

There is no real reason to go through stringstream except you do not need to clear it if the user enters something invalid, plus you can echo it back to them..

int arg;
while(! (cin >> arg) )
{
   cin.clear();
   cin.ignore(numeric_limits<streamsize>::max(), '\n');

   // tell the user their value was invalid
}

// now we have a valid number

If you allow the user to enter integers or strings but just behave differently when it is a string you can of course use istringstream.

Note that you may wish to check your stream is empty after reading the number in case the user entered 123XYZ which if you just streamed in would be considered "valid" as it starts with an integer.

彼岸花ソ最美的依靠 2024-10-24 14:11:01

像这样的事情应该做:

#include <iostream>
#include <sstream>
#include <string>

int main(void)
{
  std::cout << "input:" << std::endl; 
  std::string input;
  std::getline(std::cin, input);

  std::istringstream str(input);
  int iv = 0;
  // this checks that we've read in an int and we've consumed the entire input, else
  // treat as string.
  if (str >> iv && str.tellg() == input.length())
  {
    std::cout << "int: " << iv << std::endl;
  }
  else
  {
    std::cout << "string: " << input << std::endl;
  }     

  return 0;
}

Something like this ought to do:

#include <iostream>
#include <sstream>
#include <string>

int main(void)
{
  std::cout << "input:" << std::endl; 
  std::string input;
  std::getline(std::cin, input);

  std::istringstream str(input);
  int iv = 0;
  // this checks that we've read in an int and we've consumed the entire input, else
  // treat as string.
  if (str >> iv && str.tellg() == input.length())
  {
    std::cout << "int: " << iv << std::endl;
  }
  else
  {
    std::cout << "string: " << input << std::endl;
  }     

  return 0;
}
满地尘埃落定 2024-10-24 14:11:01

使用 std::stringstream 和运算符>> 并检查如果转换成功。


像这样的东西:

#include <iostream>
#include <sstream>


int main()
{
    std::stringstream ss;

    ss << "1234";

    int a = 0;
    ss >> a;
    if ( ss.fail() )
    {
        std::cout << "it is not a number" << std::endl;
    }
    else
    {
        std::cout << a << std::endl;
    }
}

Use std::stringstream and operator>> and check if the conversion was successful.


Something like this:

#include <iostream>
#include <sstream>


int main()
{
    std::stringstream ss;

    ss << "1234";

    int a = 0;
    ss >> a;
    if ( ss.fail() )
    {
        std::cout << "it is not a number" << std::endl;
    }
    else
    {
        std::cout << a << std::endl;
    }
}
山有枢 2024-10-24 14:11:01

boost::lexical_cast 与执行以下操作大致相同:

std::stringstream i; i << strArg;
int k; i >> k;

boost::lexical_cast is apprximately the same as doing:

std::stringstream i; i << strArg;
int k; i >> k;
撩发小公举 2024-10-24 14:11:01

您可以使用正则表达式来检查它。据我所知,新的C++标准支持正则表达式

You may use regular expressions to check it. As far as I know, new C++ standart has support of regular expressions

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文