-1

Let's say I have a txt file

Rahim
5 6 7
Karim
6 7 8 
Rahman
7 8 9

I want to save those name in a vector and I want to put those number in a nested vector as integers.

I tried this but it's not working.

#include <string>
#include <vector>

using namespace std; 


int main(){
    vector<vector<int> > data;
    vector<int> temp;
    vector<string> nameF; 
    string file; 

    cout << "Enter a file: "; 
    cin >> file; 
    ifstream infile(file); 
    if(!infile.good()){
        cout << "Enter a  valid file: " << endl;
        return 1;
    }   

    string name; 
    string values; 

    while(!infile.eof()){
        getline(infile, name); 
        getline(infile, values);
    
        if(!infile.fail()){
            nameF.push_back(name);
        
            for(int i = 0; i < values.length(); i++){
                string s = values.substr(i, values.find(" "));
                temp.push_back(stoi(s));
            }
            data.push_back(temp); 
        }
    }
}
JaMiT
  • 9,693
  • 2
  • 12
  • 26
  • 3
    [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – 463035818_is_not_a_number Jan 14 '21 at 19:42
  • 1
    What is the meaning of "not working" ? – 463035818_is_not_a_number Jan 14 '21 at 19:42
  • Its giving me this. terminate called after throwing an instance of 'std::invalid_argument' what(): stoi Aborted (core dumped – Mosrour Tafadar Jan 14 '21 at 19:43
  • To fix the potential bug identified in the first comment you can replace `while(!infile.eof()){ getline(infile, name); getline(infile, values);` with `while ( getline(infile, name) && getline(infile, values) ) {` – drescherjm Jan 14 '21 at 19:47
  • What is the loop `for(int i = 0; i < values.length(); i++){` supposed to do? It looks like you wanted to extract all numbers from this line. – churill Jan 14 '21 at 19:56
  • 1
    You are confusing `values.length()` (which is the length of the string `values`) with the number of integers in the string (which is a totally dfferent number). – john Jan 14 '21 at 19:57
  • 1
    Do you always have three numbers per line (would make it much easier if you do)? – john Jan 14 '21 at 19:58
  • No. It can be much bigger – Mosrour Tafadar Jan 14 '21 at 20:00
  • @churill, I read as string but as getline so now I want to make those values integer. – Mosrour Tafadar Jan 14 '21 at 20:01
  • Do you really want to use the names and the values that follows the names separately - or are you planning on using them in combination by indexing `nameF` and `data`? – Ted Lyngmo Jan 14 '21 at 21:05
  • I want them separately – Mosrour Tafadar Jan 15 '21 at 06:15
  • I understood that from your question and that's not what I asked about. My question was more focused on how you are planning on using them. Would they still be usable if you put the names (`nameF`) in random order while keeping `data` as-is? – Ted Lyngmo Jan 15 '21 at 15:39

1 Answers1

1

So I suggest you use an istringstream to read the integers from the second string

    #include <sstream>

    nameF.push_back(name);
    
    istringstream buffer(values);
    vector<int> temp;
    int value;
    while (buffer >> value) {
        temp.push_back(value);
    }
    
    data.push_back(temp); 

An istringstream lets you read from a string as if it was any other input source. So it's an easy way to convert a whitespace separated string of integers.

Another problem in your original code is that you reuse temp for each line without clearing it in between. So each line is going to add to the integers that are already there from previous lines. I've fixed that problem by moving the declaration of temp to inside the loop, but you could just call temp.clear(); before the integer reading loop.

john
  • 71,156
  • 4
  • 49
  • 68