Hey there! Sign in to join this conversationNew here? Join for free
    • Thread Starter
    Offline

    0
    ReputationRep:
    I keep getting an error when I try to run this code, I can't seem to find out how to correct the error. Could someone please help me identify the error and if there are any improvements that I can do please tell. All suggestions are welcome.

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    struct garden_tools
    {
        string nameTool[10];
        int price;
        garden_tools *next;
    };
    
    
    int main()
    {
        garden_tools *temp=NULL;
        temp=newNode;
    
    
        cout << "Please enter name: ";
        cin >> temp ->nameTool;
    
    
        cout << "Please enter price: ";
        cin >> temp ->price;
    
    
        temp -> next = NULL;
    
    
        cout << "Name " << temp ->nameTool << endl;
        cout << "Price " << temp ->price << endl;
        return 0;
    }
    Offline

    2
    ReputationRep:
    There should be a space in new Node.

    You should post the error message as well.

    PS: is Node even defined?
    • Thread Starter
    Offline

    0
    ReputationRep:
    (Original post by hevlar.kelmet)
    There should be a space in new Node.

    You should post the error message as well.

    PS: is Node even defined?
    It's not Thanks.

    I fixed the code! I had mixed it up a bit.
    Offline

    2
    ReputationRep:
    (Original post by Diiiii)
    It's not Thanks. So I should check if there's space and then insert an error message? :confused:
    It appears you're trying to create an instance of your garden_tools class, so I'd say try replacing 'newNode' with 'new garden_tools'.

    If 'Node' is actually defined, then you should stick a space between 'new' and 'node' and if there's still and error you should post the compiler error message so we can see.
    • Thread Starter
    Offline

    0
    ReputationRep:
    (Original post by hevlar.kelmet)
    It appears you're trying to create an instance of your garden_tools class, so I'd say try replacing 'newNode' with 'new garden_tools'.

    If 'Node' is actually defined, then you should stick a space between 'new' and 'node' and if there's still and error you should post the compiler error message so we can see.
    Thanks I had mixed it up.

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    struct garden_tools
    {
        string nameTool;
        int price;
    
    
    };
    
    
    int main()
    {
        garden_tools *next;
        next = new garden_tools;
    
    
        cout << "Please enter name: ";
        cin >> next ->nameTool;
    
    
        cout << "Please enter price: ";
        cin >> next ->price;
    
    
        cout << "\nName " << next ->nameTool << endl;
        cout << "Price " << next ->price << endl;
        return 0;
    }
    If I need to make the first node null, how could I code that?
    Offline

    14
    ReputationRep:
    (Original post by Diiiii)
    Thanks I had mixed it up.

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    struct garden_tools
    {
        string nameTool;
        int price;
    
    
    };
    
    
    int main()
    {
        garden_tools *next;
        next = new garden_tools;
    
    
        cout << "Please enter name: ";
        cin >> next ->nameTool;
    
    
        cout << "Please enter price: ";
        cin >> next ->price;
    
    
        cout << "\nName " << next ->nameTool << endl;
        cout << "Price " << next ->price << endl;
        return 0;
    }
    If I need to make the first node null, how could I code that?
    Is this to do with implementing linked lists?
    • Thread Starter
    Offline

    0
    ReputationRep:
    (Original post by Psyk)
    Is this to do with implementing linked lists?
    Yes
    Offline

    14
    ReputationRep:
    (Original post by Diiiii)
    Yes
    Ok, well if you want to make a linked list, each 'garden_tools' object should have a pointer to another 'garden_tools' object.

    Every time you add a new object, you make the 'next' pointer of the last one you created point to the new one. Then on the new one you set the next pointer to NULL so you can tell it's the end of the list.

    And you also need to keep a pointer to the very first object somewhere, so you can access what's in the list.
 
 
 
Reply
Submit reply
TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

Updated: March 30, 2013
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • Poll
    Would you like to hibernate through the winter months?
    Useful resources
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

    Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

    Quick reply
    Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.