473,326 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

problem with vector

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
3 1252
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
13
by: Joseph | last post by:
I was doing my assignment,but encountered a problem at last step!!!!!! for easy reading, i ommited lots of other things //=====================code begin================================...
13
by: Adam Hartshorne | last post by:
Hi All, I was wondering if anybody can tell me what is wrong with the following code, in a .h file I have std::list<std::vector<Site> > positions ; std::list<std::vector<Site> >::iterator...
3
by: rahul8143 | last post by:
hello, I write a following program and have problem in understanding constructors and destructors. #include <iostream.h> class vector { public: double x; double y;
0
by: rokuingh | last post by:
ok, so i've been working on this one for quite a while, and the code is very big so i'm just going to give the relevant parts. this is a program that builds polymers (chemical structures of repeated...
4
by: Prasad | last post by:
HI. I have written the following code..in VC++(win32 console with MFC support) CMapStringToPtr chat; vector<UserMessage *> v; /* UserMessage is a class.. */
5
by: Pradeep | last post by:
Hi All, I am facing some problem using istream_iterator for reading the contents of a file and copying it in a vector of strings.However the same thing works for a vector of integers. The...
11
by: Brian | last post by:
Dear Programmers, I have a class with a pointer to an array. In the destructor, I just freed this pointer. A problem happens if I define a reference to a vector of this kind of class. The...
8
by: Mike Jolley | last post by:
Hello First off, I'm a student so I'm pretty new to C++, and therefore I have probably made a stupid mistake somewhere. Anyway Ive been trying to fix this 5 hours straight now, so i need a...
2
by: bingo | last post by:
Hi, All: I was really new to C++, so please forgive me if I asked stupid questions. I was trying to use this following Vector class(not written by me) to do some calculations : ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.