CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2009
    Location
    Israel
    Posts
    1

    why cout doesn't print my string?

    First of all, I tried to search for similar questions, and took a look at the FAQ, but I didn't found anything similar enough.

    Here's my problem:

    I have got a structure declared as following:

    struct pizza
    {
    char * name;
    string tyname;
    float diam;
    float weight;
    };

    and the following function, that declares a new struct from the type above.
    It uses input to initialize the variables, the important ones are the char array, and the string.
    Afterward it returns the new struct.

    pizza NewPizza()
    {
    pizza newpizza;
    char name[100];
    cout << "What is the company of the pizza? ";
    cin.getline(name, 100);
    cout << "please enter the type of the pizza: ";
    getline(cin, newpizza.tyname);
    cout << "Diameter: ";
    cin >> newpizza.diam;
    cout << "Weight: ";
    cin >> newpizza.weight;
    newpizza.name = new char[strlen(name)+1];
    strcpy(newpizza.name, name);
    return newpizza;
    }

    and my main() looks like that:

    int main()
    {
    pizza * first = &NewPizza();
    cout << endl << first->name << "'s- " << first->tyname << ", weights " << first->weight
    << ", and it's size is " << first->diam;
    cin.get();
    cin.get();
    return 0;
    }

    Well everything seems to work just fine, except that cout doesn't prints first->tyname.
    it does prints it when I initialize the struct returns by the function into a new declared struct.
    And it does prints it when i try to print it inside the function.
    As well, it does prints a pointer to a string when i declare the string inside the function.

    Since I'm returning the struct I don't think it got something to do with the scope, nor with the output stream because i tried to clear it before the print of the string.

    thanks for your help...

  2. #2
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: why cout doesn't print my string?

    your code
    Code:
    pizza  *first = &NewPizza();
    lets first point to the temporary object returned by NewPizza()

    make it
    Code:
    pizza  first = NewPizza();
    and everything works.

  3. #3
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: why cout doesn't print my string?

    You should also add copy constructor, which allocates memory for 'name' member. Why?
    Well, the class object may be copied into another (one or more), if any of them change the content of name, all object's 'name' would be changed!
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  4. #4
    Join Date
    Dec 2008
    Posts
    56

    Re: why cout doesn't print my string?

    Let's start by cleaning up the code, and more importantly placing it within CODE tags so that it is easier to read.

    First, avoid using char arrays in C++; instead use the STL string container whenever appropriate.

    For your Pizza structure, consider the following:
    Code:
    struct Pizza
    {
       Pizza();
       Pizza(const std::string& vendor, const std::string& type, const float diam, const float weight);
    
       std::string vendor;
       std::string type;
       float diam;
       float weight;
    };
    Note that I have changed the data type of "name" to be an STL string, and in the course of creating this post, renamed it to "vendor". Also structs, just like classes, can have a constructor and methods too, thus I have added a couple of constructor declarations. Declaring the no-arg constructor is probably not the best thing to do, but I did it merely to demonstrate your code a bit further.

    So here is the implementation of the Pizza constructors:
    Code:
    Pizza::Pizza()
    {
       using std::cout;
       using std::cin;
    
       cout << "Pizza company? ";
       cin  >> vendor;
    
       cout << "Type of pizza? ";
       cin  >> type;
    
       cout << "Diameter of pizza? ";
       cin  >> diam;
    
       cout << "Weight of pizza? ";
       cin  >> weight;
    }
    
    Pizza::Pizza(const std::string& v, const std::string& t, const float d, const float w)
       : vendor(v),
         type(t),
         diam(d),
         weight(w)
    {
    }
    There you have it... a Pizza struct/object and its implementation. Now to use it:
    Code:
    int main()
    {
       Pizza p1;     // note memory allocation is not needed!  No-arg Pizza() constructor called here.
    
       // If you want to call the Pizza constructor that takes parameters (probably best idea), then...
       std::string vendor;
       std::string type;
       float diam;
       float weight;
    
       std::cout << "Pizza company? ";
       std::cin  >> vendor;
    
       std::cout << "Type of pizza? ";
       std::cin  >> type;
    
       std::cout << "Diameter of pizza? ";
       std::cin  >> diam;
    
       std::cout << "Weight of pizza? ";
       std::cin  >> weight;
    
       Pizza p2(vendor, type, diam, weight);   // once again, no allocation needed.
    
       // to display Pizza data
       std::cout << "Vendor: " << p2.vendor << "\n"
                  << "Type: " << p2.type << "\n"
                  << "Diam: " << p2.diam << "\n"
                  << "Weight: " << p2.weight << std::endl;
    }
    Of course, it would be preferable to provide the Pizza struct with a friend method that would display the pizza attributes so that a statement such as the following could be used:
    Code:
       std::cout << p2 << std::endl;
    To pull this off, augment the Pizza struct with the following declaration:
    Code:
    struct Pizza
    {
       ...
    
       friend std::ostream& operator<<(std::ostream& os, const Pizza& p);
    };
    
    // Implement friend method
    std::ostream& operator<<(std::ostream& os, const Pizza& p)
    {
       os << "Vendor: " << p.vendor << "\n"
         << "Type: " << p.type << "\n"
         << "Diam: " << p.diam << "\n"
         << "Weight: " << p.weight;
    
       return os;
    }
    I was pressed for time, so I have not compiled any of the ideas presented above, but hopefully it will assist you with your exercise.

    In conclusion, I hope a few lessons were learned:
    1) Implement structs and/or classes with the methods that assist in defining the object; avoid declaring the methods outside of the struct/class.

    2) Use memory allocation from the heap sparingly (ie avoid using "new"), for in most situations it is not required.

    3) Avoid mixing C and C++ concepts. One should never have a need to use the likes of strcpy() in a C++ program.
    Last edited by dwhitney67; September 17th, 2009 at 08:05 AM.

  5. #5
    Join Date
    Sep 2009
    Location
    Israel
    Posts
    1

    Re: why cout doesn't print my string?

    Thank you guys.
    I didn't tried it yet (although I'm sure it will work...) because I don't have the time right now.
    But from a quick glance I already saw that there's things i could learn here, which I appreciate even more than just telling me the correct way to do it- so thx.

    (btw- why everything sounds so goddamn polite in English?)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured