1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#include <iostream>
#include <string>
#include <sstream>
void print_city_totals( std::istream& input, std::ostream& output )
{
constexpr char COMMA = ',' ;
std::string line ;
while( std::getline( input, line ) ) // for each line input the input stream
{
std::istringstream stm(line) ;
double first ;
stm >> first ; // get the first number
stm.ignore( 100, COMMA ) ; // throw away the comma
std::string city_name ;
std::getline( stm, city_name, ',' ) ; // read everything upto the comma into name
double second ;
stm >> second ; // get thesecond number
if(stm) output << city_name << ' ' << first+second << '\n' ;
else output << "[ error in line: '" << line << "' ]\n" ;
}
}
int main()
{
std::istringstream input( "15.1,Chico,17.4\n"
"34.4,Seattle,30.52\n"
"what is this?\n"
"2.9,Portland,26.1\n"
".5,Death Valley,1.1\n" ) ;
print_city_totals( input, std::cout ) ;
}
|