0

I'm quite new to C++, so sorry if this is a dumb question!

For a project we are given a file with a couple of thousand lines of values, each line having 9 different numbers.

I want to create a for/while loop that, for each loop, stores the 8th and 9th integer of a line as a variable so that I can do some calculations with them. The loop would then move onto the next line, store the 8th and 9th numbers of that line as the same variable, so that I can do the same calculation to it, ending when I've run out of lines.

My problem is less to do with reading the file, I'm just confused how I'd tell it to take only the 8th and 9th value from each line.

Thanks for any help, it is greatly appreciated!

2 Answers2

0

First, split the string, then convert those to numbers using atoi. You then will take the 8th and 9th values from the array or vector with the numbers.

//Split string
std::vector<std::string> &split(const std::string &s, char delim,   std::vector<std::string> &elems) {
    std::stringstream ss(s);
    std::string item;
    while (std::getline(ss, item, delim)) {
        elems.push_back(item);
    }
    return elems;
}


std::vector<std::string> split(const std::string &s, char delim) {
    std::vector<std::string> elems;
    split(s, delim, elems);
    return elems;
}

//new code goes here
std::string line;

std::vector<std::string> lineSplit = split(line, ' ');
std::vector<int> numbers;

for (int i = 0; i < lineSplit.size(); i++)
    numbers.push_back(atoi(lineSplit[i]);//or stoi

int numb1 = numbers[7];//8th
int numb2 = numbers[8];//9th
Community
  • 1
  • 1
Liam McInroy
  • 4,339
  • 5
  • 32
  • 53
  • Since this is C++ you should recommend C++ constructs like [`std::istringstream`](http://en.cppreference.com/w/cpp/io/basic_istringstream) and [`std::stoi`](http://en.cppreference.com/w/cpp/string/basic_string/stol), though the latter needn't be used in this scenario. – David G Mar 30 '14 at 18:56
  • @0x499602D2 I fail to see how `istringstream` would be applicable here? – Liam McInroy Mar 30 '14 at 19:13
  • He needs to go through each line and then extract an integer from that line. He would go through the line using `std::getline()` which puts the line into a string, and then he makes the string act as a stream using `std::istringstream` so he can read into an integer. – David G Mar 30 '14 at 19:20
  • @0x499602D2 But each line has 9 different integers, and he needs to first find the sub strings, then he can call `stoi` or `atoi` to get the value. Check edits – Liam McInroy Mar 30 '14 at 19:32
0

Designed for readability rather than speed. It also performs no checking that the input file is the correct format.

template<class T> ConvertFromString(const std::string& s)
{
    std::istringstream ss(s);
    T x;
    ss >> x;
    return x;
}


std::vector<int> values8;
std::vector<int> values9;

std::ifstream file("myfile.txt");
std::string line;
while (std::getline(file, line))
{
    std::istringstream ss(line);
    for (int i = 0; i < 9; i++)
    {
        std::string token;
        ss >> token;
        switch (i)
        {
            case 8:
            {
                values8.push_back(ConvertFromString<int>(token));
            }
            break;

            case 9:
            {
                values9.push_back(ConvertFromString<int>(token));
            }
            break;
        }
    }

}
Neil Kirk
  • 21,327
  • 9
  • 53
  • 91