read input into vector?

Feb 4, 2009 at 12:20am
from my last post, the best help suggestion was to use vectors instead of arrays for reading input data of an unkown size (but known format)


that said, i am currently trying to put a 3 column by 5 row set of input data into 3 vectors (one per column)

how can i achieve this? my first thoughts are to first declare the vectors like

vector<string> colone
vector<int> coltwo
vector<string> colthree

ifstream infile('data.txt')

int i=0
while (infile)

infile >> colone(i) >> coltwo(i) >> colthree(i) >> endl;
i++



i get massive errors when i try this :( any ideas?

Feb 4, 2009 at 12:38am
[i[i][/i]], not (i).
Let me guess. Your previous language was BASIC?
And no >>endl. That just doesn't make any sense.

Also, that won't work. Vectors aren't automatically resized, so you must do it yourself:
1
2
3
infile >> x >> y >> z;
colone.push_back(x);
//And so forth... 
Last edited on Feb 4, 2009 at 12:38am
Feb 4, 2009 at 12:40am
Post your entire main function exactly as you are trying to compile it. What you have posted will obviously not compile by itself. I'm very busy and would be glad to help in my spare time but unless I see exactly what you are trying to compile there is no way for me or anyone else to help.
Feb 4, 2009 at 1:10am
ok, i will try this and post my code when i have reworked my thought process. really appreciate the great help i get here
Feb 4, 2009 at 1:29am
here goes....


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <string>
#include <fstream>
#include <vector>
#include <iostream>

using namespace std;

int main()
{
  vector<string> player;
  vector<string> team;
  vector<int> number;

  ifstream infile( "bdata.txt" );
  string str;
  int x;

  while(infile)
  {
               infile >>
     str >>
     str >>
     x;
     player.push_back(str);
     team.push_back(str);
     number.push_back(x);
     }
     
        infile.close();


}


this does not give me the desired output
Feb 4, 2009 at 1:34am
str (an std::string) can only hold one value at a time. See that in my example above, I distinctly used x, y, and z.
Feb 4, 2009 at 2:57am
you are right, i changed it to

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  string strplay;
  string strteam;
  int x;

  while(infile)
  {
               infile >>
     strplay >>
     strteam >>
     x;
     player.push_back(strplay);
     team.push_back(strteam);
     number.push_back(x);
     }


however i am still not getting the right results
Feb 4, 2009 at 3:03am
Well, what are you getting?

By the way, it's (usually) not a good idea to have a statement across several lines.
Feb 4, 2009 at 3:49am
well, for some reason, when the input is a 3 row 3 column text file

it returns 4 for all the sizes using player.size() number.size() etc...


other than that, i can get it to output all data in the vectors :) which is AWESOME, i feel like i am getting there

my current hurdle is trying to find the maximum value in the number vector (which i can do using the algorithm)

but once i find the max number i want to go to the corresponding player name to see who it is - so i need a way to store *where* the max value occurs at so i can put this into the player[] vector to evaluate it there.

currently stumped and back to googling
Topic archived. No new replies allowed.