#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    C++ array of indexes


    hi to all! i have a little problem with a program i made i C++.. i made a function for the class product which create an array of pointers, it steals wrong...
    Code:
    class product {  
    private:  
    string code;  
    string name;  
    string information;  
    public:  
    void search(int )};   
    void product:: void search(int m)  
    {   string productforsearch;  
    cout<<"\nPlease incert name for search:\n";  
    getline(cin,productforsearch);  
    name* X = new name[m];  
    for(i=0;i<m;i++)  {  
    if(name==productforsearch)  
    X[i]-> name=name;  
    cout << X[i]-> code=code;  }  }
    if i delete the second "void" of function the compliler appears me a lot of errors
    where i have wrong? can you help me please?
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,367
    Rep Power
    1870
    > if(name==productforsearch)
    > X[i]-> name=name;
    Name is a TYPE, not a variable.

    > void search(int )};
    The closing } on this line is messing things up as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by salem
    > if(name==productforsearch)
    > X[i]-> name=name;
    Name is a TYPE, not a variable.

    > void search(int )};
    The closing } on this line is messing things up as well.
    i understand, so that i have to do is to rename the "name"?
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,367
    Rep Power
    1870
    Meh, I got that wrong :(

    Code:
    name* X = new name[m];  
    for(i=0;i<m;i++)  {  
    if(name==productforsearch)  
    X[i]-> name=name;  
    cout << X[i]-> code=code;  }  }
    name is a class member variable (of type string).
    productforsearch is a function local variable (of type string)

    To create an array of names, you do this (do you want strings, or something else?)
    string *X = new string[m];

    You can compare the class instance name with productforsearch as you are doing at the moment.

    The string assignment would be something like
    X[i] = name;

    I don't know what you're doing with the code member variable.

    Also, there can only be zero or one matches, so large values of m won't do you much good.

    Consider using
    std::vector<std::string> X;
    as a safer alternative to allocating your own array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by salem
    Meh, I got that wrong :(

    Code:
    name* X = new name[m];  
    for(i=0;i<m;i++)  {  
    if(name==productforsearch)  
    X[i]-> name=name;  
    cout << X[i]-> code=code;  }  }
    name is a class member variable (of type string).
    productforsearch is a function local variable (of type string)

    To create an array of names, you do this (do you want strings, or something else?)
    string *X = new string[m];

    You can compare the class instance name with productforsearch as you are doing at the moment.

    The string assignment would be something like
    X[i] = name;

    I don't know what you're doing with the code member variable.

    Also, there can only be zero or one matches, so large values of m won't do you much good.

    Consider using
    std::vector<std::string> X;
    as a safer alternative to allocating your own array.

    all i want to do is the user to write a name of a product and if the name exists, to saw as the code of the product
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,367
    Rep Power
    1870
    But each instance of product contains only a single name.

    So either it matches productforsearch or it doesn't.

    Code:
    void product:: void search(int m) {
        string productforsearch;  
        cout<<"\nPlease incert name for search:\n";  
        getline(cin,productforsearch);  
        if ( name==productforsearch ) {
            // this instance of product matches the user input
        }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by salem
    But each instance of product contains only a single name.

    So either it matches productforsearch or it doesn't.

    Code:
    void product:: void search(int m) {
        string productforsearch;  
        cout<<"\nPlease incert name for search:\n";  
        getline(cin,productforsearch);  
        if ( name==productforsearch ) {
            // this instance of product matches the user input
        }
    }
    yes you got right but if 2 products had the same name we must seperate them from the code of each product, the code is unique not the name. and thats why i want to realize the code with an array of indexes for the member "name" and then returns me the code and that i do as above but the compilers shows me wrong, which is the right code with array of indexes?
  14. #8
  15. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,367
    Rep Power
    1870
    Code:
    class product {  
    private:  
    string code;  
    string name;  
    string information;
    Look at this.

    Now, ignoring the private thing so you can see what is stored
    product fruit; // an instance of your class
    fruit.code = "1";
    fruit.name = "banana";
    fruit.information = "it's yellow";

    is ONE instance of that class containing data.

    Here is another one
    product vegetable;
    vegetable.code = "2";
    vegetable.name = "cabbage";
    vegetable.information = "it's green";


    Now there is NO way you can do
    fruit.code = "3";
    fruit.name = "orange";
    fruit.information = "it's orange";

    and still have any memory that fruit contained a banana.

    If you've got multiple products, you need a container (say std::vector) to store them in.

    Your search function needs to look through some kind of 'list' of products, like say
    Code:
    void search ( std::vector<product> &inventory ) {
        string productforsearch;  
        cout<<"\nPlease incert name for search:\n";  
        getline(cin,productforsearch);  
        for ( i = 0 ; i < inventory.size(); i++ ) {
            if ( inventory[i].getName() == productforsearch ) {
                // you found it
            }
        }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0
    i understand thnx very much nice job :D

IMN logo majestic logo threadwatch logo seochat tools logo