Help with C++

Discussion in 'OT Technology' started by hurleyint1386, Apr 6, 2007.

  1. hurleyint1386

    hurleyint1386 Someone has sand in their vagina

    Joined:
    Jan 6, 2005
    Messages:
    3,687
    Likes Received:
    0
    Location:
    Rochester, NY
    So I'm not the best programmer, I need a lot of help. I've got 3 files, list.h, list.cpp and main.cpp. I can't figure out how to pass a string variable to the void function in list.cpp from main.cpp. IIRC, I might have to pass it by reference, but I can't remember exactly how to do that. Could any one give me a mini-lecture on using strings with classes? Thanks a lot
     
  2. antiyou

    antiyou OT Supporter

    Joined:
    Jul 13, 2005
    Messages:
    25,295
    Likes Received:
    0
    Location:
    in ur base
    cstrings or string class?
     
  3. hurleyint1386

    hurleyint1386 Someone has sand in their vagina

    Joined:
    Jan 6, 2005
    Messages:
    3,687
    Likes Received:
    0
    Location:
    Rochester, NY
    string class
     
  4. hurleyint1386

    hurleyint1386 Someone has sand in their vagina

    Joined:
    Jan 6, 2005
    Messages:
    3,687
    Likes Received:
    0
    Location:
    Rochester, NY
    ok... i think it might kinda work, but every so often i get a Bus Error. I'm narrowing it down to exactly what it does when i get the error. one of them, i insert the node into the list, then it gives me a Bus Error. if i do a "cout<<"Inserting "<<name<<" into list"<<endl;" then insert it, then it works fine :confused:
     
  5. hurleyint1386

    hurleyint1386 Someone has sand in their vagina

    Joined:
    Jan 6, 2005
    Messages:
    3,687
    Likes Received:
    0
    Location:
    Rochester, NY
    ok, i narrowed it down again.. it seems to only happen when i insert nodes before the head. for example:
    This works -
    (first element) Insert: Jordan
    Insert Ryan
    Insert Mike

    This does not work -
    (first element) Insert: Jordan
    Insert Ryan
    Insert Brandon (B is before J, so Brandon would be the new Head, but it doesn't work)

    Also, if I insert "Jordan Bommelje" (my name) then it gives a Bus Error. I assume it has to do with the space. how can i avoid that? (i suppose i could break it up into 2 names)

    any ideas? I'll keep looking at it
     
  6. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    post the files, use the
    Code:
     tags
     
  7. hurleyint1386

    hurleyint1386 Someone has sand in their vagina

    Joined:
    Jan 6, 2005
    Messages:
    3,687
    Likes Received:
    0
    Location:
    Rochester, NY
    ok, so i've made some improvements. i forgot about the getline function, but now when when it asks for the name of the person to add to the list, it automatically inserts a blank and returns to the menu. here's the code i'm using:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include "list.h"
    
    using namespace std;
    
    int main()
    {
            float x;
            string database;
            ifstream dB;
            string name;
            short menuItem;
            short count=0;
            ClassRoster List;
            system("clear");
            do{
                    cout<<"Please choose a menu option: "<<endl;
                    cout<<"1. Read in database from file"<<endl;
                    cout<<"2. Insert name into list"<<endl;
                    cout<<"3. Display List"<<endl;
                    cout<<"4. Quit program"<<endl;
                    cout<<"==============================="<<endl;
                    cin>>menuItem;
                    if (menuItem == 1)
                    {
                            cout<<"Please enter the name of the database file: \n";
                            cin>>database;
                            dB.open(database.c_str());
                            do{
                                    getline(dB, name);
                                    if(name != "")
                                    {
                                            //cout<<"Inserting "<<name<<"."<<endl;
                                            List.InsertNode(name);
                                    }
                            } while (!dB.eof());
                    }
                    if (menuItem == 2)
                    {
                            cout<<"Please enter a name to insert into the list: ";
                            getline(cin, name);
                            List.InsertNode(name);
                    }
                    if (menuItem == 3)
                            List.DisplayList();
            } while (menuItem != 4);
        return 0;
    }//end main
    
    that's the main.cpp file

    here's the insert function:
    Code:
    void ClassRoster::InsertNode(string &Name)
    {
            ListNode *NewNode, *NodePtr, *PreviousNode;
            NewNode = new ListNode;
            NewNode -> Value = Name;
    
            if (!Head)
            {
                    Head = NewNode;
                    NewNode->Next = NULL;
            }
            else
            {
                    NodePtr = Head;
                    while (NodePtr != NULL && NodePtr -> Value  < Name)
                    {
                            PreviousNode = NodePtr;
                            NodePtr = NodePtr -> Next;
                    }
                    PreviousNode->Next = NewNode;
                    NewNode -> Next = NodePtr;
            }
    }
    
     
  8. Tommyboy88

    Tommyboy88 New Member

    Joined:
    Dec 21, 2001
    Messages:
    204
    Likes Received:
    0
    You can try using a dummy node refrenced by head. This dummy node will never have a value for name. Therefore, it will never be moved. This will enable you to always do inserts the same way.

    This is Java, but its very similar:

    Node<E> p = head;
    while( p != null && head.next.value < name){
    p = p.next;
    }
    p.next = new Node(name, p.next);

    This technique is called using a dummy node in a linked list. Hope this helps!
     
  9. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    This is very close to being correct. The problem is that, in the case where the while loop doesn't execute (because Name is less than Head->Value), you don't assign PreviousNode before you use it. Assign PreviousNode to NULL at the top of the function, and then check whether it's NULL before dereferencing it (PreviousNode->Next = NewNode) and you should be fine.
     
  10. hurleyint1386

    hurleyint1386 Someone has sand in their vagina

    Joined:
    Jan 6, 2005
    Messages:
    3,687
    Likes Received:
    0
    Location:
    Rochester, NY
    is this what you meant?
    Code:
    void ClassRoster::InsertNode(string &Name)
    {
            ListNode *NewNode, *NodePtr, *PreviousNode;
            NewNode = new ListNode;
            NewNode -> Value = Name;
            PreviousNode = NULL;
            if (!Head)
            {
                    Head = NewNode;
                    NewNode->Next = NULL;
            }
            else
            {
                    NodePtr = Head;
                    while (NodePtr != NULL && NodePtr -> Value  < Name)
                    {
                            PreviousNode = NodePtr;
                            NodePtr = NodePtr -> Next;
                    }
                    if (PreviousNode == NULL)
                    {
                            PreviousNode->Next = NewNode;
                            NewNode -> Next = NodePtr;
                    }
            }
    }
    
    sorry, im still having some difficulty
     

Share This Page