• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

Question about vectors with reading a file

Hello everyone how are you doing? I am sorry to bother you all, however I have been at this for awhile now and I am not sure what to do. I am reading a file and counting the records and puting them into a vector. However the file in the data looks like this:
some text
398339
some text
3903993
What I need to do is count the records which is easy but I want to display the vector but only the text not the numbers. I have tried like arrays do A1[i].text A1[i].number. But it doesn't seem to work that way how do vectors work to do that or do they not work like that. Thanks.


0
Corey_819
Asked:
Corey_819
  • 6
  • 2
  • 2
1 Solution
 
AxterCommented:
What type is your vector holding?

If your type has a text member and a number member, then it can do that.

Please post some code so we can further assist you.
0
 
AxterCommented:
>>But it doesn't seem to work that way how do vectors work to do that or do they not work like that. Thanks.

Also, please give more details as to how it does not work.
Does it fail to compile?
Does it fail to link?
Does it crash?
Does it run, but fails to display properly, and if so, more details.
0
 
Corey_819Author Commented:
I am doing this bear with me on the syntax I don't have the exact code with me it is at home and I am at work. But all this code is doing as you probably can see is loading the entire some text 934873 some text 939339. Thanks for your help greatly appreciated. :)

vector<string> v1;
string line;
infile("file.dat")

while(getline(infile, line, '\n')
{

v1.push.back(line);

}
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Corey_819Author Commented:
No I don't get any errors I just don't know how to load up the array distiguishing the text and the numbers. Thanks
0
 
Corey_819Author Commented:
I meant vector not array
0
 
jhshuklaCommented:
the way you are currently doing reads lines from files. it will treat a string of digits as a string of chars and not as numbers. the numbers that you read in are stored as strings of chars in line. if you want to read them in to numbers instead, you can define a struct that holds a string and a number and make a vector out of that type.

or you could skip the elements that hold numbers. use v1[2*i]
0
 
Corey_819Author Commented:
jhshukla thanks for the help. I was wondering could you give me an example on how to do that with vectors. I haven't used vectors that much so I am still learning. Thanks. :)
0
 
Corey_819Author Commented:
struct Data{

    string city_name;
      double pop;
};


vector<Data> vec1;
      ifstream infile ("cities.dat");
    infile >> Data
    getline(infile, Data);

Something like this jhshukla
0
 
jhshuklaCommented:
read all the lines as you are doing it now. then,
for(int i=0; i<v1.size(); i++){
  if( i%2 == 0 ) //if i is an even number
    cout << v1[i] << endl;
}

OR

for(int i=0; i < v1.size()/2; i++){
  cout << v1[i*2] << endl;
}

=======================
OR
=======================

struct Data{
    string city_name;
     double pop;
};

...
  vector<Data> vec1;
     ifstream infile ("cities.dat");
  loop{
    getline(infile, vec1[i].city_name);
    infile >> vec1[i].pop;
  }

  loop{
     cout << vec1[i].city_name
  }
...

hope that helps
jay
0
 
Corey_819Author Commented:
Thank you that is exactly what I was lookking for. :)
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 6
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now