Hi,

my dad asked me two write a litlle program. Nothing special, except i have problems with string to float conversion...

Here is the code i wrote:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string.h>
#include <stdlib.h>
#include <cstring>
#include <iomanip>
using namespace std;
//
main()
{
    string sKBP, sE, sBP;
    int iDS;
    cout << "Name of the file:\n";
    cin >> sBP;
    sKBP += sBP + ".csv"; 
    cout << "In which column data is?\n";
    cin >> iDS;
    const char *ccBP = sKBP.c_str (); 
    ifstream ifsB(ccBP /*"tete.csv"*/);
    if(!ifsB)
        cout << "bla bla..\n";
    else
        {
        while (getline(ifsB, sE))
            {
            istringstream issE(sE);
            string sL;
            int iK=0;
            while(getline(issE, sL, ';'))
                {
                iK++;
                if (iK == iDS)
                    {
                    sL.erase(sL.begin() + sL.find(','));
                    /*char *cNL=new char[sL.size()+1];
                    cNL[sL.size()]=0;
                    memcpy(cNL, sL.c_str(), sL.size());
                    double test = strtod(cNL, NULL);*/
                    stringstream ssTF(sL);
                    float fTEST;
                    ssTF << sL;
                    ssTF >> fTEST;
                    //sL.push_back('\0');
                    //const char *ccL = sL.c_str();
                    cout << "String: " << setprecision(8) << /*atof(ccL)*/ fTEST /*sNL*/ << " length: " << sL.length() << "\n";
                    }
                }
            }
        }
}

after conversion i get only one digit after floating point.. could someone help finding the problem ? works with setprecision - but thats only for cout.. but i need the precision in further calculations not in cout..

Recommended Answers

All 15 Replies

Please post the first few lines of the csv file so we have something with which to test your program.

Please post the first few lines of the csv file so we have something with which to test your program.

first 10 lines:

"Klasifikuoti
 ";;"Nr.
 ";;"Pavadinimas
 ";;"Pardavimai (Lt)
 ";;"Likutis (Lt)
 ";;"Dalis Pardavimai (Lt)
 ";"D";;10000000
1;;241844720;;"Arlanga pvc UAB";;688,609.50;;"259.120,58";;"*********************************************";4694589.355;;
2;;170681385;;"Rūdupis UAB";;545,077.56;;"199.543,72";;"************************************";;;
3;;"E000001";;"VBH Latvia SIA";;492,410.07;;"242.556,47";;"********************************";;;

Why are the first 7 lines different than from the rest of the lines in the file?

Why are the first 7 lines different than from the rest of the lines in the file?

i do not know, this is how openoffice (now libreoffice) saves to csv..

Well you had better find out because it will be impossible to parse that file until you know that.

Looks like LibreOffice is only in beta -- it could contain bugs. Get and use OpenOffice instead.

But i parse the data completely good. I can manage any cell. when all is string. Bet when it comes to converting.. i mentioned in first post what my problem is.. Look, if you remove setprecision from that cout, you get only one digit after floating point, why is that ? does that stringstream to float round's off automaticly ?

>>Look, if you remove setprecision from that cout, you get only one digit after floating point

cout does not display trailing 0s therefore 609.50 will be displayed as 609.5.

>>Look, if you remove setprecision from that cout, you get only one digit after floating point

cout does not display trailing 0s therefore 609.50 will be displayed as 609.5.

but it does not display other digits, like if i have 609.53 it displays me 609.5 - that is the problem

which column is the data in that you want to use?

which column is the data in that you want to use?

7

i attached the whole sample file, you just need to rename the extension

I compiled and ran with VC++ 2010 Express. Here is the output

In which column data is?
7
String: 688609.5 length: 9
String: 545077.56 length: 9
String: 492410.06 length: 9
String: 479745.75 length: 9
String: 397472.59 length: 9
String: 322399.22 length: 9
String: 313920.53 length: 9
String: 310676.25 length: 9
String: 301033.41 length: 9
String: 253613.02 length: 9
String: 249180.86 length: 9
String: 212169.13 length: 9
String: 194152.77 length: 9
String: 179574.28 length: 9
String: 176200.3 length: 9
String: 168904.89 length: 9
String: 163588.73 length: 9
String: 158025.33 length: 9
String: 154117.23 length: 9
String: 134688.05 length: 9
String: 130347.8 length: 9
String: 120936.63 length: 9
String: 120706.15 length: 9
String: 103261.97 length: 9
String: 100171.06 length: 9
String: 98498.453 length: 8
String: 91046.797 length: 8
String: 87599.477 length: 8
String: 81331.117 length: 8
String: 78216.523 length: 8
String: 77100.492 length: 8
String: 74609.227 length: 8
String: 71967.641 length: 8
String: 71522.813 length: 8
String: 70197.523 length: 8
String: 70009.492 length: 8
String: 69391.477 length: 8
String: 67461.5 length: 8
String: 65430.859 length: 8
String: 63470.82 length: 8
String: 58885.488 length: 8
String: 57822.172 length: 8
String: 57336.828 length: 8
String: 55385.309 length: 8
String: 50895.75 length: 8
String: 50323.75 length: 8
String: 48846.23 length: 8
String: 48513.172 length: 8
String: 46685.871 length: 8
String: 46157.941 length: 8
String: 45874.16 length: 8
String: 45267.66 length: 8
String: 41757.191 length: 8
String: 38563.012 length: 8
String: 37186.57 length: 8
String: 35708.41 length: 8
String: 35292.488 length: 8
String: 33743.191 length: 8
String: 33656.879 length: 8
String: 33423.199 length: 8
String: 33241.109 length: 8
String: 32541.48 length: 8
String: 31339.811 length: 8
String: 30646.98 length: 8
String: 30617.859 length: 8
String: 28680.33 length: 8
String: 28387.59 length: 8
String: 28076.211 length: 8
String: 27524.051 length: 8
String: 27357.471 length: 8
String: 27062.449 length: 8
String: 26673.609 length: 8
String: 26402.051 length: 8
String: 25859.859 length: 8
String: 24480 length: 8
String: 23619.811 length: 8
String: 23225.289 length: 8
String: 22246.891 length: 8
String: 21255.34 length: 8
String: 20691.221 length: 8
String: 20473.551 length: 8
String: 20213.391 length: 8
String: 19949.961 length: 8
String: 19511.68 length: 8
String: 19487.57 length: 8
String: 18685.699 length: 8
String: 18505.32 length: 8
String: 17853.59 length: 8
String: 17352.141 length: 8
String: 17160.17 length: 8
String: 17022.43 length: 8
String: 16193.76 length: 8
String: 15735.79 length: 8
String: 15641.43 length: 8
String: 15139.62 length: 8
String: 14960.41 length: 8
String: 14836.14 length: 8
String: 14016 length: 8
String: 13217.59 length: 8
String: 13130.53 length: 8
Press any key to continue . . .

