c++ - 将不同的变量分配给来自 cin C++ 的输入

标签 c++ string cin

在我的实验室中,我正在尝试处理一条代表地球上目的地的线。这方面的一个例子是......

“加利福尼亚大道 152/N 200/E”

在我教授的笔记中,他说如果你这样做......

std::cin >> latitude >> latitudeDirection >> longitude >> longitudeDirection >> address

一直消耗到加利福尼亚,从那里开始,字符串在每个空白处一次消耗一个。我该怎么做才能消耗其余的输入?这是我的变量在分配时的样子......

latitude = 152
latitudeDirection = "/N"
longitude = 200
longitudeDirection = "/E"
address = "California"

address 只在我希望它包含“California Ave”时包含“California”。这是我到目前为止的代码。

int numberOfLocations;
    std::cin >> numberOfLocations;
    std::cin.ignore();
    for (int x = 0; x < numberOfLocations; x++) {
        double longitude, latitude;
        std::string longitudeDirection, latitudeDirection, address;

        /*
        std::cin >> latitude >> latitudeDirection >> longitude >> longitudeDirection >> address;
        std::cout << latitude << latitudeDirection << latitude << latitudeDirection << address << std::endl;
        */
    }

最佳答案

普通>>作为输入运算符将输入拆分为由空格分隔的“单词”。

为避免这种情况,请考虑每行输入读取一个项目。您可以使用 std::getline来自 <string>阅读一行。在需要数字的地方,您可以使用例如解析该行std::stoi .

如果您别无选择,只能处理包含多项输入的字符串,而最后是可以包含空格的文本数据,那么您可以使用 std::istringstream 阅读正文前的项目。


示例 1 – 每行读取一个项目:

#include <iostream>
#include <stdlib.h>         // exit, EXIT_FAILURE
#include <string>           // std::string
using namespace std;

void error( string const& s )
{
    cerr << "!" << s << endl;
    exit( EXIT_FAILURE );
}

auto get_input_line()
    -> string
{
    string line;
    getline( cin, line );
    if( cin.fail() ) { error( "Uh oh, ..." ); }
    return line;
}

auto main() -> int
{
    cout << "This program adds two numbers A and B." << endl;
    cout << "Number A, please? ";
    double const a = stod( get_input_line() );
    cout << "Number B, please? ";
    double const b = stod( get_input_line() );
    cout << a << " + " << b << " = " << a + b << "." << endl;
}

示例 2 – 解析一行文本
(恕我直言不聪明,但也许是一个要求)

#include <iostream>
#include <stdlib.h>         // exit, EXIT_FAILURE
#include <string>           // std::string
#include <sstream>          // std::istrstream
using namespace std;

void error( string const& s )
{
    cerr << "!" << s << endl;
    exit( EXIT_FAILURE );
}

auto main() -> int
{
    string const data   = "152/N 200/E California Ave";

    cout << "This program parses the line '" << data << "'." << endl;
    cout << endl;
    cout << "Result:" << endl;

    istringstream stream( data );
    char    dummy_char;
    int     latitude;
    char    latitudeDirection;
    int     longitude;
    char    longitudeDirection;
    string  address;

    stream
        >> latitude >> dummy_char >> latitudeDirection
        >> longitude >> dummy_char >> longitudeDirection;
    if( stream.fail() ) { error( "Initial items extraction failed." ); }
    while( stream.peek() == ' ' ) { stream.get(); }
    getline( stream, address );

    cout << "> "
        << latitude << "/" << latitudeDirection << " "
        << longitude << "/" << longitudeDirection << " "
        << "'" << address << "'"
        << "<" << endl;
}

关于c++ - 将不同的变量分配给来自 cin C++ 的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33160702/

相关文章:

c++ - 从URL获取IP(C++)

javascript - 使用javascript将文本区域的输入与div中的文本进行比较

c# - 来自带有特殊表情符号的字符串的子字符串,String.IndexOf 返回 -1

c++ - 是否可以将cin与cout并行使用?

c++ - *什么时候* C++ 异常会破坏 C 代码

c++ - 在 LLVM 中将 ConstantDataArray 转换为 i8*

c++ - 在 C++ 中使用环境变量作为编译时常量

c++ - 创建包含另一个字符串的多个拷贝的字符串的最佳方法

c++ - cin >> 操作数不能与枚举一起使用

c++ - 检查输入和 sleep boost 线程