473,756 Members | 3,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ti me >sensed_data.se nsor_who >>
sensed_data.air craft_who
>sensed_data.IF F >sensed_data.xc or >>
sensed_data.yco r
>sensed_data.al titude >sensed_data.sp eed >>
sensed_data.cou rse
>sensed_data.Tr ack_Quality)
{
// cout << "Reading input file\n";
if (sensed_data.ti me == last_clock)
{
cout << "Adding report to list\n";
cout << "Data to add: " << sensed_data.sen sor_who << " "
<< sensed_data.air craft_who << "\n";
sensor_reports. push_back (sensed_data);
cout << "# sensor reports = " << sensor_reports. size() <<
"\n";
}

Nov 1 '06 #1
3 1275
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.ti me >sensed_data.se nsor_who >>
sensed_data.air craft_who
>sensed_data.IF F >sensed_data.xc or >>
sensed_data.yco r
>sensed_data.al titude >sensed_data.sp eed >>
sensed_data.cou rse
>sensed_data.Tr ack_Quality)
{
// cout << "Reading input file\n";
if (sensed_data.ti me == last_clock)
{
cout << "Adding report to list\n";
cout << "Data to add: " << sensed_data.sen sor_who << " "
<< sensed_data.air craft_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.ti me >sensed_data.se nsor_who >>
sensed_data.air craft_who
>sensed_data.IF F >sensed_data.xc or >>
sensed_data.yco r
>sensed_data.al titude >sensed_data.sp eed >>
sensed_data.cou rse
>sensed_data.Tr ack_Quality)
{
// cout << "Reading input file\n";
if (sensed_data.ti me == last_clock)
{
cout << "Adding report to list\n";
cout << "Data to add: " << sensed_data.sen sor_who << " "
<< sensed_data.air craft_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
10628
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 namespace std; : : vector<string>* getTyphoon()
13
2148
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================================ class Buyer{ void start(void); friend void Buyer_run(Buyer *buyer);
13
4919
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 itSteps ;
3
1743
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
2607
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 monomers) which are represented as doubly pointed noncomplete binary trees. There are three different types of monomers (hence the three different constructer calling functions) the first one is the "leaves" of the tree, the second adds length...
4
5554
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
2836
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 code that doesn't work is std::vector<std::stringvecStr; std::ifstream fstrRead("Test.txt");
11
1922
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 destruction of the assigned memory seems to call the class destructor more than once. I don't know the reason or whether I used the vector class correctly. Attached is my program. Thanks for your help. Regards,
8
1951
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 little assistance. What I'm trying to do I am using inheritance to make some bookings for a marina, which are: Booking
2
237
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 : ============================================= class Vector { public:
0
10034
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8713
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.