0

Trying to read from a text file with format:

Jamie 27 31
Tom 31 22
Rashid 22 19
Sarah 18 22
Ricardo 90 27

I want to separate this into three vectors, <name>, <x> and <y>:

ifstream in ("data.txt");
vector<string> names;
string name;
if (!in) 
    return;
while (in >> name) 
    names.push_back(name);

And then the same for x and y but with vector<int>.

As it stands this code will read all words into names and treat them like strings. I need to do some maths on x and y so I need them as type int.

I was thinking of using multiples but I'm sure there is a more elegant solution. As in, y will always have an index completely divisible by three once read into names, providing I create an empty first position in names[0]. Can anybody help me out?

KR

smollma
  • 239
  • 3
  • 12
  • 1
    Have you considered defining a structure and using a single vector instead? That said, what is your current solution? What have you tried? And where is the minimal example as required by the site guidelines? – Ulrich Eckhardt Nov 20 '15 at 16:20

2 Answers2

1

How about:

...
vector <int> x;
vector <int> y;
...
...
int num_x, num_y;
while (in >> name >> num_x >> num_y){
   names.push_back(name);
   x.push_back(num_x);
   y.push_back(num_y);
}

There are different ways. But for instance you can then access the elements like this:

for(int i=0; i<names.length(); i++){
   cout << "index " << i << " names: "<< names[i] << ", x:" << x[i] << ", y:" << y[i] << "\n";
}
dingalapadum
  • 2,077
  • 2
  • 21
  • 31
  • What if either input operation fails? – Ulrich Eckhardt Nov 20 '15 at 16:21
  • @dingalapadum: You can fix this by using another variable and doing `while (in >> name >> num1 >> num2)` – Blastfurnace Nov 20 '15 at 16:31
  • This solution works fine and is actually neat, but if I write this into a function, can I return `name`, `x` and `y` in one go as: `return x, y, name;`? Should I put this into a structure and return that? Or would you recommend just creating separate functions of the same code and returning `name`, `x` and `y`? – smollma Nov 20 '15 at 17:40
  • @lmsavk Ah. But now you changed your requirement... this is not what you originally were asking for. Anyway: to answer your comment: No, a function can only return "one thing". But you have other options: you could pass the three vectors by reference to the function. The function then simply fills the vector and not return anything (`void`). Or as you noted, you could make a `struct myStruct{string name; int x; int y; };` and then have a `vector`. – dingalapadum Nov 20 '15 at 17:47
  • @dingalapadum and might I asked how to access vector elements and vectors once inside my returned vector of struct? So, at the moment I declare my struct: `struct data_struct {string names; int x; int y; } ;` and then put it in a vector to return, `vector data;` and `return data;`. – smollma Nov 20 '15 at 20:54
  • @lmsavk: i edited my answer. In your case: for a member variable `names` of the struct you can just write `data[elemNr].names;` – dingalapadum Nov 20 '15 at 21:03
  • @dingalapadum do you see a syntactic problem with this: `ifstream in ("countries.txt"); while (in >> country_val >> num_x >> num_y){...}`? I don't but I get a C2697 error on `in >>`... – smollma Nov 20 '15 at 22:00
  • Apologies, just forgot header. – smollma Nov 20 '15 at 22:06
  • @dingalapadum I'm having some trouble using structs to transport my vectors around. How would I do it by reference? So far, I can only find examples of sending vectors from main, to a function to use them. I don't see how to send them from a function to main. – smollma Nov 20 '15 at 23:00
  • @lmsavk I think this is way out of scope of the original question. Passing by reference is a topic where you should find a lot of material. Have a look at: http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/ . To pass a vector by ref, just do `void myFunction(vector &v){...}`. You might also have a look at; http://stackoverflow.com/questions/15890427/passing-vector-by-reference and http://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value. And maybe also this: http://slash7.com/2006/12/22/vampires/ ;) – dingalapadum Nov 23 '15 at 10:34
  • @dingalapadum Great. Thank you! Done now. – smollma Nov 24 '15 at 17:58
0

I would recommend taking the input as a string line by line (getline() and istringstream()), parse the input ( Split string with delimiters in C ), then push it into your vector. If you need to convert, run a simple conversion function such as atoi()

Community
  • 1
  • 1
Kendall Weihe
  • 193
  • 5
  • 18