By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 996 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

problem with vector

P: n/a
I am having a strange problem with a vector. I am reading data in
from a file and putting it in the vector. On the third item read in,
it reaches the .push_back code. However, after that, I use .size(),
and it says that it only has two items. This is bizarre. Any
thoughts?

Code fragment may be found below. Thanks, Alan

while (inFile >sensed_data.time >sensed_data.sensor_who >>
sensed_data.aircraft_who
>sensed_data.IFF >sensed_data.xcor >>
sensed_data.ycor
>sensed_data.altitude >sensed_data.speed >>
sensed_data.course
>sensed_data.Track_Quality)
{
// cout << "Reading input file\n";
if (sensed_data.time == last_clock)
{
cout << "Adding report to list\n";
cout << "Data to add: " << sensed_data.sensor_who << " "
<< sensed_data.aircraft_who << "\n";
sensor_reports.push_back (sensed_data);
cout << "# sensor reports = " << sensor_reports.size() <<
"\n";
}

Nov 1 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Alan wrote:
I am having a strange problem with a vector. I am reading data in
from a file and putting it in the vector. On the third item read in,
it reaches the .push_back code. However, after that, I use .size(),
and it says that it only has two items. This is bizarre. Any
thoughts?

Code fragment may be found below. Thanks, Alan

while (inFile >sensed_data.time >sensed_data.sensor_who >>
sensed_data.aircraft_who
>sensed_data.IFF >sensed_data.xcor >>
sensed_data.ycor
>sensed_data.altitude >sensed_data.speed >>
sensed_data.course
>sensed_data.Track_Quality)
{
// cout << "Reading input file\n";
if (sensed_data.time == last_clock)
{
cout << "Adding report to list\n";
cout << "Data to add: " << sensed_data.sensor_who << " "
<< sensed_data.aircraft_who << "\n";
sensor_reports.push_back (sensed_data);
cout << "# sensor reports = " << sensor_reports.size() <<
"\n";
}
And how are we supposed to use it? The data file is unavailable, the
definition of 'sensed_data' type is unavailable, how 'sensor_reports' is
used before this loop is unavailable... If the printout indicates the
size of the vector is 2, it is 2. It most likely means that if you
think there was no elements before the loop is entered, you must be
mistaken, provided that the printout happens right after the first call
to 'push_back' in this loop.

Add 'assert(sensor_reports.empty());' before this code in your program.

I am not sure how else I could help with the information you've given.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 1 '06 #2

P: n/a
Victor,
You are right. I did not provide more info in the post. I
found the problem, which was not even in the fragment I posted. Next
time I`ll post the whole thing or simplify to narrow in on the problem,
so folks can understand the complete problem.

Thanks, Alan

Nov 1 '06 #3

P: n/a

Alan wrote:
I am having a strange problem with a vector. I am reading data in
from a file and putting it in the vector. On the third item read in,
it reaches the .push_back code. However, after that, I use .size(),
and it says that it only has two items. This is bizarre. Any
thoughts?

Code fragment may be found below. Thanks, Alan

while (inFile >sensed_data.time >sensed_data.sensor_who >>
sensed_data.aircraft_who
>sensed_data.IFF >sensed_data.xcor >>
sensed_data.ycor
>sensed_data.altitude >sensed_data.speed >>
sensed_data.course
>sensed_data.Track_Quality)
{
// cout << "Reading input file\n";
if (sensed_data.time == last_clock)
{
cout << "Adding report to list\n";
cout << "Data to add: " << sensed_data.sensor_who << " "
<< sensed_data.aircraft_who << "\n";
sensor_reports.push_back (sensed_data);
cout << "# sensor reports = " << sensor_reports.size() <<
"\n";
}
Can you tell me what time it is at the North Pole right now?
Because that would probably be easier to determine rather than guessing
at your problem as it stands.

We know nothing of your structs/classes nor the constants (like
last_clock) nor do we know how the data is formatted in the file
(newlines?).
sensed_data's type looks like what? Is it copyable?

Nov 1 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.