gpt4 book ai didi

c++ - 使用字符串流 : Only working for first value

转载 作者:行者123 更新时间:2023-11-28 06:23:51 25 4
gpt4 key购买 nike

我正在尝试将 stringstream 与 peek() 和 get(n) 结合使用。它仅对第一个值 Name 起作用,然后为除重量之外的所有内容提供空值,重量为 0(当使用 cout 打印时)。

    stringstream ss;
ss.str("John Smith, Toyota, 160, brown blue, orange");
//Extract into these variables:
string Name = "" ;
string car = "" ;
int weight = 0;
string hairColor = "";
string eyeColor = "";
string favoriteColor = "";





while (ss.peek () != ',')
{
char temp;
ss. get(temp);
Name += temp;
}
while (ss.peek () != ',')
{
char temp;
ss. get(temp);
car += temp;
}
while (ss.peek () != ',')
{
char temp;
ss. get(temp);
weight += temp;
}
while (ss.peek () != ',')
{
char temp;
ss. get(temp);
hairColor += temp;
}
while (ss.peek () != ',')
{
char temp;
ss. get(temp);
eyeColor += temp;
}
while (ss.peek () != ',')
{
char temp;
ss. get(temp);
favoriteColor += temp;
}


cout << "Name is: " << Name << endl;
cout << "car is: " << car << endl;
cout << "weight is: " << weight << endl;
cout << "Hair color is: " << hairColor << endl;
cout << "Eye color is: " << eyeColor << endl;
cout << "Favorite color is: " << favoriteColor << endl;

这里有什么问题?

最佳答案

直接的问题是,在第一个循环之后,继续条件为 false,因此后续循环中没有一个具有相同的继续条件,不会执行任何操作。

但是,提取项目的常用方法是使用 getline 函数,您可以在其中指定任意分隔符而不是换行符。

正确地做到这一点有点复杂:

#include <iomanip>              // std::setw
#include <iostream>
#include <stdexcept> // std::runtime_error, std::exception
#include <stdlib.h> // EXIT_FAILURE, EXIT_SUCCESS
#include <sstream> // std::istringstream
using namespace std;

auto fail( const string& s ) -> bool { throw runtime_error( s ); }

auto get_string( istream& stream, const char delimiter = ',' )
-> string
{
while( stream.peek() == ' ' )
{
stream.get();
}
string result;
getline( stream, result, delimiter )
|| fail( "get_string: failed to extract text" );
return result;
}

template< class Type >
auto get( istream& stream, const char delimiter = ',' )
-> Type
{
istringstream conversion_stream( get_string( stream, delimiter ) );
Type result;
conversion_stream >> result
|| fail( "get: failed to convert text to value" );
return result;
}

template<>
auto get<string>( istream& stream, const char delimiter )
-> string
{ return get_string( stream, delimiter ); }

template< class Type >
void display( const char name[], const Type& value )
{
cout << setw( 15 ) << name << " = " << value << endl;
}

#define DISPLAY( x ) display( #x, x )
void cpp_main()
{
istringstream data_stream( "John Smith, Toyota, 160, brown, blue, orange" );

const auto name = get<string>( data_stream );
const auto car = get<string>( data_stream );
const auto weight = get<int>( data_stream );
const auto hairColor = get<string>( data_stream );
const auto eyeColor = get<string>( data_stream );
const auto favoriteColor = get<string>( data_stream );

DISPLAY( name );
DISPLAY( car );
DISPLAY( weight );
DISPLAY( hairColor );
DISPLAY( eyeColor );
DISPLAY( favoriteColor );
}

auto main() -> int
{
try
{
cpp_main();
return EXIT_SUCCESS;
}
catch( const exception& x )
{
cerr << "!" << x.what() << endl;
}
return EXIT_FAILURE;
}

关于c++ - 使用字符串流 : Only working for first value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28863071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com