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

struct phonenode{
	char surname[15];
	char initals[4];
	phonenode *next;
};

phonenode *start_ptr = NULL;
phonenode *crrnt_ptr;


void read_txt(){

	ifstream ifs("test.txt");
	char surn[15];
	char inital[4];
	phonenode *temp1, *temp2;
	temp1 = new phonenode;
	if (ifs.fail()){
		cout << "ERROR" << endl;
	}
	while(ifs.good()){
		ifs>>surn>>inital;
		if(ifs.fail()){
			break;
		}
		surn >> temp1->surname;
		inital >> temp1->initals;

		if (start_ptr == NULL){
			start_ptr = temp1;
			crrnt_ptr = start_ptr;
		}

		else{
			temp2 = start_ptr;
			while (temp2->next != NULL){
				temp2 = temp2->next;
			}
			temp2->next = temp1;
		}

	}
	ifs.close();
	
}

void main(){
	read_txt();
	system("PAUSE");
}

above code is mine.. and I'm new to C++.

in "test.txt"

abcd      H.J.
efc       K.W.

I want to read text and want to put in linked list; however, I got some errors
error C2296: '>>' : illegal, left operand has type 'char [15]'
error C2297: '>>' : illegal, right operand has type 'char [15]'
error C2296: '>>' : illegal, left operand has type 'char [4]'
error C2297: '>>' : illegal, right operand has type 'char [4]'

sorry for noob question but... how can I fix these problem?

lines 31 and 32: what are you attempting to do there? chara arrays do not have overloaded operators such as >> If you want to copy the arrays then use strcpy() function. Either read your textbook or google to find out how to use that function.

Edited 7 Years Ago by Ancient Dragon: n/a

lines 31 and 32: what are you attempting to do there? chara arrays do not have overloaded operators such as >> If you want to copy the arrays then use strcpy() function. Either read your textbook or google to find out how to use that function.

I just want to put values into linked list(surname and initals) from the text file.. okay I will try strcpy() function! :p

thanks!

strcpy(temp1->surname, surn);
		strcpy(temp1->initals, inital);

hmmmm... gave me error....

from the test.txt,
"abcd" into linked list(phonenode), surname and
"H.J." into initals.

how can I do this.... :(

Note that you have to allocate a new node on each iteration of the file read.

while( fd >> surn >> initial)
{
       temp1 = new phonenode;
       strcpy(temp1->surn, surn);
       strcpy(temp1->initial, initial);
       if( start_ptr == NULL)
      {
             // Add new node to the head of the linked list
      }
      else
       {
            // add new node to the tail of the linked list
       }
}

}

Edited 7 Years Ago by Ancient Dragon: n/a

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

struct phonenode{
	char surname[15];
	char initial[4];
	struct phonenode *next;
};


phonenode *start_ptr = NULL;
phonenode *crrnt_ptr;


void read_txt(){

	ifstream ifs("test.txt");
	char surname[15];
	char initial[4];
	phonenode *temp1, *temp2;
	temp1 = new phonenode;
	if (ifs.fail()){
		cout << "ERROR" << endl;
	}
	while( ifs >> surname >> initial){
		temp1 = new phonenode;
		strcpy(temp1->surname, surname);
		strcpy(temp1->initial, initial);
		if (start_ptr == NULL){
			start_ptr = temp1;
			crrnt_ptr = start_ptr;
		}
		else{
			temp2= start_ptr;
			while (temp2->next != NULL){
				temp2 = temp2->next;
			}
			temp2->next = temp1;
		}

	}
}

void main(){
	read_txt();
	system("PAUSE");
}

it comples but give
"Unhandled exception at 0x013f16ba in phonecompany.exe: 0xC0000005: Access violation reading location 0xcdcdcde1."

do I have to use ()malloc()? or something like that?

else{
			temp2= start_ptr;
			while (temp2->next != NULL){
				temp2 = temp2->next;
			}
			temp2->next = temp1;
		}

seems there are some errors. ummm, but I dont know what is wrong..

Edited 7 Years Ago by hur1214: n/a

you forgot to initialize temp->next = NULL; at between lines 28 and 29, so at line 37 it can not find the last node.

oh yes. I fixed that and sadly another error!
"Run-Time Check Failure #2 - Stack around the variable 'initial' was corrupted."


omg.... linked list is soo difficult..

is doing something like

ifs >> tempsur >> tempini

bad thing?

I just re-started my computer and it works w/o any errors..
omg...

Thanks, Ancient Dragon.
YOU ARE MY HERO!

awesome I love how problems are marked solved with no posted solution.. thanks for wasting internet space with half finished code...

I apologize for my previous post.. I'm new to using this site. I was rather angry at the internet for 10 more incomplete websites and threads just like this one.

This question has already been answered. Start a new discussion instead.