The problem:
Will not compile, i get the following errors:
"invalid conversion from char to char*"
"invalid conversion from char to const char*"

Program Description:
Opens a file and read each character into a linked list

#include <iostream>
#include <fstream>
using namespace std;

const int MAX = 30;

struct node
{
        char base;
        node* next;
};
node* head;

void initialise();
void insertion(char);


int main()
{
          ifstream ins;
          char filename[MAX];
          int count;
          char x;
          
          initialise();
          
          cout << "Enter the name of the file you wish to open: " <<endl;
          cin >> filename;
          
          ins.open(filename, ios::in); 
          
          if(ins.good())
          {
                        cout<<"File opened successfully\n" <<endl;
                        
                        while(!ins.eof())
                        {
                                ins.get(x);
                                insertion(x);
                                
                                count++;    
                        }
             
          }
          else
          {
                        cerr <<"Error opening the file" <<endl;
          }
          
          return 0;
}

void initialise()
{
	head = NULL;
}

void insertion(char x)
{

        node* tmp = new node;

        if (tmp == NULL)
        {
                return;
        }
               	                
        strcpy(tmp->base, x);	// This is the problem line
        tmp->next = NULL;
	                        
         if (head == NULL)
        {
                head = tmp;
        }
       else
       {
      	node* newhead = head;
      	
                while (newhead->next != NULL)
                {
                        newhead = newhead->next;
      	}

      	newhead->next = tmp;
       }                       
                    
}

Read in your textbook or online about the parameter to ins.get() -- it takes a pointer and you are passing a char. Whenever you get an error like this one you need to look up the function in your textbook or google for it so that you can verify for yourself that you have the right or wrong parameters.

You were nearly right! Here's a correction--

#include <iostream>
#include <fstream>
using namespace std;

const int MAX = 30;

struct node
{
    char base;
    node* next;
};
node* head;

void initialise();
void insertion(char);
void printlist(node& root);

int main()
{
    ifstream ins;
    char filename[MAX];
    int count = 0; // initialize it! =)
    char x;

    initialise();

    cout << "Enter the name of the file you wish to open: " <<endl;
    cin >> filename;

    ins.open(filename, ios::in);

    if(ins.good())
    {
        cout<<"File opened successfully\n" <<endl;

        while(!ins.eof())
        {
            ins.get(x);
            insertion(x);

            count++;
        }

    }
    else
    {
        cerr <<"Error opening the file" <<endl;
    }

    printlist(*head);

    return 0;
}

void printlist(node& root){
    node* temp = &root;

    if(temp != NULL){
        while(temp->next != NULL){
            std::cout << temp->base << std::flush;
            temp = temp->next;
        }
    }else std::cout << "List is empty" << std::endl;

}

void initialise()
{
    head = NULL;
}

void insertion(char x)
{
    node* tmp = new node;

    if (tmp == NULL)
    {
        return;
    }

    //strcpy(tmp->base, x);
    tmp->base = x; // no need to allocate memory for this char
    tmp->next = NULL;

    if (head == NULL)
    {
        head = tmp;
    }
    else
    {
        node* newhead = head;

        while (newhead->next != NULL)
        {
            newhead = newhead->next;
        }

        newhead->next = tmp;
    }
}

I'm sure it can be done tons of better ways, but you were incredibly close.

There was no need to allocate memory to the non-pointer declared in your struct. Although it was not initialized, it is not a pointer. It is simply an "object" that exists on the stack and will continue to do so until it falls out of scope (in which, it will when the class no longer exists).

You should follow up on AD's advice and study about scope and object initialization. It'll help you understand when to do something (like allocate memory, etc).

The printlist function was necessary for further debugging (to see if the file was being read, etc). Keep or remove it if you wish, but there are some things about that function that could use an improvement (could be const node& root or node* root parameter), though I'm not sure if these have been discussed in your class yet.

>>Here's a correction
I'm afraid not. Line 38 is wrong.

thanks alot Alex, your a legend, problem solved.. thanks for the advice also

>>Here's a correction
I'm afraid not. Line 38 is wrong.

Oh, I didn't even catch that @_@.

Looks like I need to do more I/O in C++ =/

Edit: After reading this link I suppose it is better to use ifstream.get() and assign it to the char? char c = ins.get();

Change the problem line to:

tmp->base = x;

strcpy function is only valid with strings (array of chars). In your case you are copying simple character, and you need to use the assignment operator to do this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.