Thread: my node

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    47

    Post my node

    well i tryed changing this around alot and this is the way i have it and got the least amount of errors i was trying to make a node but it really didnt work out.. this was just to get used to them no real point in it..
    ok well i have left the part i think is wrong in red..
    Code:
    #include <iostream>
    
    using namespace std;
    
    struct node
    {
           int start_up;
           float p;
           int position;
           int sprint;
           int total;
           node* next;
    };
    
    class database
    {
          private:
                  node one;
                  node two;
                  node three;
                  
                  one.next = &two;
                  two.next = &three;
          public:
                 void football_one();
                 void football_two();
                 void football_three();
    };
    
    void database::football_one()
    {
        cout<<"How long it the start up. (in yards)"<<endl;
        cin>> one.start_up;
        cout<<"How long is the sprint? (in yards)"<<endl;
        cin>> one.sprint;
        one.total = one.start_up + one.sprint;
        cout<<"you have to run :"<<one.total<<" yards."<<endl;
        cout<<"Now to find out how fast you will run. \nEnter you yard position"<<endl;
        cin>> one.position;
        cout<<"You will be running at :";
        if ((100 * one.position) / one.sprint >= one.sprint)
        {
                 cout<<"100%"<<endl;
        }
        else
        {
            one.p = (100 * one.position) / one.sprint;
            cout<< one.p<<"%"<<endl;
        }
    }
    void database::football_two()
    {
        cout<<"How long it the start up. (in yards)"<<endl;
        cin>> two.start_up;
        cout<<"How long is the sprint? (in yards)"<<endl;
        cin>> two.sprint;
        one.total = two.start_up + two.sprint;
        cout<<"you have to run :"<<two.total<<" yards."<<endl;
        cout<<"Now to find out how fast you will run. \nEnter you yard position"<<endl;
        cin>> two.position;
        cout<<"You will be running at :";
        if ((100 * two.position) / two.sprint >= two.sprint)
        {
                 cout<<"100%"<<endl;
        }
        else
        {
            one.p = (100 * two.position) / one.sprint;
            cout<< three.p<<"%"<<endl;
        }
    }
    void database::football_three()
    {
        cout<<"How long it the start up. (in yards)"<<endl;
        cin>> three.start_up;
        cout<<"How long is the sprint? (in yards)"<<endl;
        cin>> three.sprint;
        one.total = three.start_up + three.sprint;
        cout<<"you have to run :"<<three.total<<" yards."<<endl;
        cout<<"Now to find out how fast you will run. \nEnter you yard position"<<endl;
        cin>> three.position;
        cout<<"You will be running at :";
        if ((100 * three.position) / three.sprint >= three.sprint)
        {
                 cout<<"100%"<<endl;
        }
        else
        {
            one.p = (100 * three.position) / one.sprint;
            cout<< three.p<<"%"<<endl;
        }
    }
    
    int main()
    {
        football_one();
        football_two();
        football_three();
    }

  2. #2
    *this
    Join Date
    Mar 2005
    Posts
    498
    Code:
    class database
    {
          private:
                  node one;
                  node two;
                  node three;
          public:
                 database () { one.next = &two; two.next = &three; };   //private data needs to be initalized
                 void football_one();
                 void football_two();
                 void football_three();
    };
    Code:
    int main()
    {
        database d;
        d.football_one();
        d.football_two();
        d.football_three();
    }

  3. #3
    *this
    Join Date
    Mar 2005
    Posts
    498
    Well you now know how to use nodes, but you didnt utilize them. in your program the *next is useless because you inputted the data and outputted the data without cycling through the nodes. Maybe you should make a print function that prints each node and cycles through. Ill post some example code from a Store program I created that is sort of similar.

  4. #4
    *this
    Join Date
    Mar 2005
    Posts
    498
    Here is some example code that can help you see how to output linked Nodes and how to fiddle with them.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    47
    ok but i still get some error here :
    Code:
    class database
    {
          private:
                  node one;
                  node two;
                  node three;
                  
                  one.next = &two;
                  two.next = &three;
          public:
                 database () { one.next = &two; two.next = &three; };
                 void football_one();
                 void football_two();
                 void football_three();
    };

  6. #6
    *this
    Join Date
    Mar 2005
    Posts
    498
    Ya, because in a class you have private and public data members and functions. So that means you cant do any assigning in that part, only within functions.

    Basically you can only declare variables and functions.

    Thats why I moved it to the default constructor database() that is called each time you create a database object. So in that function surrounded by the brackets, it does what you had before.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Debugging my AVL tree program.
    By Nextstopearth in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 01:48 AM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM