3 Replies - 9624 Views - Last Post: 02 June 2009 - 01:06 PM Rate Topic: -----

#1 theomegacode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-June 09

Reading from File into a Linked List

Posted 02 June 2009 - 11:54 AM

I cannot figure out what I'm doing wrong here. I have a feeling there's a problem with how I'm doing the file read and parsing, but I'm not completely sure. The program compiles, but shows no output. Any suggestions?
I've also attached the file I'm reading from.

#ifndef MAIN_H_INCLUDED
#define MAIN_H_INCLUDED

// ********************************************************************
//  File:  Display a Phone Directory
//
//		Display a Phone Directory – Read in names and phone numbers
//			from a data file and store the information in a linked list.
//			Then output the information in two left-aligned
//			columns: one for names, the other for phone numbers.
//
//
//   Author:  Cameron Hoerig
//  Created:  31 May 2009
//  Updated:
//
//  Assignment:  EECE180 Lab 10
//	Compiler:  Code::Blocks 8.02
//
//**********************************************************************
//  INPUTS:  Names and phone numbers from a data file.
//
// OUTPUTS:  The names and numbers from the data file.
//
// CONSTRAINTS: The names contain no digits, the phone numbers contain
//			  no letters.
//
// *********************************************************************
// DESIGN
//  1.0 Create linked list
//	  1.1 Create the head of a linked list
//		  1.11 The structure contains a member to hold a name, phone
//			   number, a constructor, and pointer that is initialized
//			   to NULL
//  2.0 Read in data
//	  2.1 While the end of file has not been reached, read in data
//		  2.11 Read data until a digit is encountered and store
//			   the input as the name.
//		  2.12 Read data until a letter is encountered and store the
//			   input as the phone number.
//		  2.13 Dynamically create new node
//			  2.131 If the list is empty, set the head equal to the
//					new node
//			  2.132 If the list is not empty create a new pointer
//					that is initialized to the address in the head.
//					Step the new pointer through the list until the
//					pointer points to value NULL. Then, set the pointer
//					equal to the new node.
//  3.0 Display the data
//	  3.1 Create a new node pointer and initialize it to the value of
//		  of the head. While the pointer is not equal to NULL, step the
//		  pointer through the list (= pointer -> next) and display the
//		  name and phone number in each node. The information should
//		  be in two columns, both left-aligned in their respective
//		  columns.
//
//
//**********************************************************************


#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;



#endif // MAIN_H_INCLUDED




#include "main.h"

	//
	//  1.0 Create linked list
	//	  1.1 Create the head of a linked list
	//		  1.11 The structure contains a member to hold a name, phone
	//			   number, a constructor, and pointer that is initialized
	//			   to NULL
	//

	struct PhoneList
	{
		string name;
		string phoneNumber;
		PhoneList *next;
		PhoneList(char n[], char p[], PhoneList *next1 = NULL){   //Constructor
			name = n;
			phoneNumber = p;
			next = next1;
		}
	};



int main(int argc, char *argv)
{
	const int LENGTH = 100;
	char fileRead[100];
	char name[60];
	char number[60];
	PhoneList *head; //pointer to the head of list
	PhoneList *list; //pointer used to traverse the list
	string fileName = argv;

	fstream dataFile;
	dataFile.open(argv, ios::in);

	//
	//  2.0 Read in data
	//	  2.1 While the end of file has not been reached, read in data
	//

	while(!dataFile.eof()){
		dataFile.getline(fileRead, LENGTH, '/n');

		//
		//		  2.11 Read data until a digit is encountered and store
		//			   the input as the name.
		//

		int i = 0; //set iterator. Defined outside loop because it will be
				   // used in two different loops
		for(; i < 60; i++){
			while(fileRead[i] < '48' || fileRead[i] > '57'){
				name[i] = fileRead[i];
			}
		}


		//
		//		  2.12 Read data until a letter is encountered and store the
		//			   input as the phone number.
		//

		for(int j = 0; j < 60; j++){
			while(fileRead[i] < '65' || fileRead[i] > '122'){
				number[j] = fileRead[i];
				i++; //increment i to step through fileRead
			}
		}

		//
		//		  2.13 Dynamically create new node
		//			  2.131 If the list is empty, set the head equal to the
		//					new node
		//
		if(head == NULL){
			head = new PhoneList(name, number);
		}

		//
		//			  2.132 If the list is not empty create a new pointer
		//					that is initialized to the address in the head.
		//					Step the new pointer through the list until the
		//					pointer points to value NULL. Then, set the pointer
		//					equal to the new node.
		//

		else{
			list = head;
			while(list->next != NULL){ // traverse the list until the end is reached
				list = list->next;
			}
			list->next = new PhoneList(name, number);
		}
	}

	//
	//  3.0 Display the data
	//	  3.1 Create a new node pointer and initialize it to the value of
	//		  of the head. While the pointer is not equal to NULL, step the
	//		  pointer through the list (= pointer -> next) and display the
	//		  name and phone number in each node. The information should
	//		  be in two columns, both left-aligned in their respective
	//		  columns.
	//

	list = head;
	while(list){
		cout << setw(25) << left; // set output formatting
		cout << list->name;
		cout << list->phoneNumber;
		cout << endl;
	}

	dataFile.close();

}

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Reading from File into a Linked List

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: Reading from File into a Linked List

