473,395 Members | 1,488 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,395 software developers and data experts.

Using a struct in a vector.

Hello. I'm trying to use the following code but I continue to get
Aborted as the output.

Please note that this is compiled using gcc 3.3.4 on Linux 2.6.11.11

---
//file: vectest.cc

#include<iostream>
#include<string>
#include<vector>
#include<stdio.h>

struct foo_node {

std::string first_name;
std::string last_name;

foo_node() : first_name("John"), last_name("Doe") {}
foo_node(std::string a, std::string b) : first_name(a), last_name(b)
{}
~foo_node() {}

void operator=(const foo_node& o) { first_name = o.first_name;
last_name = o.last_name; }

}

int main() {

std::vector<foo_node> aList;

for(int x=0;x<25;x++) {

char *str;
sprintf(str, "Person%d", x);
std::string m1(str);

sprintf(str, "LastName%d", x);
std::string m2(str);

foo t1(m1, m2);
aList.push_back(t1); //<---- This is where the
program stops.
}

std::vector<foo_node>::iterator iterList;

for(iterList=aList.begin(); iterList != aList.end(); iterList++)
std::cout << iterList->first_name << "/n" << iterList->last_name
<< std::endl;

return 0;
}

//End of vectest.cc
--------------

First I know I could have used 'using namespace std' to make some of
this a lot more readable. I'm sorry about that, but this is the code
copied and pasted.

So I was wondering why it keeps giving me 'Aborted' as the output and
what I can do to correct this code.

Thank you in advance,
Justin

Jul 23 '05 #1
6 1739
Hi justin

Well ur program aborts cause u use char *str without initializing it.

char *str;
str contains garbage value
sprintf(str, "Person%d", x);
u try to store input at this garbage address (i think it
should have been sscanf)

To avoid this problem try initializing str to some reasonable value
like.
str=new char[100];

instead why don't u try

stiring str1;str2;
cout<<"Person"<<x;
cin>>str1;
cout<<"last name"<<x;
cin>>str2;
foo t1(str1,str2);

Jul 23 '05 #2
I'm such a goof! Why did I not see it before? Maybe the coding at
2:40am should stop.

My modified code is the following.

---------
//file: vectest.cc

//some code has been skipped....

for(int x=0;x<25;x++) {

//-------------------Changed code--------------------
const int SOME_VALUE = 30;
char str[SOME_VALUE];
//-------------------Changed code--------------------
sprintf(str, "Person%d", x);
std::string m1(str);

sprintf(str, "LastName%d", x);
std::string m2(str);

foo t1(m1, m2);

//Rest of code is the same as above....

//End of vectest.cc
--------------------------

I know that this was such a silly mistake on my behalf and I thank you
for pointing that out to me.
Program is now working again.

PS. The reason that cin is not used is that the program that the above
code is mocking is non-interactive and, therefore, the code above
closely represents that fact.

Jul 23 '05 #3
slack_justyb wrote:

My modified code is the following.

for(int x=0;x<25;x++) {

const int SOME_VALUE = 30;
char str[SOME_VALUE];
sprintf(str, "Person%d", x);
std::string m1(str);


If you are interested in "the C++ way" to do things, it is
to use the stream formatting operators (but send them into
a memory buffer instead of to std::cout etc.):

std::ostringstream oss;
oss << "Person" << x;
std::string m1 = oss.str();

You could wrap this code in a function if you're going to
use it often. In fact I often do something like this in my
programs:

#include <sstream>
#include <ostream>

template<typename IntT>
std::string i2s(IntT t)
{
std::ostringstream oss;
oss << t;
return oss.str();
}

Jul 23 '05 #4
Old Wolf wrote:
slack_justyb wrote:

My modified code is the following.

for(int x=0;x<25;x++) {

const int SOME_VALUE = 30;
char str[SOME_VALUE];
sprintf(str, "Person%d", x);
std::string m1(str);


If you are interested in "the C++ way" to do things, it is
to use the stream formatting operators (but send them into
a memory buffer instead of to std::cout etc.):

std::ostringstream oss;
oss << "Person" << x;
std::string m1 = oss.str();

You could wrap this code in a function if you're going to
use it often. In fact I often do something like this in my
programs:

#include <sstream>
#include <ostream>

template<typename IntT>
std::string i2s(IntT t)
{
std::ostringstream oss;
oss << t;
return oss.str();
}


That's really nice! It's the first time I've heard of stringstream.
I'll take your advice from now on. Also, and I could be wrong, since
there is an ostringstream, would that mean that there is also an
istringstream?

Also, if you know of a link about this topic on the Internet I'd love
it if you posted it here.

Thanks everyone.

Jul 23 '05 #5
slack_justyb wrote:
That's really nice! It's the first time I've heard of stringstream.
I'll take your advice from now on. Also, and I could be wrong, since
there is an ostringstream, would that mean that there is also an
istringstream?


Yep.
Jul 23 '05 #6
Also, if you know of a link about this topic on the Internet I'd love
it if you posted it here.


The MSDN contains a nice description of the C++ standard libs.
Try this for all the stringstream stuff:

http://msdn.microsoft.com/library/de...eammembers.asp

Some of it might be VC (Microsoft Visual C++) specific, but for the most
part they flag the non-standard stuff with "Microsoft-specific" or alike.

Or another iostream reference (althoug I don't know how good it is):
http://www.cplusplus.com/ref/iostream/
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Christof Krueger | last post by:
Hello, I'm quite new to C++ and have some base C knowledge. So I know how to solve a given problem, by I sometimes tend to fall back to C. That is what I want to avoid in my current project....
6
by: _mario.lat | last post by:
I have a struct like that (for a graph): struct edge { int nodea; int nodeb; int weight; } I have an array of this struct: struct e edge;
7
by: tehn.yit.chin | last post by:
I am trying to experiment <algorithm>'s find to search for an item in a vector of struct. My bit of test code is shown below. #include <iostream> #include <vector> #include <algorithm>...
10
by: The Cool Giraffe | last post by:
I got a hint recently (guess where, hehe) and was directly pointed to the vector class. Now, i have no issues using that way but i'm concerned about the performance issue. Which is fastest...
3
Digital Don
by: Digital Don | last post by:
I have a problem sending the Vectors as Referencial parameters..i.e. I am having a struct vector struct board { int r; int c; };
11
by: Matthew Page | last post by:
I need to pass an STL Vector to a Visual Basic program using COM. This has to be a vector of a user defined structure and it has to also be within a structure itself. As near as I can tell, I...
0
by: zman77 | last post by:
Hi. In C dll, there is a struct that contains an array of struct pointers. I do not know how to represent that in C#. That's my problem. Here is the relevant C code: struct vector { uint...
6
by: jmsanchezdiaz | last post by:
CPP question: if i had a struct like "struct str { int a; int b };" and a vector "std::vector < str test;" and wanted to push_back a struct, would i have to define the struct, fill it, and then...
2
by: beet | last post by:
Hi all, I tried to declare a c++ struct like following in a header file; I want to include this header file in other files to create and access this struct. ------ 1 #ifndef _SEARCHDATA_H_...
36
by: pereges | last post by:
Hi, I am wondering which of the two data structures (link list or array) would be better in my situation. I have to create a list of rays for my ray tracing program. the data structure of ray...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.