如何用空格读取名称? [英] How to read names with space?

查看:49
本文介绍了如何用空格读取名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这样的数据:

  Michael 30汤姆35 

我可以通过以下代码来处理它:

  #include< iostream>#include< string>int main(){std :: string名称;年龄while(std :: cin>名称>>年龄){std :: cout<<名称<<是<<年龄<<岁."<<std :: endl;}返回0;} 

但是,如果我的数据看起来像这样:

  Michael Corleone30汤姆·哈根(Tom Hagen)35 

我尝试使用此代码,但是它只读取第一行,逻辑上似乎应该读取所​​有行,所以我不知道为什么这种尝试会失败.

  #include< iostream>#include< string>int main(){std :: string名称;年龄while(std :: getline(std :: cin,name)&&std :: cin>> age){std :: cout<<名称<<是<<年龄<<岁."<<std :: endl;}返回0;} 

更糟糕的是,如果我的数据看起来像这样:

  Michael Corleone 30汤姆·哈根35 

在提出建议的答案之后,我可以将名称分为 name1 name2 :

  int main(){std :: string name1,name2;年龄while(std :: cin> name1>> name2>>年龄){std :: cout<<名称1 +''+名称2<<是<<年龄<<岁."<<std :: endl;}返回0;} 

但是,这不好,例如,如果有人使用中间名怎么办?

解决方案

使用此数据查看第二个示例:

  Michael Corleone30汤姆·哈根(Tom Hagen)35 

问题在于,使用 std :: cin>>读取 age 后,age 行尾字符仍然留在流中,因此以下 std :: getline 不会得到任何结果,因为它认为该行是空的.>

诀窍是在读取每个新记录之前跳过空格:

  std :: string名称;年龄while(std :: cin>> std :: ws&& std :: getline(std :: cin,name)& std :: cin>> age){std :: cout<<名称<<是<<年龄<<岁."<<std :: endl;} 

或更简洁地说:

  std :: string名称;年龄while(std :: getline(std :: cin>> std :: ws,name)>> age){std :: cout<<名称<<是<<年龄<<岁."<<std :: endl;} 

If I have the data look like this:

Michael 30
Tom 35

I can handle it by the following code:

#include <iostream>
#include <string>
int main()  {
    std::string name;
    int age;
    while(std::cin >> name >> age)  {
        std::cout << name << " is " << age << " years old." << std::endl;
    }
    return 0;
}

However if my data looks like this:

Michael Corleone
30
Tom Hagen
35

I tried using this code, but it only read the first line, the logic seems that it should read all the lines, so I don't know why this attempt fails.

#include <iostream>
#include <string>
int main()  {
    std::string name;
    int age;
    while(std::getline(std::cin, name) && std::cin >> age)  {
        std::cout << name << " is " << age << " years old." << std::endl;
    }
    return 0;
}

Even worse, if my data look like this:

Michael Corleone 30
Tom Hagen 35

Follow the suggestion by one of the answers, I can have a workaround by splitting the name into name1 and name2:

int main()  {
    std::string name1, name2;
    int age;
    while(std::cin >> name1 >> name2 >> age)  {
        std::cout << name1 + ' ' + name2 
            << " is " << age << " years old." << std::endl;
    }
    return 0;
}

However, this is not nice, what if someone have a middle name for example?

解决方案

Looking at the second example with this data:

Michael Corleone
30
Tom Hagen
35

The problem is that after reading the age with std::cin >> age the end-of-line character is still left in the stream so the following std::getline gets nothing because it thinks the line is empty (which it is).

The trick is to skip whitespace before reading each new record:

std::string name;
int age;
while(std::cin >> std::ws && std::getline(std::cin, name) && std::cin >> age) {
    std::cout << name << " is " << age << " years old." << std::endl;
}

Or, more succinctly:

std::string name;
int age;
while(std::getline(std::cin >> std::ws, name) >> age) {
    std::cout << name << " is " << age << " years old." << std::endl;
}

这篇关于如何用空格读取名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