【问题标题】:How do I disallow cin from forwarding to the next input if I enter more than one word per cin?如果每个 cin 输入多个单词,如何禁止 cin 转发到下一个输入?
【发布时间】:2015-06-15 16:58:45
【问题描述】:

在该程序的每个输入中,我希望用户能够输入他们想要的任意数量的“单词”(意思是由空格/制表符分隔的文本)......但不允许用户输入单词超出当前的 cin。例如:如果我输入 John Smith Doe,我希望将 John 放入“name”中,并丢弃 Smith 和 Doe。然后我想输入“年龄”和“体重”,而不需要让 Smith 变老而 Doe 变重。目前,如果我输入 John Doe 作为名称,那么年龄将变为 Doe。如果我输入 John Smith Doe 作为名称,则年龄变为 Smith,体重变为 Doe。 有人可以帮忙吗?

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
    string name;
    string age;
    string weight;

    //user can enter more than one name (title, first, middle, last, etc.)
    cout << "Enter your name: " << endl;
    cin >> name;

    //if user entered two or more names above,
    //disallow cin from putting name two into 'age'
    cout << "Enter your age: " << endl;
    cin >> age;

    //same thing for weight
    cout << "Enter your weight: " << endl;
    cin >> weight;

    cout << "You typed: " << endl;
    cout << "Name: " << name << endl;
    cout << "Age: " << age << endl;
    cout << "Weight: " << weight << "." << endl;

    return 0;
}

【问题讨论】:

  • 你想要 std::getline(stream, string)
  • 你只需ignore它。
  • 当您的代码提示输入姓名时,您的代码是否跳过了“标题”输入?为什么您的 cmets 使用“first”、“middle”、“last”而不是“name”?也许你的提示是不够的。你能改进你的需求陈述吗?
  • 这是最好的答案...特别是:getline(stream, string)。

标签: c++


【解决方案1】:

我认为你可以使用

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

例如在cin &gt;&gt; name;之后。

【讨论】:

  • 沿着这些思路,但更标准一些cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max())
  • 这将跳过一行,如果换行符跟在 'name' 后面
  • @DieterLücking,你是对的。我将编辑我的答案
【解决方案2】:

逐行读取流,将每一行传递给 istingstream 并读取感兴趣的字段:

// Read Fields [Teminating State]
inline std::istream& read_fields(std::istringstream& line_stream)
{
    return line_stream;
}

// Read Fields
template <typename Arg, typename ... Args>
inline std::istream& read_fields(std::istringstream& line_stream, Arg& arg, Args&... args)
{
    if(line_stream >> arg)
        read_fields(line_stream, args...);
    return line_stream;
}

// Read and Skip Fields of a Line
template <typename ... Args>
std::istream& read_line(std::istream& stream, Args& ... args)
{
    std::string line;
    if(std::getline(stream, line)) {
        std::istringstream line_stream(line);
        read_fields(line_stream, args...);
        if(line_stream.fail()) {
            // Pass the fail state to the stream.
            stream.setstate(std::ios_base::failbit);
        }
    }
    return stream;
}

// Test
int main()
{
    std::string name;
    read_line(std::cin, name);

    std::string age;
    std::string weight;
    read_line(std::cin, age, weight);

    if(std::cin)
        std::cout << "Name: " << name << ", Age: " << age << "Weight: " << weight << '\n';
    else
        std::cout << "Failure\n";
    return 0;
}

【讨论】:

    【解决方案3】:

    这是我开发的解决方案。感谢您的帮助!

    string name;
    string age;
    string weight;
    
    cout << "Enter your name: " << endl;
    //this line allows me to take in a full line (up
    //to the newline character) at once
    getline(cin, name);
    //then i search for the first occurence of a 
    //spacebar, or output the whole line if none
    //is found
    int i = 0;
    int len = name.length();
    for(; i < len; i++)
    {
        if(name[i] == ' ')
        {
            break;
        }
    }
    //chop off end of variable 'name'
    //after first spacebar or leave 
    //it whole if no spacebar was
    //found
    name = name.substr(0, i);
    
    //same thing as above for here
    cout << "Enter your age: " << endl;
    getline(cin, age);
    i = 0;
    len = age.length();
    for(; i < len; i++)
    {
        if(age[i] == ' ')
        {
            break;
        }
    }
    age = age.substr(0, i);
    
    //ditto for here
    cout << "Enter your weight: " << endl;
    getline(cin, weight);
    i = 0;
    len = weight.length();
    for(; i < len; i++)
    {
        if(weight[i] == ' ')
        {
            break;
        }
    }
    weight = weight.substr(0, i);
    
    cout << "You typed: " << endl;
    cout << "Name: " << name << endl;
    cout << "Age: " << age << endl;
    cout << "Weight: " << weight << endl;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-01
      • 1970-01-01
      • 2014-07-31
      • 2016-02-20
      相关资源
      最近更新 更多