Single list with++-indicator next



  • From the text file, the command line argument is a file flow. It contains the words separated from the ported line and starting with one of three signs (+,-?). There's a single contact list inside.

    #include  <iostream>
    #include  <fstream>
    #include  <cstdio>
    #include  <cstring>
    

    using namespace std;

    class List
    {
    public:
    void chk_list();
    char word[30];
    List * next;
    List * head;
    List();
    };

    List::List()
    {
    head = NULL;
    }

    void ShiftLeft(char buffer[30])
    {
    for(int i = 0; i<30 ; i++)
    {
    buffer[i] = buffer[i+1];
    }
    }

    int main(int argc, char *argv[])
    {
    int count = 0;
    List * stuff;
    ifstream input_file(argv[1]);
    char buffer[30];

        while(!input_file.eof())
        {
                if(buffer[0] == '+')
                {       
                        ShiftLeft(buffer);
                        strcpy(stuff-&gt;word, buffer);
                        cout&lt;&lt;"добавлено :   "&lt;&lt; stuff-&gt;word &lt;&lt;"\n"
                        stuff=stuff-&gt;next;
                        count++;
                }
    
                input_file&gt;&gt;buffer;
        }
    

    return 0;
    }

    Problem with the index next. The cycle below is performed once and even puts on the list and puts out the first word, when it is repeated, it shows Segmentation Fault in the first reference to "stuff->wordstrcpy()♪ Well, if before. strcpy() try to take out "stuff->word"and it's locked up on the conclusion, which is a segmentation fault when he first mentioned it.

     while(!input_file.eof())
    {
    if(buffer[0] == '+')
    {
    ShiftLeft(buffer);
    strcpy(stuff->word, buffer);
    cout<<"добавлено : "<< stuff->word <<"\n"
    stuff=stuff->next;
    count++;
    }

                input_file&gt;&gt;buffer;
        }
    

    How to fix this and the secret of the indexes to the next element of the list. What am I doing wrong?



  • Appointment of this function is unclear

    void ShiftLeft(char buffer[30])
    {
            for(int i = 0; i<30 ; i++)
            {
                    buffer[i] = buffer[i+1];
            } 
    }
    

    Moreover, this function is the source of the programme &apos; s uncertain behaviour, as it is in the case of i equal 29 Remembrance outside the scope of the proposal

    buffer[i] = buffer[i+1];
                       ^^^^
    

    If you do this to remove the leading symbol from the set. "+, -, ?"there is no need for this because the reference line could be replicated from the list not the first symbol, but from the second, for example.

    strcpy( stuff->word, buffer + 1 );
    

    Naturally, instead of symbolic masses, it would be better and more reliable to use standard class. std::string

    The list should be defined as follows:

    class List
    {
    private:
        struct Node
        {
            char word[30];
            Node *next;
        } *head = nullptr;
    public:
        void chk_list();
    };
    

    A copy designer and a transfer operator could be identified as remote.

    The main cycle is also a source of uncertain programme behaviour, as the variable buffer not initiated

    //...
    char buffer[30];
    

    while(!input_file.eof())
    {
    if(buffer[0] == '+')
    ^^^^^^^^^^^^^^^^^^^
    //...

    Better use the method for reading words getlinethe size of the symbolic mass. For example,

    while ( input_file.getline( buffer, sizeof( buffer ) ) )
    //...

    You should also add to the list definition a method that would include new elements in the list.

    You have to determine whether you add new elements to the list, or to the end of the list. What you're doing now, just recording.

    stuff=stuff->next;

    naturally incorrect. You don't give memory to the new element.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2