data input problem

the variable zipCode is not being read in on line 73 or 85. i am guessing i am using the wrong way to input it. advice please

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
using namespace std;

const int MAX_CLIENT_SIZE = 100;
const int FIRST_NAME_LEN = 11;
const int LAST_NAME_LEN = 13;
const int ADDRESS_LEN = 25;
const int CITY_NAME_LEN = 16;
const int STATE_LEN = 3;

struct CustomerType
{
	char lastName[LAST_NAME_LEN];
	char firstName[FIRST_NAME_LEN];
	char streetAddress[ADDRESS_LEN];
	char city[CITY_NAME_LEN];
	char state[STATE_LEN];
	int zipCode;
};

void reportHeading(ostream& outfile);
int getInfo(CustomerType struct_array[], istream& infile, int& count);
void printCustomers(ostream& outfile, CustomerType output[], int& count);
void sortByLastName(CustomerType struct_array[], int& count);

int main()
{
	int count;
	CustomerType clients[MAX_CLIENT_SIZE];

	ifstream infile("PJ902_customers.txt");
	if (!infile){
		cerr << "File loading failed.\n";
		return -1;
	}
	ofstream outfile("PJ902_report.txt");
	if (!outfile)
	{
		cerr << "File output failed.\n";
		return -1;
	}
	reportHeading(outfile);
	int success = getInfo(clients, infile, count);
	cout << success << endl;
	printCustomers(outfile, clients, count);



}

void reportHeading(ostream& outfile)
{
	outfile << setw(30) << "Customer Information Report\n";
	outfile <<	"Reported by Josh Panfil\n\n\n";
	outfile << setw(10) << "First Name" << setw(10) << "Last Name" << setw(9) << "Address"
		<< setw(20) << "City" << setw(8) << "State" << setw(12) << "Zip Code\n";
	outfile << setw(10) << "----------" << setw(10) << "---------" << setw(9) << "-------"
		<< setw(20) << "----" << setw(8) << "-----" << setw(12) << "--------\n";
	
}

int getInfo(CustomerType struct_array[], istream& infile, int& count)
{
	int index = 0;

	infile.get(struct_array[index].firstName, FIRST_NAME_LEN);
	infile.get(struct_array[index].lastName, LAST_NAME_LEN);
	infile.get(struct_array[index].streetAddress, ADDRESS_LEN);
	infile.get(struct_array[index].city, CITY_NAME_LEN);
	infile.get(struct_array[index].state, STATE_LEN);
	infile >> struct_array[index].zipCode; if (!infile) cerr << "Fail";
	
	while( index < MAX_CLIENT_SIZE && infile.good() )
	{
		index++;
		infile >> ws;

		infile.get(struct_array[index].firstName, FIRST_NAME_LEN);
		infile.get(struct_array[index].lastName, LAST_NAME_LEN);
		infile.get(struct_array[index].streetAddress, ADDRESS_LEN);
		infile.get(struct_array[index].city, CITY_NAME_LEN);
		infile.get(struct_array[index].state, STATE_LEN);
		infile >> struct_array[index].zipCode;
	}

	count = index;

	//Test the file status after reading in the data
	 if( index == MAX_CLIENT_SIZE && infile >> ws && infile.good() )
		 return 1; //indicator value for too many data
	 else if( !infile.eof() && infile.fail() )
	 	 return -1; //indicator value for bad data
	 else
		 return 0; //everything was successful
}

void printCustomers(ostream& outfile, CustomerType output[], int& count)
{
	for (int i = 0; i < count; i++){
		outfile << output[i].firstName << output[i].lastName << output[i].streetAddress
			<< output[i].city << output[i].state << output[i].zipCode << endl;
	}
}


this is the file. note: this doesnt show the correct number of spaces. there should be enough spaces in between so that the characters for each input equals the max for each

Michael Miskovsky 153 Summer Avenue Evanston IL 60201
Ingrid Gorman 2075 Woodland Road Aurora IL 60507
Tom Davis 5003 Wilson Boulevard Chicago IL 60603
Richard Nelson 34 Settlers Lane Tinley Park IL 60477
Li Zhu 6509 Great Road Gary IN 46401
Alex Neeley 308 Lakeside Drive Chicago IL 60601
Luiz Neto 8552 Main Street Chicago IL 60602
Sarah Vanesse 35 Park Circle Crown Point IN 46307
Janet Froberg 433 Monroe Street Aurora IL 60504
Brian Campbell 14 Spencer Terrace Gary IN 46402
Linda Gupta 130 Elm Street Evanston IL 60202
Maddie Roux 443 Martin Avenue Chicago IL 60603
Alberto Azevedo 991 Crestview Drive Chicago IL 60601
Amy Shevlin 118 Forest Road Hammond IN 46320
Ansis Kalnajs 3 March Road Crown Point IN 46307
Robert Crawford 602 Pine Ridge Road Tinley Park IL 60477
Kelly Skolnik 15 Tobin Drive Wilmington IL 60481
Chris Locke 4478 Central Street Chicago IL 60611
Paul Marchant 7533 Macleod Drive Chicago IL 60603
Susan Goetz 29 Evergreen Road Aurora IL 60507
I think the problem is the reading in of the address. Its more than one word. Also its a variable number of words.

What you could do is read each word of each line into a vector<std::string> and then you could easily identify the first two and last 3 pieces of information from around the variable length address field.

Also I would use std::string rather than char arrays like this:

1
2
3
4
5
6
7
8
9
10
11
#include <string>

struct CustomerType
{
	std::string lastName;
	std::string firstName;
	std::string streetAddress;
	std::string city;
	std::string state;
	int zipCode;
};
Last edited on
Topic archived. No new replies allowed.