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
|
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
template < typename T > std::istream& read_number( std::istream& stm, T& n )
{
char c ;
if( stm >> c && c != ',' ) /* not a comma, put it back */ stm.putback(c) ;
return stm >> n ;
}
void read_values( std::istream& stm )
{
int id ;
double hours ;
double wage ;
while( read_number(stm,id) && read_number(stm,hours) && read_number(stm,wage) )
std::cout << std::fixed << std::setprecision(2) << id << ' ' << hours << ' ' << wage << '\n' ;
std::cout << "------------------------\n" ;
}
int main()
{
std::istringstream stm_ssv( "41082 20 15\n30101 39.5 20\n10201 40 10\n50201 12 12\n99201 62.75 25.5" ) ;
read_values( stm_ssv ) ;
std::istringstream stm_csv( "41082,20, 15\n30101,39.5,20\n10201, 40, 10\n50201, 12, 12\n99201,62.75, 25.5" ) ;
read_values( stm_csv ) ;
std::istringstream stm_mixed( "41082 20,15\n30101 39.5, 20\n10201, 40 10\n50201,12,12\n99201 62.75 25.5" ) ;
read_values( stm_mixed ) ;
}
|