Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Question about vectors with reading a file

Posted on 2004-10-17
10
Medium Priority
?
224 Views
Last Modified: 2010-04-01
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
Comment
Question by:Corey_819
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
  • 2
10 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 12336076
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
 
LVL 30

Expert Comment

by:Axter
ID: 12336081
>>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
 
LVL 1

Author Comment

by:Corey_819
ID: 12338350
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:Corey_819
ID: 12343331
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
 
LVL 1

Author Comment

by:Corey_819
ID: 12343480
I meant vector not array
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 12344836
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
 
LVL 1

Author Comment

by:Corey_819
ID: 12346554
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
 
LVL 1

Author Comment

by:Corey_819
ID: 12350259
struct Data{

    string city_name;
      double pop;
};


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

Something like this jhshukla
0
 
LVL 9

Accepted Solution

by:
jhshukla earned 500 total points
ID: 12378096
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
 
LVL 1

Author Comment

by:Corey_819
ID: 12380057
Thank you that is exactly what I was lookking for. :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question