如果我在每个cin输入多个单词,我如何禁止cin转发到下一个输入?

时间:2015-06-15 16:58:45

标签: c++

在这个程序的每个输入上,我希望用户能够键入他们想要的许多“单词”(意思是用空格/标签分隔的文字)但是禁止用户输入单词超越现在的cin。例如:如果我键入John Smith Doe,我希望John被置于'name'中,而Smith和Doe将被丢弃。然后我想进入'年龄'和'体重'而不让史密斯去年龄和Doe体重增加。目前,如果我输入John Doe作为名称,那么年龄就变成了Doe。如果我输入John 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;
}

3 个答案:

答案 0 :(得分:1)

我认为你可以使用

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

例如在cin >> name;之后。

答案 1 :(得分:0)

逐行读取流,将每一行传递给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;
}

答案 2 :(得分:0)

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

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;