-4

I have a text file that reads like this:

Department Max
Bob 3 5 6 2 8 F1
Frank 4 8 8 8 8 F2
Jerry 7 9 9 0 9 F3
William 0 0 12 13 14 F2
Buck 3 4 10 8 8 4 6 F4
Greg 1 2 1 4 2 F1
Mike 3 4 4 8 4 F2

While ignoring the name, how do I read these lines and extract these integers as separate ints so I can add them up together?

So far I have this :

for (int i = 0; i < 10; i++) //Loop that checks the employee and their paygrade, 10 times for 10 lines of data
    {
        getline(inFile, s); //Gets next line in Salary.txt
        string payGrade = s.substr(s.length() - 2, 2); //Checks the last two letters of the line to see paygrade
        if (payGrade == "F1")
        {
            auto pos = s.find(' ', s.find('"'));
            istringstream iss(s.substr(pos));


            F1employeeCounter++;
        }
        if (payGrade == "F2")
        {

            F2employeeCounter++;
        }
        if (payGrade == "F3")
        {

            F3employeeCounter++;
        }
        if (payGrade == "F4")
        {

            F4employeeCounter++;
        }
    }

Basically I have to check what type of "pay grade" each employee is. There are four different type of pay grades: F1,F2,F3,F4 and each has a different way of paying out the employees based on their hours worked.

  • You can use `atoi` to convert strings to ints. You will of course still need to split the string. – Thom Wiggers Oct 30 '14 at 21:37
  • http://stackoverflow.com/questions/7663709/convert-string-to-int-c + http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c may help – matsjoyce Oct 30 '14 at 21:37
  • If you have a question that is so easily searchable online you might want to try to solve it by searching first. If you still have trouble after that, show us what you tried – PeterT Oct 30 '14 at 21:45
  • Possible duplicate of: [How to test whether stringstream operator>> has parsed a bad type and skip it](http://stackoverflow.com/questions/24504582/how-to-test-whether-stringstream-operator-has-parsed-a-bad-type-and-skip-it?noredirect=1#comment37965807_24504582) – πάντα ῥεῖ Oct 30 '14 at 21:49

2 Answers2

1

Here follows an example of how it can be done if you know beforehand that the structure of the text in the file is always gonna look the same as in your example.

Streams are traditionally used in C++ for parsing data (e.g. integers) from text. In this case a stream iterator std::istream_iterator wrapped around a std::istringstream is used to parse the integer tokens. Additionally the standard algorithm std::accumulate is used to sum the parsed integers.

std::ifstream input{"my_file"};

// For every line read:
for (std::string line; std::getline(input, line);) {
    // Find position of first space after first citation character (").
    auto pos = line.find(' ', line.find('"'));

    // Use position to copy relevant numbers part into a string stream.
    std::istringstream iss{line.substr(pos)};

    // Accumulate sum from parsed integers.
    auto sum = std::accumulate(std::istream_iterator<int>{iss}, 
                               std::istream_iterator<int>{},
                               0);

    /* Do something with sum... */
}

Live example

Felix Glas
  • 15,065
  • 7
  • 53
  • 82
0
#include <iostream>
#include <string>
#include <sstream>

int main()
{
    std::istringstream iss("Bo 3 3 6 2 8\nFred 7 8 5 8 8\nJosh 7 9 4 0 1");

    while (iss.good()) //will exit this loop when there is no more text to read
    {
        std::string line;

        //extract next line from iss
        std::getline(iss, line);

        //construct string stream from extracted line
        std::stringstream ss(line);

        std::string name;
        int sum = 0;
        int i = 0;

        //extract name
        ss >> name;

        while(ss.good())
        {
            //extract integer
            ss >> i;

            //add to the sum
            sum += i;
        }

        //output name and sum
        std::cout << name << ":" << sum << std::endl;
    }
}

Output:

Bo:22
Fred:36
Josh:21
Colin Basnett
  • 4,052
  • 2
  • 30
  • 49