Code::Blocks w/MinGW compiler. For some reason the length is not the same as with VC++ 2010 Express.

In which column data is?
7
String: 688609.5 length: 9
String: 545077.56 length: 9
String: 492410.06 length: 9
String: 479745.75 length: 9
String: 397472.59 length: 9
String: 322399.22 length: 9
String: 313920.53 length: 9
String: 310676.25 length: 9
String: 301033.41 length: 9
String: 253613.02 length: 9
String: 249180.86 length: 9
String: 212169.12 length: 9
String: 194152.77 length: 9
String: 179574.28 length: 9
String: 176200.3 length: 9
String: 168904.89 length: 9
String: 163588.73 length: 9
String: 158025.33 length: 9
String: 154117.23 length: 9
String: 134688.05 length: 9
String: 130347.8 length: 9
String: 120936.63 length: 9
String: 120706.15 length: 9
String: 103261.97 length: 9
String: 100171.06 length: 9
String: 98498.453 length: 8
String: 91046.797 length: 8
String: 87599.477 length: 8
String: 81331.117 length: 8
String: 78216.523 length: 8
String: 77100.492 length: 8
String: 74609.227 length: 8
String: 71967.641 length: 8
String: 71522.812 length: 8
String: 70197.523 length: 8
String: 70009.492 length: 8
String: 69391.477 length: 8
String: 67461.5 length: 8
String: 65430.859 length: 8
String: 63470.82 length: 8
String: 58885.488 length: 8
String: 57822.172 length: 8
String: 57336.828 length: 8
String: 55385.309 length: 8
String: 50895.75 length: 8
String: 50323.75 length: 8
String: 48846.23 length: 8
String: 48513.172 length: 8
String: 46685.871 length: 8
String: 46157.941 length: 8
String: 45874.16 length: 8
String: 45267.66 length: 8
String: 41757.191 length: 8
String: 38563.012 length: 8
String: 37186.57 length: 8
String: 35708.41 length: 8
String: 35292.488 length: 8
String: 33743.191 length: 8
String: 33656.879 length: 8
String: 33423.199 length: 8
String: 33241.109 length: 8
String: 32541.48 length: 8
String: 31339.811 length: 8
String: 30646.98 length: 8
String: 30617.859 length: 8
String: 28680.33 length: 8
String: 28387.59 length: 8
String: 28076.211 length: 8
String: 27524.051 length: 8
String: 27357.471 length: 8
String: 27062.449 length: 8
String: 26673.609 length: 8
String: 26402.051 length: 8
String: 25859.859 length: 8
String: 24480 length: 8
String: 23619.811 length: 8
String: 23225.289 length: 8
String: 22246.891 length: 8
String: 21255.34 length: 8
String: 20691.221 length: 8
String: 20473.551 length: 8
String: 20213.391 length: 8
String: 19949.961 length: 8
String: 19511.68 length: 8
String: 19487.57 length: 8
String: 18685.699 length: 8
String: 18505.32 length: 8
String: 17853.59 length: 8
String: 17352.141 length: 8
String: 17160.17 length: 8
String: 17022.43 length: 8
String: 16193.76 length: 8
String: 15735.79 length: 8
String: 15641.43 length: 8
String: 15139.62 length: 8
String: 14960.41 length: 8
String: 14836.14 length: 8
String: 14016 length: 8
String: 13217.59 length: 8
String: 13130.53 length: 8

Process returned 0 (0x0) execution time : 2.777 s
Press any key to continue.

Code::Blocks w/MinGW compiler. For some reason the length is not the same as with VC++ 2010 Express.

yes i get the same. Now try to eliminate setprecision and see what you get..

If you don't specify otherwise the default cout will display only 6 significant digits both before and after the decimal point but without leading or tailing 0s.

If you add fixed cout << "String: " << fixed << fTEST << " length: " << sL.length() << "\n"; it will display more digits.

If you don't specify otherwise the default cout will display only 6 significant digits both before and after the decimal point but without leading or tailing 0s.

If you add fixed cout << "String: " << fixed << fTEST << " length: " << sL.length() << "\n"; it will display more digits.

so it is problem of cout, not the conversion, right ? if i will make firther calculations with that float, i will have the precision, yes ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.