A complete solution

Discussion in 'C++' started by Keith H Duggar, Jan 25, 2004.

  1. 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 ;
     
    Keith H Duggar, Jan 25, 2004
    #1
    1. Advertising

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

    "Help for a Pascal programmer please?"
     
    Keith H Duggar, Jan 25, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Francis
    Replies:
    0
    Views:
    435
    Andrew Francis
    Jun 28, 2006
  2. =?Utf-8?B?Y2FzaGRlc2ttYWM=?=

    Solution file not in the solution folder

    =?Utf-8?B?Y2FzaGRlc2ttYWM=?=, Sep 12, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    1,127
    Laurent Bugnion
    Sep 12, 2006
  3. , India
    Replies:
    17
    Views:
    1,093
    James Kanze
    Oct 1, 2007
  4. Replies:
    8
    Views:
    523
  5. Replies:
    0
    Views:
    437
Loading...

Share This Page