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..

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?

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

Edited 6 Years Ago by n0de: n/a

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.

Edited 6 Years Ago by Ancient Dragon: n/a

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 ?

Edited 6 Years Ago by n0de: n/a

>>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?

7

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

Attachments
"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;;"Rdupis UAB";;545,077.56;;"199.543,72";;"************************************";;;
3;;"E000001";;"VBH Latvia SIA";;492,410.07;;"242.556,47";;"********************************";;;
4;;149717784;;"Alseka UAB";;479,745.75;;"133.708,42";;"*******************************";;;
5;;125030479;;"Megrame medis UAB";;397,472.59;;"193.621,53";;"**************************";;;
6;;153701266;;"Stamita UAB";;322,399.21;;"110.842,75";;"*********************";;;
7;;302409536;;"HCTC UAB";;313,920.53;;"-16.389,63";;"*********************";;;
8;;165711510;;"Sumeda UAB";;310,676.24;;"53.942,71";;"********************";;;
9;;122990844;;"Lanreta UAB";;301,033.40;;"29.702,34";;"********************";;;
10;;167535057;;"Lakaja UAB";;253,613.02;;"171.043,11";;"*****************";;;
11;;302416107;;"Lang gama UAB";;249,180.86;;"-4.787,74";;"****************";;;
12;;170679352;;"Doleta UAB";;212,169.13;;"41.497,94";;"**************";;;
13;;301818042;;"Aradas ir ko UAB";;194,152.76;;"36.803,64";;"*************";;;
14;;300088619;;"Meranti UAB";;179,574.28;;"64.555,34";;"************";;;
15;;161732763;;"Varkojis P. ir kompanija UAB";;176,200.30;;"29.414,30";;"************";;;
16;;134973799;;"Intornas UAB";;168,904.89;;"40.746,60";;"***********";;;
17;;179349384;;"Preikaiio J. firma Taujanta";;163,588.73;;"32.096,26";;"***********";;;
18;;110702540;;"Aveplast UAB";;158,025.33;;"32.092,57";;"**********";;;
19;;2400;;"Kiti pirkjai";;154,117.24;;"0,00";;"**********";;;
20;;165220372;;"Piet Megrame UAB";;134,688.05;;"-4.613,20";;"*********";;;
21;;147829382;;"Aulaukis UAB";;130,347.80;;"16.859,46";;"*********";;;
22;;158828449;;"Roda UAB";;120,936.63;;"5.437,97";;"********";;;
23;;301378536;;"Kontesta UAB";;120,706.15;;"30.181,90";;"********";;;
24;;300052352;;"Langasa UAB";;103,261.97;;"35.957,36";;"*******";;;
25;;154704169;;"Tyla UAB";;100,171.06;;"13.472,38";;"*******";;;
26;;141805531;;"Sadvita UAB";;98,498.45;;"18.847,05";;"******";;;
27;;"E000002";;"VBH Estonia AS";;91,046.80;;"14.908,20";;"******";;;
28;;301821280;;"Optilangai UAB";;87,599.48;;"0,00";;"******";;;
29;;300548416;;"Larnetos langai UAB";;81,331.12;;"17.279,80";;"*****";;;
30;;110572926;;"Thuringia Megaplast-Ukmerg B";;78,216.52;;"1.578,57";;"*****";;;
31;;123404234;;"Arlanga wood UAB";;77,100.49;;"22.386,80";;"*****";;;
32;;160444886;;"Kauno Hronas UAB";;74,609.23;;"3.652,63";;"*****";;;
33;;169281294;;"Ms teritorija UAB";;71,967.64;;"15.900,08";;"*****";;;
34;;245857590;;"Kontivis plius partneriai UAB";;71,522.81;;"19.466,24";;"*****";;;
35;;145241220;;"Bodesa UAB";;70,197.52;;"21.648,98";;"*****";;;
36;;141681432;;"Vinkelis ir KO UAB";;70,009.49;;"-102,40";;"*****";;;
37;;183219083;;"Ukmergs Versms Lang Centras";;69,391.48;;"11.642,11";;"*****";;;
38;;300141089;;"Stikmeta UAB";;67,461.50;;"29.391,89";;"****";;;
39;;300610857;;"Kogita UAB";;65,430.86;;"16.061,96";;"****";;;
40;;300656121;;"Vikonta UAB";;63,470.82;;"-112,47";;"****";;;
41;;147032618;;"Vais viloje UAB";;58,885.49;;"9.906,42";;"****";;;
42;;149733112;;"Stali gaminiai AB";;57,822.17;;"-1,20";;"****";;;
43;;235753650;;"DM langai UAB";;57,336.83;;"15.935,37";;"****";;;
44;;302493071;;"Strommen-Mituva UAB";;55,385.31;;"2.326,79";;"****";;;
45;;157025757;;"Srega UAB";;50,895.75;;"1,17";;"***";;;
46;;301727607;;"Vakar frontas UAB";;50,323.75;;"10.946,14";;"***";;;
47;;124103248;;"Plastakona UAB";;48,846.23;;"13,08";;"***";;;
48;;125706181;;"Langarda UAB";;48,513.17;;"3.425,49";;"***";;;
49;;151299819;;"Alga UAB koncernas";;46,685.87;;"1.428,58";;"***";;;
50;;123354322;;"Aliuminio konstrukcijos UAB";;46,157.94;;"13.778,79";;"***";;;
51;;126189242;;"Staticus UAB";;45,874.16;;"10.640,92";;"***";;;
52;;126112744;;"Langunita UAB";;45,267.66;;"0,00";;"***";;;
53;;301841660;;"Letonas UAB";;41,757.19;;"2.962,41";;"***";;;
54;;302317739;;"Dorvinda UAB";;38,563.01;;"8.430,05";;"***";;;
55;;2637296;;"Pataiens A. I. .";;37,186.57;;"0,00";;"**";;;
56;;302469259;;"Kaita ir Ko UAB";;35,708.41;;"-3.517,48";;"**";;;
57;;122035695;;"Fauga UAB";;35,292.49;;"13.558,99";;"**";;;
58;;111781626;;"BMB kokybiki langai UAB";;33,743.19;;"-52,04";;"**";;;
59;;302351022;;"Dakeda UAB";;33,656.88;;"-2.375,20";;"**";;;
60;;300028180;;"Aliuminio stilius UAB";;33,423.20;;"-5.353,89";;"**";;;
61;;302434532;;"Arvilsta UAB";;33,241.11;;"-335,29";;"**";;;
62;;2500;;"Pirkjas mamena";;32,541.48;;"0,00";;"**";;;
63;;302295552;;"Aimpex UAB";;31,339.81;;"-67,18";;"**";;;
64;;148516650;;"Glaskek langai UAB";;30,646.98;;"16.275,14";;"**";;;
65;;300633941;;"KG Constructions UAB";;30,617.86;;"3.401,11";;"**";;;
66;;145376111;;"Metaloidas UAB";;28,680.33;;"-1.863,44";;"**";;;
67;;300570409;;"Stumdomosios sistemos UAB";;28,387.59;;"0,00";;"**";;;
68;;300585902;;"Daniz UAB";;28,076.21;;"-16,85";;"**";;;
69;;210793950;;"Heinzmann Lietuvos ir Vokietij";;27,524.05;;"6.051,65";;"**";;;
70;;177104870;;"Skatul UAB";;27,357.47;;"0,00";;"**";;;
71;;141602488;;"Liskandas UAB";;27,062.45;;"6.976,86";;"**";;;
72;;7104706;;"Paulausko E. firma";;26,673.61;;"12.408,35";;"**";;;
73;;300531801;;"Lang artel UAB";;26,402.05;;"3.287,10";;"**";;;
74;;123400022;;"Linkm UAB";;25,859.86;;"9.700,94";;"**";;;
75;;110630176;;"VBH Vilnius B UAB";;24,480.00;;"3.291,20";;"**";;;
76;;136058177;;"Mituvos langai UAB";;23,619.81;;"9.921,43";;"**";;;
77;;300110649;;"Kranto ekspeditoriai UAB";;23,225.29;;"1.801,15";;"**";;;
78;;124931734;;"Sunvila UAB";;22,246.89;;"4.341,89";;"*";;;
79;;124479236;;"Skrema UAB";;21,255.34;;"297,81";;"*";;;
80;;301740533;;"Al Construction UAB";;20,691.22;;"0,01";;"*";;;
81;;163396927;;"Dolena UAB";;20,473.55;;"3.534,41";;"*";;;
82;;148355033;;"Legrama UAB";;20,213.39;;"13.745,81";;"*";;;
83;;302300501;;"Agi group UAB";;19,949.96;;"0,00";;"*";;;
84;;300666496;;"Aviresta UAB";;19,511.68;;"-1.794,95";;"*";;;
85;;300591086;;"Valklanga UAB";;19,487.57;;"4.106,29";;"*";;;
86;;153740269;;"Sanlangis UAB";;18,685.70;;"0,00";;"*";;;
87;;8560778;;"Gelanga UAB";;18,505.32;;"-1.192,61";;"*";;;
88;;302507457;;"Gamybos linija UAB";;17,853.59;;"0,86";;"*";;;
89;;302429859;;"Hakas windows UAB";;17,352.14;;"0,05";;"*";;;
90;;173016316;;"Aila UAB";;17,160.17;;"738,35";;"*";;;
91;;123436239;;"Archista UAB";;17,022.43;;"-14,80";;"*";;;
92;;300051161;;"Toldomus UAB";;16,193.76;;"3.012,90";;"*";;;
93;;136060623;;"Lux fausta UAB";;15,735.79;;"4.078,94";;"*";;;
94;;161284349;;"Helanas UAB";;15,641.43;;"0,00";;"*";;;
95;;124826772;;"Kredosta UAB";;15,139.62;;"219,19";;"*";;;
96;;150162212;;"RV Oferta UAB";;14,960.41;;"6.015,09";;"*";;;
97;;142017270;;"Plastrema UAB";;14,836.14;;"907,10";;"*";;;
98;;300502116;;"DOMEX HOUSE UAB";;14,016.00;;"13.910,16";;"*";;;
99;;124600620;;"Magirnis UAB";;13,217.59;;"8.609,09";;"*";;;
100;;301738443;;"Furnika UAB";;13,130.53;;"0,00";;"*";;;

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 ?

This article has been dead for over six months. Start a new discussion instead.