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)
-
phonelist.txt (926bytes)
Number of downloads: 409