I'm working on a project where I need to create a Linked List from a given data file. It contains operation codes, first names, last names, addresses, cities, states, and zip codes. The problem is that there is an unknown number of spaces on the lines that contain an address and the city. An example of the data file is as follows:
A
Donald
Duck
1123 Appleberry Circle
Saint Cloud
MN
88084
A
Barry
Briches
112 New York Ave
Saint Cottleville
FL
78098
I've tried using a combination of:
infile >> FirstName >> LastName;
infile.getline(Address, 20);
infile.getline(City, 20);
infile >> State >> ZipCode;
but when I go to print it out, the output comes out really weird. Any help on the issue would be greatly appreciated. All code I have done is attached.
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string.h>
#include "list.h"
using namespace std;
int main()
{
ifstream infile("data2.txt", ios::in);
ofstream outfile("output2.txt", ios::out);
char opCode, fname[12], lname[12], address[20], city[12], state[4],
zip[6];
MailListClass mailingList;
infile >> ws >> opCode;
while (opCode != 'Q')
{
switch (opCode)
{
case 'A' : infile >> fname >> lname;
infile.getline(address, 20, '\n');
infile >> city >> state >> zip;
mailingList.addRecord(fname, lname, address, city,
state, zip, outfile);
break;
case 'P' : mailingList.printRecord(outfile);
break;
}
infile >> ws >> opCode;
}
return 0;
}
The .h and .cpp struct/class files:
#ifndef LIST_H
#define LIST_H
#include <fstream>
using namespace std;
struct node
{
char lname[12], fname[12], city[12], address[20], state[4], zip[6];
node *next;
};
class MailListClass
{
private:
node *headPtr, *currPtr, *prevPtr;
public:
MailListClass();
void addRecord(char fname[12], char lname[12], char address[20],
char city [12], char state[4], char zip[6],
ofstream &outfile);
void printRecord(ofstream &outfile);
};
#endif
list.cpp
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include "list.h"
using namespace std;
MailListClass::MailListClass()
{
headPtr = NULL;
currPtr = NULL;
prevPtr = NULL;
}
void MailListClass::addRecord(char fname[12], char lname[12], char address[20], char city[12],
char state[4], char zip[6], ofstream &outfile)
{
node *tail = new node;
tail->next = NULL;
strcpy(tail->fname, fname);
strcpy(tail->lname, lname);
strcpy(tail->address, address);
strcpy(tail->city, city);
strcpy(tail->state, state);
strcpy(tail->zip, zip);
if (headPtr != NULL)
{
currPtr = headPtr;
while (currPtr->next != NULL)
{
currPtr = currPtr->next;
}
currPtr->next = tail;
}
else
{
headPtr = tail;
}
}
void MailListClass::printRecord(ofstream &outfile)
{
currPtr = headPtr;
while (currPtr != NULL)
{
outfile << left << setw(12) << currPtr->lname;
outfile << setw(12) << currPtr->fname;
outfile << setw(20) << currPtr->address;
outfile << setw(12) << currPtr->city;
outfile << setw(6) << currPtr->state;
outfile << setw(10) << currPtr->zip << endl;
currPtr = currPtr->next;
}
}
The outfile should looks like this:
Last Name First Name Address City State Zip
Last Name First Name Address City State Zip
but with the getline() in my code, my output looks closer to this:
Last Name First Name
Address# AddressNameCityCity
Last Name First Name
Address# AddressName