-1

So i slurped in a file with numbers in it like: 39.00 into a vector<std::string> and now I need to convert groups of numbers that look like 3 9 0 0 back into the form 39.00

heres a small sample.

3 4 5 0 1 2 5 0 3 4 0 0 3 4 9 0 7 0 0 0 8 0 0 0 9 0 0 0 6 5 0 0 3 9 3 2 1 1 2 0 0 0 2 5 0 0 3 0 9 0 4 0 0 0 5 5 5 0 2 0 0 0 2 5 0 0 3 0 9 0 4 0 0 0 5 5 5 0 2 2 3 0 0 0 2 4 0 0 4 5 0 0 6 7 0 0 6 5 5 0 5 6 9 0 8 7 0 0 4 3 5 0 5 6 9 8 5 5 4 0 3 3 6 2 0 0 3 4 5 0 1 2 5 0 3 4 0 0 3 4 9 0 7 0 0 0 8 0 0 0 9 0 0 0 6 5 0 0 3 9 0 3 2 1 1 2 0 0 0 2 5 0 0 3 0 9 0 4 0 0 0 5 5 5 0 2 0 0 0 2 5 0 0 3 0 9 0 4 0 0 0 5 5 5 0 2 2 3 0 0 0 2 4 0 0 4 5 0 0 6 7 0 0 6 5 5 0 5 6 9 0 8 7 0 0 4 3 5 0 5 6 9 8 5 5 4 0 3 3 6 2 0 0 3 4 5 0 1 2 5 0 3 4 0 0 3 4 9 0 7 0 0 0 8 0 0 0 9 0 0 0 6 5 0 0 3 9 0 0 

transformed into 34.50 12.50 34.00....

My goal is to eventually find the average of all the floats.

Of course if there is a way to slurp a file while keeping formatting only using the standard library that would be cool too.

#include <iostream>
#include <string>
#include <fstream>
#include <streambuf>
#include <regex>
#include <vector>
#include <math.h>

void tableWriter(std::string);
float employeeAverage(std::string);
float employeeTotal(std::string);
float totalAverage(std::string);

void totalPayroll(std::string, std::vector<std::string>);


std::string getEmployeeName(std::string, std::string[]);


int main(int argc, const char * argv[]) {
    try {
    std::vector<std::string> regexContainer;

    std::ifstream t("TheSales.txt");
    std::string theSales;

    t.seekg(0, std::ios::end);
    theSales.reserve(t.tellg());
    t.seekg(0, std::ios::beg);

    theSales.assign((std::istreambuf_iterator<char>(t)),
               std::istreambuf_iterator<char>());

    //std::cout << theSales << std::endl;

    totalPayroll(theSales, regexContainer);
    std::cout << std::endl << regexContainer.empty() << std::endl;

    return 0;
    } catch (int w) {
        std::cout << "Could not open file. Exiting Now." << std::endl; return 0;
    }
}


void tableWriter(std::string){}
float employeeAverage(std::string){return 0.0;}
float employeeTotal(std::string){return 0.0;}
float totalAverage(std::string){return 0.0;}



void totalPayroll(std::string theSales, std::vector<std::string> regexContainer) {

    std::string matches;
    std::regex pattern ("\\d");

    const std::sregex_token_iterator end;
    for (std::sregex_token_iterator i(theSales.cbegin(), theSales.cend(), pattern);
         i != end;
         ++i)
    {
        regexContainer.push_back(*i);
        for (std::vector<std::string>::const_iterator i = regexContainer.begin(); i != regexContainer.end(); ++i)
            std::cout << *i << ' ';
    }


}

this is the data:

2.40 5.30 6.30 65.34 65.34
3.40 7.80 3.20 65.34 65.34
3.40 5.20 8.20 23.54 12.34
2.42 5.30 6.30 5.00  65.34
3.44 7.80 3.20 34.55 65.34
3.45 5.20 8.20 65.34 65.34
Rekumaru
  • 421
  • 1
  • 4
  • 10
  • 2
    What're you trying to do? Is this an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)? – legends2k Jul 08 '15 at 07:07
  • I am writing the averages of sales per employees and their totals and the total sales of every week of every employee using regular expressions. This is merely a set back. – Rekumaru Jul 08 '15 at 07:17
  • you are right this is an xy problem. I just read that link. – Rekumaru Jul 08 '15 at 07:19
  • Just read into a `std::vector` and throw out your regexes. – molbdnilo Jul 08 '15 at 07:19
  • the file also has characters in it that I took out to simplify the problem – Rekumaru Jul 08 '15 at 07:21
  • @asd If reading and writing float is the problem then see [How to parse space-separated floats in C++ quickly?](http://stackoverflow.com/q/17465061/183120). I wouldn't waste time in going through an intermediate representation of `int`. – legends2k Jul 08 '15 at 07:21
  • the regex is to get the specific row of data and then im going to map over it and get the average for that row. – Rekumaru Jul 08 '15 at 07:22
  • Ok i'll go look at that. – Rekumaru Jul 08 '15 at 07:22
  • So, your "sample data" isn't actually a sample of your data and you're trying to solve a different problem from the one you're describing. You won't get very useful answers unless you describe the actual problem. – molbdnilo Jul 08 '15 at 07:30

1 Answers1

1

Functions such as fscanf are able to read from your file and return a correctly formed float number. This should be more efficient than trying to reconstruct them from a stream of char...

  • isn't it a bad idea to read floats with fscanf? someone said it was undefined. – Rekumaru Jul 08 '15 at 07:25
  • Undefined ? What does that mean ? As long as you are sure of your input format, this should do the trick. I suggest you look at legends2k link also, which presents a good set of different solutions. – Fabien Dupont Jul 08 '15 at 07:38
  • Undefined as in something unexpected could happen that you did not intend. i e. reading from an area of memory that you can't access. I'll check out the link. – Rekumaru Jul 08 '15 at 08:02