Using a struct in a vector.

Discussion in 'C++' started by justyb11@gmail.com, Jun 13, 2005.

  1. Guest

    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
     
    , Jun 13, 2005
    #1
    1. Advertising

  2. Guest

    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);
     
    , Jun 13, 2005
    #2
    1. Advertising

  3. slack_justyb Guest

    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.
     
    slack_justyb, Jun 13, 2005
    #3
  4. Old Wolf Guest

    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::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();
    }
     
    Old Wolf, Jun 14, 2005
    #4
  5. red floyd Guest

    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.
     
    red floyd, Jun 14, 2005
    #5
  6. Swampmonster Guest


    > 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/
     
    Swampmonster, Jun 14, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pmatos
    Replies:
    6
    Views:
    24,124
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,440
    Chris Fogelklou
    Apr 20, 2004
  3. Replies:
    8
    Views:
    1,999
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    622
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    396
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page