Posted 02 June 2009 - 12:15 PM

Compiling without warnings on, or without heeding warnings, leads to issues like these. Here are the warnings:

 bash-3.2$ make garbage
g + +	 garbage.cpp   -o garbage
garbage.cpp:48:44: warning: multi-character character constant
garbage.cpp:58:33: warning: multi-character character constant
garbage.cpp:58:55: warning: multi-character character constant
garbage.cpp:70:33: warning: multi-character character constant
garbage.cpp:70:55: warning: multi-character character constant
garbage.cpp: In function ‘int main(int, char*)’:
garbage.cpp:58: warning: comparison is always true due to limited range of data type
garbage.cpp:58: warning: comparison is always false due to limited range of data type
garbage.cpp:70: warning: comparison is always true due to limited range of data type
garbage.cpp:70: warning: comparison is always false due to limited range of data type

Was This Post Helpful? 0
  • +
  • -

#3 theomegacode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-June 09

Re: Reading from File into a Linked List

Posted 02 June 2009 - 12:19 PM

I saw the warnings, and could not make sense of them. The comparison warnings contradict themselves, and I have no clue why it's saying the character arrays are constant.
Was This Post Helpful? 0
  • +
  • -

#4 theomegacode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-June 09

Re: Reading from File into a Linked List

Posted 02 June 2009 - 01:06 PM

I've made some progress. I now get stuck in the

while(!dataFile.eof())

loop. As far as the constant character arrays, that shouldn't be a problem, should it? My compiler is telling me the fileRead array is constant, which should not cause a problem as it is only being read after getline() is called.
I still don't know why it see a problem with my ASCII ranges, though.

#include "main.h"

	//
	//  1.0 Create linked list
	//	  1.1 Create the head of a linked list
	//		  1.11 The structure contains a member to hold a name, phone
	//			   number, a constructor, and pointer that is initialized
	//			   to NULL
	//

	struct PhoneList
	{
		string name;
		string phoneNumber;
		PhoneList *next;
		PhoneList(char n[], char p[], PhoneList *next1 = NULL){   //Constructor
			name = n;
			phoneNumber = p;
			next = next1;
		}
	};



int main(int argc, char *argv[])
{
	const int LENGTH = 100;
	char fileRead[130];
	char name[60];
	char number[60];
	PhoneList *head = NULL; //pointer to the head of list
	PhoneList *list = NULL; //pointer used to traverse the list

	char fileName[15];
		for(int i = 0; argv[0][i] != '\0' && i < 15; i++)
			fileName[i] = argv[0][i]; // file to open

	fstream dataFile;
	dataFile.open(fileName, ios::in);

	//
	//  2.0 Read in data
	//	  2.1 While the end of file has not been reached, read in data
	//
cout << "a" << endl;
	while(!dataFile.eof()){
		dataFile.getline(fileRead, LENGTH, '\n');

		//
		//		  2.11 Read data until a digit is encountered and store
		//			   the input as the name.
		//
cout << "b" << endl;
		int i = 0; //set iterator. Defined outside loop because it will be
				   // used in two different loops
		for(; i < 60; i++){
			if(fileRead[i] < '48' || fileRead[i] > '57'){
				name[i] = fileRead[i];
			}
			else{
				name[i] = '\0';
			}
		}

cout << "c" << endl;
		//
		//		  2.12 Read data until a letter is encountered and store the
		//			   input as the phone number.
		//

		for(int j = 0; j < 60; j++){
			if(fileRead[i] < '65' || fileRead[i] > '122'){
				number[j] = fileRead[i];
				i++; //increment i to step through fileRead
			}
			else{
				number[j] = '\0';
				i++;
			}
		}

		//
		//		  2.13 Dynamically create new node
		//			  2.131 If the list is empty, set the head equal to the
		//					new node
		//
cout << "d" << endl;
		if(head == NULL){
			head = new PhoneList(name, number);
		}

		//
		//			  2.132 If the list is not empty create a new pointer
		//					that is initialized to the address in the head.
		//					Step the new pointer through the list until the
		//					pointer points to value NULL. Then, set the pointer
		//					equal to the new node.
		//

		else{
			list = head;
			while(list->next != NULL){ // traverse the list until the end is reached
				list = list->next;
			}
			list->next = new PhoneList(name, number);
		}
	}
cout << "e" << endl;
	//
	//  3.0 Display the data
	//	  3.1 Create a new node pointer and initialize it to the value of
	//		  of the head. While the pointer is not equal to NULL, step the
	//		  pointer through the list (= pointer -> next) and display the
	//		  name and phone number in each node. The information should
	//		  be in two columns, both left-aligned in their respective
	//		  columns.
	//

	list = head;
	while(list){
		cout << setw(25) << left; // set output formatting
		cout << list->name;
		cout << list->phoneNumber;
		cout << endl;
	}
cout << "f" << endl;
	dataFile.close();

	return 0;
}



Edit: Nevermind. I forgot to add an if statement to check if an error occurred in opening the file. Apparently, that's why it was stuck in the loop.

This post has been edited by theomegacode: 02 June 2009 - 01:13 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1