Using a struct in a vector.

J

justyb11

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
 
C

chandra.somesh

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);
 
S

slack_justyb

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.
 
O

Old Wolf

slack_justyb said:
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::eek:stringstream 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::eek:stringstream oss;
oss << t;
return oss.str();
}
 
R

red floyd

slack_justyb said:
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.
 
S

Swampmonster

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/d...y/en-us/vcstdlib/html/vclrfsstreammembers.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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top