-1

Given a regular text file of:

56789
28385
43285
22354
34255

I am trying to read each string character in the text file and store them in a 2D vector.

First I would like to take each string row. Then I would like to take each character in the row and convert into an int then push_back into the row. Then I'd like to repeat for each line.

When outputting each column and row in my 2D vector, I would like the same exact output of:

56789 //each number an int now instead of a string
28385
43285
22354
34255

My issue is that I try to use i = stoi(j); which gives the error:

No matching function for call to 'stoi'

I do have the correct #include to be able to use stoi()

vector<vector<int>> read_file(const string &filename) 
{
    string file, line; stringstream convert; int int_convert, counter;
    vector<vector<int>> dot_vector;

    file = filename;
    ifstream in_file;
    in_file.open(file);

    while (getline(in_file, line)) {
        counter++; //how many lines in the file
    }

    char current_char;
    while (getline(in_file, line)) {
        for (int i = 0; i < counter; i++) {
            vector<int> dot_row;
            for (int j = 0; j < line.size(); j++) {
                current_char = line[j];
                i = stoi(j); //this is giving me an error
                dot_row.push_back(i);
            }
            dot_vector.push_back(dot_row);
        }
    }

    in_file.close();
    return dot_vector;
}
JeJo
  • 30,635
  • 6
  • 49
  • 88
Dracep
  • 388
  • 2
  • 13
  • `std::stoi` requires you to include ``. – Toby Speight Oct 15 '18 at 16:58
  • After counting all the lines in the file, the file pointer as at the end. Also, what is `counter` for anyway? – 001 Oct 15 '18 at 17:02
  • @JohnnyMopp I was thinking of using counter to read the number of lines so when I iterate in the for loop the range will only iterate until the counter. Is there a more convenient way? – Dracep Oct 15 '18 at 17:04
  • 1
    I don't see why you need `counter` at all. `while(getline(in_file, line)) {` should be sufficient. – 001 Oct 15 '18 at 17:06
  • @JohnnyMopp got it, thanks for your suggestion! – Dracep Oct 15 '18 at 17:08

1 Answers1

2

Here

 i = stoi(j);
 // j is integer already

std::stoi expect a string as the argument and what you providing is an int.

You can either convert the char to string and call std::stoi like follows

std::string CharString(1, line[j]);
dot_row.emplace_back(std::stoi(CharString));

or can convert the char to int directly while keeping to the vector:

dot_row.emplace_back(static_cast<int>(line[j] - '0'));

You have other problems in your code. Like in the comments mentioned, you do not need the extra counting of lines. Once you have your first while loop, you will reach already end of your file. The code after is then meaningless.

Secondly, you do not need two for loops. Simply use a range-based for loop for each line of strings and while iterating through it, convert to integer and save to vector.

while (getline(in_file, line)) 
{
    std::vector<int> dot_row; dot_row.reserve(str.size());
    for (const std::string& eachChar: line) 
    {
        std::string CharString(1, eachChar);
        dot_row.push_back(std::stoi(CharString));
        // or other option mentioned above
    }
    dot_vector.push_back(dot_row);
}
JeJo
  • 30,635
  • 6
  • 49
  • 88
  • Thanks! This was helpful. Why is the - '0' needed in (line[j] - '0')? – Dracep Oct 15 '18 at 17:02
  • @Dracep because in ASCII code, [the numbers (digits) start from 48](https://stackoverflow.com/questions/439573/how-to-convert-a-single-char-into-an-int) – JeJo Oct 15 '18 at 17:04
  • appreciate the feedback that you gave! This made more sense reading the while and for loop. I got a little confused using 2D vectors and needing two for loops. – Dracep Oct 15 '18 at 17:26
  • 1
    @Dracep glad that I could help you out. However, I would strongly [suggest reading good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), which help you more to understand C++. – JeJo Oct 15 '18 at 17:30