One of the functions of the contact list is not operational



  • Function add_after() should supplement the list after the line has been found a_word[] line a_word_after[]♪ Both lines shall be entered by the user after completing the list.
    The compiler points to the line indicated and states:

    error: incompatible types in assignment of 'char* to 'char [80]

    I understand it's impossible to assign an index to a symbol to a body of symbols? But isn't a single-dimensional mass an index to the zero element of this body? Please explain how to fix this?

    #include <iostream>
    #include <stdlib.h>
    #include <string.h>
    

    struct node
    {
    char word[80];
    node* ptrToNextNode;
    };

    void fillList(node *&list);
    void prntList(node *list);
    void makeList(node *&list);
    void add_after(node *list,char a_word[],char a_word_after[]);

    using namespace std;

    int main()
    {
    node* list;
    char a_word[80],a_word_after[80];
    fillList(list);
    prntList(list);
    cout<<"Input a_word: ";
    cin>>a_word;
    cout<<"Input a_word_after: ";
    cin>>a_word_after;
    add_after(list,a_word,a_word_after);
    prntList(list);
    return 0;
    }
    void fillList(node*& list)
    {
    makeList(list);
    cout<<"Input word end press 'Enter'(input '.' for exit): ";
    cin>>list->word;
    if(!strcmp(".",list->word))
    {
    delete list;
    list=NULL;
    }
    node *current, *last;
    current=list;
    while(current!=NULL)
    {
    last=new node;
    cout<<"Input word end press 'Enter'(input '.' for exit): ";
    cin>>last->word;
    if(!strcmp(".",last->word))
    {
    delete last;
    last=NULL;
    }
    current->ptrToNextNode=last;
    current=last;
    }
    }
    void makeList(node *&list)
    {
    list=new node;
    if(list==NULL)
    {
    cout<<"Not enough memory! Terminate program...";
    exit(1);
    }
    cout<<"List ready\n";
    }
    void prntList(node *list)
    {
    if(list==NULL)
    {
    cout<<"This list is empty\n";
    }
    else
    {
    while(list!=NULL)
    {
    cout<<list->word<<endl;
    list=list->ptrToNextNode;
    }
    cout<<"//////////End of list//////////";
    }
    }
    void add_after(node list,char a_word[],char a_word_after[])
    {
    if(list==NULL)
    {
    cout<<"This list is empty";
    }
    else
    {
    while(list!=NULL)
    {
    if(!strcmp(list->word,a_word))
    {
    node
    filling_pointer;
    makeList(filling_pointer);
    filling_pointer->word=a_word_after; //<---------------Ошибка в этой строке
    filling_pointer->ptrToNextNode=list->ptrToNextNode;
    list->ptrToNextNode=filling_pointer;
    }
    list=list->ptrToNextNode;
    }
    cout<<"//////////Pasting over//////////";
    }
    }



  • The problem is you have node: the word already points to the char.
    You need to not assign the signs, but copy from one body to another, for example: strcpy(filling_pointer->word, a_word_after)

    Or diamically redeem the memory of all the lines.


Log in to reply
 


Suggested Topics

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