A complete solution

K

Keith H Duggar

Hi Tad,

While learning to use streams with a simple parse such as this my
opinion is that you don't need the complication of a splitting
function, vectors, etc. When you extract number types (int,float,...)
from the stream it will read all appropriate digits and stop at
characters such as a comma. Therefore, you can simply extract the
number and then the separator. Here is a complete solution that writes
the transformed string to standard out :

#include <fstream>
#include <iostream>

int main ( int argc , char * argv[] {

std::ifstream input ( "AA.ASC" ) ;

int itemp ;
char comma ;
double ftemp ;

input >> itemp ;
std::cout << "AA," << itemp ;

std::cout.precision ( 2 ) ;
std::cout.flags ( std::ios::fixed ) ;

for ( int i = 0 ; i < 4 ; ++i ) {

input >> comma >> ftemp ;
std::cout << comma << ftemp ;

}

input >> comma >> itemp ;
std::cout << comma << itemp ;

return 0 ;

}

In this case I hard-coded the file name and output stream but of
course in the future you will probably want to improve upon this.
Also, it differs slightly from your requirements because streams ROUND
the decimal rather than truncating it. I figured that's probably what
you actually wanted; but, if you really want to truncate then alter
the code as follows :

input >> comma >> ftemp ;
ftemp = 0.01 * int ( 100.0 * ftemp ) ; // truncate instead of
round
std::cout << comma << ftemp ;

or better :

input >> comma >> ftemp ;
ftemp = 0.01 * static_cast<int> ( 100.0 * ftemp ) ;
std::cout << comma << ftemp ;
 
K

Keith H Duggar

Sorry, this message was meant as a follow up to the thread :

"Help for a Pascal programmer please?"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top