4 Replies - 1248 Views - Last Post: 25 July 2010 - 01:40 PM Rate Topic: -----

#1 Drj312   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 28-June 10

data input failure

Posted 25 July 2010 - 10:47 AM

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
#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);

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 )
	{
		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;
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: data input failure

#2 sarmanu   User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: data input failure

Posted 25 July 2010 - 11:05 AM

I don't think you get the meaning of ifstream::get function. This extracts 'n' characters from your file. In your case:
infile.get(struct_array[index].firstName, FIRST_NAME_LEN);


struct_array[index].firstName will contain FIRST_NAME_LEN characters. If first name does not contain exactly FIRST_NAME_LEN characters, you will end up in trouble. Why not simply use >> operator to extract data from file? This should work:
int getInfo(CustomerType struct_array[], istream& infile, int& count)
{
	int index = 0;
	
        // Better use .good(). Simply using infile may led you in trouble,
        // as the error flag may not be set when data can't be read.
	while( index < MAX_CLIENT_SIZE && infile.good())
	{
	    infile >> struct_array[index].firstName;
	    infile >> struct_array[index].lastName;
	    infile >> struct_array[index].streetAddress;
	    infile >> struct_array[index].city;
	    infile >> struct_array[index].state;
	    infile >> struct_array[index].zipCode;

            // Arrays should be indexed from 0, that's why I moved this here.
            // Incrementing, then extracting, will give you an 1 index base.
	    index++;
	}

	count = index;

        // Really remove some redundant checks.
	//Test the file status after reading in the data
	 if( index == MAX_CLIENT_SIZE)
		 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
}


But I definitely recommend you a std::vector approach for holding structs, and also std::string instead of char arrays.
Was This Post Helpful? 0
  • +
  • -

#3 Drj312   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 28-June 10

Re: data input failure

Posted 25 July 2010 - 11:13 AM

thank you for your advice. i am doing it the way my teacher told us to do it. i would much rather do it your way, but i have to do it her way. she wanted us to use char arrays.

here is the input file. maybe it will help

Quote

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

all the columns have the exact length that i used for the global variables

it doesnt come out right so i will just attach the text file

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#4 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1862
  • View blog
  • Posts: 6,711
  • Joined: 19-February 09

Re: data input failure

Posted 25 July 2010 - 01:09 PM

If you print the contents of struct_array, before testing for fail, the results are quite interesting.

  ...
  infile >> struct_array[index].zipCode;

  // added code
  cout << struct_array[index].firstName << endl;
  cout << struct_array[index].lastName << endl;
  cout << struct_array[index].streetAddress << endl;
  cout << struct_array[index].city << endl;
  cout << struct_array[index].state << endl;

  if (!infile) cerr << "Fail";
  ...


Was This Post Helpful? 0
  • +
  • -

#5 Drj312   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 28-June 10

Re: data input failure

Posted 25 July 2010 - 01:40 PM

thank you. i was able to fix my problem by adding infile >> ws after each infile.get
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1