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;
}
}