LInked list Problems, Please help

Discussion in 'C++' started by Rylios@comcast.net, Dec 5, 2005.

  1. Guest

    I am trying to make a very basic text editor using a linked list,
    fstream, sorting...

    This is what i have so far
    ------------------------------------------------------------------------------------------------------------------------------------
    The Linked.H --- Header file

    #include <iostream>

    using namespace std;

    struct NodeType
    {
    char Data;
    NodeType *Address;
    };

    //global variables
    NodeType *list;
    NodeType *before;
    NodeType *here;
    NodeType *after;
    NodeType *NewNode;

    //Prototypes
    void AddNode(char item);
    void Search(char Sv,bool & found);
    void Insert(char item,char Sv);
    void Delete(char item);
    void Display();

    void main()
    {
    bool found;
    char Sv,item;
    int input;
    list = NULL;
    before = NULL;
    here = NULL;
    after = NULL;
    NewNode = NULL;



    cout<<" Please Choose from the following menu\t";
    cout<<"1. Add a Company\n";
    cout<<"2. Delete a Company\n";
    cout<<"3. Insert a Company\n";
    cout<<"4. Display\n";
    cout<<"5. Exit\n";
    cin >>input;


    switch (input)
    {

    case 1:
    AddNode();
    break;

    case 2:
    Delete();
    break;

    case 3:
    Insert();
    break;

    case 4:
    Display();
    break;

    default:
    cout<<"Invalid option!"<<endl;
    break;
    }


    void AddNode(char item)
    {
    NewNode= New NodeType; //Creates a new node
    NewNode->Data=item; // Places item in the data field
    NewNode->Address=list; //Places the address of the next node into the
    address field
    list=NewNode; //updates the address of the list
    }

    void Display() // displays the data to the screen
    {
    here = list;
    while(here != NULL)
    {
    cout<<here->Data<<" ";
    here= here->Address;
    }
    }

    void Search(char Sv, bool & found)
    {
    here = list;
    found = false; before = list;
    while((here != NULL) && ( !found))
    {
    if(Sv == here->Data)
    {
    found=true;
    after=here->Address;
    }

    else
    {
    before=here;
    here=here->Address;
    }
    }
    void Delete(char item)
    {
    bool found;
    Search(item, found);
    if(found)
    {
    if(here==list)
    {
    list=after;
    delete here;
    }
    else{ before->Address=after;
    delete here;
    }
    else
    cout<<"Cannot delete item"<<item<<"not in the list"<<endl;
    }
    }

    void Insert(char item, char Sv)
    {
    bool found;
    Search(Sv,found)
    if(found)
    {
    NewNode = New NodeType; NewNode->Data=item;
    here->Address=NewNode;
    NewNode->Address=after;
    cout<<item<<"added to the list after"<<Sv<<endl;
    }
    else
    cout<<"Cannot Insert"<<Sv<<"not found";


    }

    -------------------------------------------------------------------------------------------------------------------------------
    The readin/display file

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cassert>
    #include <cfloat>
    //#include "Linked.h"

    using namespace std;


    string company;
    string inputFileName;

    int main ()
    {
    cout<<"This Program Is Designed To Modify A File(s)";
    cout<<"\n Please Enter The Complete File Name\n";
    cin>>inputFileName; //takes in the file name specified by the user
    ifstream inStream;
    ofstream outStream; //establishes connection and check for success
    inStream.open(inputFileName.data());
    assert(inStream.good());
    int count=0;

    for(;;)
    {
    inStream>>company;
    if(inStream.eof()) break;
    cout<<"The File Contains"<<company<<"\n";
    }
    inStream.close();
    return 0;

    }

    ---------------------------------------------------------------------------------------------------------------------------------

    My main problem is i'm getting a message that says local definitions
    are illegal
    I'm not sure what it could be, i have searched a few forums/sites and
    they all state you probably are missing a brace but when i look it
    seems fine...

    Any help would be greatly appriciated

    Thnanks very much....

    Ryl
    , Dec 5, 2005
    #1
    1. Advertising

  2. * :
    > I am trying to make a very basic text editor using a linked list,
    > fstream, sorting...
    >
    > This is what i have so far
    > ------------------------------------------------------------------------------------------------------------------------------------
    > The Linked.H --- Header file
    >
    > #include <iostream>
    >
    > using namespace std;


    Don't put that in a header file.


    > struct NodeType
    > {
    > char Data;
    > NodeType *Address;
    > };


    What is 'Address'? Use self-descriptive names.



    > //global variables
    > NodeType *list;
    > NodeType *before;
    > NodeType *here;
    > NodeType *after;
    > NodeType *NewNode;


    Don't use global variables.


    > //Prototypes
    > void AddNode(char item);
    > void Search(char Sv,bool & found);
    > void Insert(char item,char Sv);
    > void Delete(char item);
    > void Display();




    > void main()


    'main' must have result type 'int'.

    Don't put a 'main' function in a header file.


    > {
    > bool found;
    > char Sv,item;
    > int input;
    > list = NULL;
    > before = NULL;
    > here = NULL;
    > after = NULL;
    > NewNode = NULL;
    >
    >
    >
    > cout<<" Please Choose from the following menu\t";
    > cout<<"1. Add a Company\n";
    > cout<<"2. Delete a Company\n";
    > cout<<"3. Insert a Company\n";
    > cout<<"4. Display\n";
    > cout<<"5. Exit\n";
    > cin >>input;
    >
    >
    > switch (input)
    > {
    >
    > case 1:
    > AddNode();


    Your prototype specifies an argument, in this call there is none.


    > break;
    >
    > case 2:
    > Delete();
    > break;
    >
    > case 3:
    > Insert();
    > break;
    >
    > case 4:
    > Display();
    > break;
    >
    > default:
    > cout<<"Invalid option!"<<endl;
    > break;
    > }


    You probably want to put that menu thing in a loop.



    > void AddNode(char item)
    > {
    > NewNode= New NodeType; //Creates a new node
    > NewNode->Data=item; // Places item in the data field
    > NewNode->Address=list; //Places the address of the next node into the
    > address field
    > list=NewNode; //updates the address of the list
    > }
    >
    > void Display() // displays the data to the screen
    > {
    > here = list;
    > while(here != NULL)
    > {
    > cout<<here->Data<<" ";
    > here= here->Address;
    > }
    > }
    >
    > void Search(char Sv, bool & found)
    > {
    > here = list;
    > found = false; before = list;
    > while((here != NULL) && ( !found))
    > {
    > if(Sv == here->Data)
    > {
    > found=true;
    > after=here->Address;
    > }
    >
    > else
    > {
    > before=here;
    > here=here->Address;
    > }
    > }


    Aren't you missing something here?


    > void Delete(char item)
    > {
    > bool found;
    > Search(item, found);
    > if(found)
    > {
    > if(here==list)
    > {
    > list=after;
    > delete here;
    > }
    > else{ before->Address=after;
    > delete here;


    Aren't you missing something here?


    > }
    > else




    Aren't you missing something here?

    > cout<<"Cannot delete item"<<item<<"not in the list"<<endl;
    > }
    > }


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Dec 5, 2005
    #2
    1. Advertising

  3. Will Guest

    I dislike when other's replay to an issue without addressing the
    problem, but the following is just FYI...

    Unless this example is for an educational institution that is forcing
    you to learn the fundamentals of linked lists, or you are running on an
    embedded environment where your binary size is an issue... I would
    recommend that you use STL lists or other container classes in leu of
    creating your own. STL is easy to use has may features and is fully
    tested. Using STL allows you to pay more attention to your business
    logic and less on the implimentation of your linked list.

    - Will
    Will, Dec 5, 2005
    #3
  4. * Will:
    > I dislike when other's replay to an issue without addressing the
    > problem, but the following is just FYI...


    Please read up on quoting, Will.

    And also on netiquette in general.

    Since I was the only one answering I assume you're talking about my
    posting; there's no truth in your allegation.


    > Unless this example is for an educational institution that is forcing
    > you to learn the fundamentals of linked lists,


    That is a reasonable assumption.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Dec 5, 2005
    #4
  5. Guest

    He was actually refering to himself.
    I dislike ..., *but* the following ...
    Regards,
    Marc
    , Dec 5, 2005
    #5
  6. John Carson Guest

    <> wrote in message
    news:
    > He was actually refering to himself.
    > I dislike ..., *but* the following ...
    > Regards,
    > Marc


    You are probably right, but in that case it was a curious choice to reply to
    Alf rather than to the OP.

    --
    John Carson
    John Carson, Dec 5, 2005
    #6
  7. wrote:

    > I am trying to make a very basic text editor using a linked list,
    > fstream, sorting...
    >
    > This is what i have so far
    >

    ------------------------------------------------------------------------------------------------------------------------------------
    > The Linked.H --- Header file
    >
    > #include <iostream>
    >
    > using namespace std;
    >
    > struct NodeType
    > {
    > char Data;
    > NodeType *Address;
    > };
    >
    > //global variables
    > NodeType *list;
    > NodeType *before;
    > NodeType *here;
    > NodeType *after;
    > NodeType *NewNode;
    >
    > //Prototypes
    > void AddNode(char item);
    > void Search(char Sv,bool & found);
    > void Insert(char item,char Sv);
    > void Delete(char item);
    > void Display();
    >
    > void main()
    > {
    > bool found;
    > char Sv,item;
    > int input;
    > list = NULL;
    > before = NULL;
    > here = NULL;
    > after = NULL;
    > NewNode = NULL;
    >
    >
    >
    > cout<<" Please Choose from the following menu\t";
    > cout<<"1. Add a Company\n";
    > cout<<"2. Delete a Company\n";
    > cout<<"3. Insert a Company\n";
    > cout<<"4. Display\n";
    > cout<<"5. Exit\n";
    > cin >>input;
    >
    >
    > switch (input)
    > {
    >
    > case 1:
    > AddNode();
    > break;
    >
    > case 2:
    > Delete();
    > break;
    >
    > case 3:
    > Insert();
    > break;
    >
    > case 4:
    > Display();
    > break;
    >
    > default:
    > cout<<"Invalid option!"<<endl;
    > break;
    > }
    >
    >
    > void AddNode(char item)
    > {
    > NewNode= New NodeType; //Creates a new node
    > NewNode->Data=item; // Places item in the data field
    > NewNode->Address=list; //Places the address of the next node into the
    > address field
    > list=NewNode; //updates the address of the list
    > }
    >
    > void Display() // displays the data to the screen
    > {
    > here = list;
    > while(here != NULL)
    > {
    > cout<<here->Data<<" ";
    > here= here->Address;
    > }
    > }
    >
    > void Search(char Sv, bool & found)
    > {
    > here = list;
    > found = false; before = list;
    > while((here != NULL) && ( !found))
    > {
    > if(Sv == here->Data)
    > {
    > found=true;
    > after=here->Address;
    > }
    >
    > else
    > {
    > before=here;
    > here=here->Address;
    > }
    > }
    > void Delete(char item)
    > {
    > bool found;
    > Search(item, found);
    > if(found)
    > {
    > if(here==list)
    > {
    > list=after;
    > delete here;
    > }
    > else{ before->Address=after;
    > delete here;
    > }
    > else
    > cout<<"Cannot delete item"<<item<<"not in the list"<<endl;
    > }
    > }
    >
    > void Insert(char item, char Sv)
    > {
    > bool found;
    > Search(Sv,found)
    > if(found)
    > {
    > NewNode = New NodeType; NewNode->Data=item;
    > here->Address=NewNode;
    > NewNode->Address=after;
    > cout<<item<<"added to the list after"<<Sv<<endl;
    > }
    > else
    > cout<<"Cannot Insert"<<Sv<<"not found";
    >
    >
    > }
    >
    >

    -------------------------------------------------------------------------------------------------------------------------------
    > The readin/display file
    >
    > #include <iostream>
    > #include <fstream>
    > #include <string>
    > #include <cassert>
    > #include <cfloat>
    > //#include "Linked.h"
    >
    > using namespace std;
    >
    >
    > string company;
    > string inputFileName;
    >
    > int main ()
    > {
    > cout<<"This Program Is Designed To Modify A File(s)";
    > cout<<"\n Please Enter The Complete File Name\n";
    > cin>>inputFileName; //takes in the file name specified by the user
    > ifstream inStream;
    > ofstream outStream; //establishes connection and check for success
    > inStream.open(inputFileName.data());
    > assert(inStream.good());
    > int count=0;
    >
    > for(;;)
    > {
    > inStream>>company;
    > if(inStream.eof()) break;
    > cout<<"The File Contains"<<company<<"\n";
    > }
    > inStream.close();
    > return 0;
    >
    > }
    >
    >

    ---------------------------------------------------------------------------------------------------------------------------------
    >
    > My main problem is i'm getting a message that says local definitions
    > are illegal
    > I'm not sure what it could be, i have searched a few forums/sites and
    > they all state you probably are missing a brace but when i look it
    > seems fine...
    >
    > Any help would be greatly appriciated
    >
    > Thnanks very much....
    >
    > Ryl


    It seems you're defining functions inside the scope of function main().
    For instance: AddNode is declared globally (which is correct off course),
    but you try to define it inside main's scope. This is illegal.
    Some compilers, though, offer the possibility to define local functions as
    an extension to the standard.

    Good luck.
    Amedeo Verita, Dec 7, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrej Hocevar
    Replies:
    1
    Views:
    470
    Nick Austin
    Aug 18, 2003
  2. fool
    Replies:
    14
    Views:
    491
    Barry Schwarz
    Jul 3, 2006
  3. joshd
    Replies:
    12
    Views:
    652
    John Carson
    Oct 2, 2006
  4. RBCC

    Please Help Linked list problems!

    RBCC, Jan 20, 2011, in forum: C Programming
    Replies:
    0
    Views:
    242
  5. RBCC

    Please Help Linked list problems!

    RBCC, Jan 20, 2011, in forum: C Programming
    Replies:
    3
    Views:
    317
    Eric Sosman
    Jan 24, 2011
Loading...

Share This Page