Thread: new operator in for() loop

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    48

    new operator in for() loop

    hi.. Im trying to assign varibles to a class[array] using the new operator in a for() loop however I am having problems.. When i cout the data, it is all blank, so the variables are never actually being set, however the data is being read in correctly to the variables... (the data exists).

    Code:
    int VAL = 200;
    book *b1[VAL];
    string title, author, publisher, month, year;
    string dummy;
    float price;
    
    getline(bookList,title,';');
    getline(bookList,author,';');
    getline(bookList,publisher,';');
    getline(bookList,month,';');
    getline(bookList,year,';');
    bookList >> price;
    getline(bookList,dummy,'\n');
    
    
    int size = 0;
    while(!bookList.eof())  {
    
       // THIS IS the line im having issues, with
       b1[size] = new book(title, author, publisher, month, year, price);
       
       getline(bookList, title,';');
       getline(bookList, author,';');
       getline(bookList, publisher,';');
       getline(bookList, month,';');
       getline(bookList,year,';');
       bookList >> price;
       getline(bookList, dummy);
       size++;
    }
    
       for(int i = 0; i < size-1; i++) {
       cout << "|" << b[i]->getTitle()
            << "|" << b[i]->getAuthor()
            << "|" << b[i]->getPublisher()
            << "|" << b[i]->getMonth() << "-" << b[i]->getYear()
            << "|" << "$" << b[i]->getPrice() << "|\n";
       }
    All of my getFunctions() simply return the variable that is suppose to be set by new..

    any help greatly appreciated.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Show the declaration and code for the book class.

    gg

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    48
    .h file

    Code:
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    class book {
            public:
                    book(string,string,string,string,string,float);
                    ~book();
                    istream getInfo(istream &in);
                    string getTitle() { return title; }
                    string getAuthor() { return author; }
                    string getPublisher() { return publisher; }
                    string getMonth() { return month; }
                    string getYear() { return year; }
                    float getPrice() { return price; }
            private:
                    string title;
                    string author;
                    string publisher;
                    string month;
                    string year;
                    float price;
    };

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    And now the constructor, destructor, and getInof() implementation.

    gg

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    48
    this is it for constructor:

    book.cxx
    Code:
    #include "book.h"
    #include <iostream>
    #include <string>
    
    using namespace std;
    book::book( string title, string author, string publisher, 
                                    string month, string year, float price) {}
    all of the getFunctions() are declared in the .h file.

    The getInfo() function is not used, and has been removed, all input is read using
    getline(fstream,variable,delimiter);

    If I cout each variable as it is read in using getline, the output is correct... the problem is with the assignment:

    b1[size] = new book(title, author, publisher, month, year, price);
    Last edited by guda; 10-08-2004 at 10:46 PM.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    The parameters that are coming into your constructor are not the same as the members of the class. The members of the class are not getting assigned a value. That's why they are "blank".
    Without changing the parameter variable names, you can assign them to the class members like so: "this->title = title" etc...

    gg

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    48
    doesnt/isnt:

    b1[size] = new book(title, author, publisher, month, year, price);

    suppose to assign the data in the parameters, to the values in the class?

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    48
    this fixed it :-).

    thanks.

    Code:
    void book::setValues(string title, string author, string publisher,
                                                            string month, string year, float price) {
            this->title=title; this->author=author; this->publisher=publisher;
            this->month=month; this->author=author; this->publisher=publisher;
    }

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Even better:
    Code:
    book::book( string& _title, string& _author, string& _publisher, 
        string& _month, string& _year, float _price)  : 
        title(_title),author(_author),publisher(_publisher),
        month(_month), year(_year), price(_price)
    {}
    1# Why is the year in string, not int?
    2# My string lib doesn't have a copy constructor. If you also don't call the c_str() method and send it to the book class string fields.
    Code:
    book::book( string& _title, string& _author, string& _publisher, 
        string& _month, string& _year, float _price)  : 
        title(_title.c_str()),author(_author.c_str()),publisher(_publisher.c_str()),
        month(_month.c_str()), year(_year.c_str()), price(_price)
    {}
    Now my string lib has a constructor with a char* argument.

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    What you want to learn from this guda is that in your code there are 3 distinct "title" string object instances:
    Code:
    b1[size] = new book(title, author, publisher, month, year, price);
    ...
    book::book( string title, string author, string publisher, 
                string month, string year, float price) 
    {
        this->title = title;
        etc...
    }
    The red instance gets copied into the the green instance, and the green instance gets copied into the blue instance. You're problem is that the last copy is never performed, which is why your data is "blank".

    gg

  11. #11
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Just a question when i saw xEraths reply, arent all names, variables or anything that starts with an underscore ('_') reserved for the standard?

  12. #12
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Quote Originally Posted by xErath
    Even better:
    Code:
    book::book( string& _title, string& _author, string& _publisher, 
        string& _month, string& _year, float _price)  : 
        title(_title),author(_author),publisher(_publisher),
        month(_month), year(_year), price(_price)
    {}
    Very good, but even a bit more better:
    Code:
    book::book( const string& title_, const string& author_, const string& publisher_, 
        const string& month_, const string& year_, float price_)  : 
        title(title_), author(author_), publisher(publisher_),
        month(month_), year(year_), price(price_)
    {}
    Quote Originally Posted by xErath
    2# My string lib doesn't have a copy constructor. If you also don't call the c_str() method and send it to the book class string fields.
    Are you saying that your string library doesn't work with the code above? Are you sure you included <string>? I'd imagine something is seriously wrong with your library installation or setup if you can't copy construct strings.

  13. #13
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> ... with an underscore ('_') reserved for the standard?
    The awnser: Leading Underscores

    gg

  14. #14
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    yes that is what I meant. Didnt know though that double underscores are also reserved, seems like I have to change my inclusion guards

Popular pages Recent additions subscribe to a feed