A while loop that doesn't work

A program that sorts data according specifics using a while loop

Page 1 of 1

4 Replies - 1297 Views - Last Post: 12 April 2009 - 02:36 AM Rate Topic: -----

#1 Powerade33  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-April 09

A while loop that doesn't work

Posted 11 April 2009 - 08:36 PM

Here is what I have so far. Only the stuff below the stars is code. I'm having difficulties getting this while loop to work. It compiles, but no data shows up in my output files (only the output file headers). I have been working on this for 3 days and have had no success. PLEASE HELP! I have attached the program5.txt file for you to use.


Description: Reads a record from the file, then decides if the person
lives in Missouri (MO) or Kansas (KS). If they live in
Kansas, then it prints the record to a file named kansas.txt.
Otherwise, the program prints it to the file named missouri.txt.
Inputs: fin
Outputs: moOut, ksOut
Algorithm: open & check files
print headers
while we can read data
if state is MO
print to moFile
else
print to ksFile
end while loop
close files

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

using namespace std;

struct addressType//declare struct
{
	string fname, lname, city, state, street;
	int zip, house;
};

//protype for file headers
void printHeader (ofstream &out, string state);
//protype for reading the input file
bool readData(ifstream &in, addressType persons);
//protype for printing data to output files
void printData(ofstream &out, addressType persons);

int main()
{
	ifstream fin("program5.txt");//in file stream for fin
	ofstream moOut("kansas.txt");//declaring output file for missouri residents
	ofstream ksOut("missouri.txt");//declaring output file for kansas residents

	if(fin.fail())//if to check that the input file opens
	{
		cerr << "Unable to open input file\n";
		exit(3);
	}
 
	addressType person;//delcaring variable for struct

	printHeader(moOut, "Missouri");//printing file headers
	printHeader(ksOut, "Kansas");

	int i=0;
	while(readData(fin, person))
	{
		if (person.state == "MO")
		{
			printData(moOut, person);
		}
		else
		{
			printData(ksOut, person);
		}
		i++;
	}
	
	fin.close();//close files
	moOut.close();
	ksOut.close();
	
	return 0;
}

void printHeader (ofstream &out, string state)//function for output file header
{
	out << "First Name" << setw (15) << "Last Name" << setw (10) << "House #" 
		<< setw (13) << "Street" << setw (13) << "City" << setw (10)
		<< "State" << setw (10) << "Zip Code" << endl;
}
//function to read data from output file
bool readData(ifstream &in, addressType persons)
{
	in >> persons.fname >> persons.lname >> persons.house >> persons.street 
		>> persons.state >> persons.zip;

	return in.good();//return true statement
}
//function to print data to output file
void printData(ofstream &out, addressType persons)
{
	out << persons.fname << setw (15) << persons.lname << setw (10) << persons.house
		<< setw (13) << persons.street << setw (13) << persons.city << setw (10)
		<< persons.state << setw (10) << persons.zip << endl;
}



** Edit ** :code:

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: A while loop that doesn't work

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: A while loop that doesn't work

Posted 11 April 2009 - 11:41 PM

Ok. :)

You are missing a field in the input record when you copy the file data to the persons struct. readData() returns false because the ifstream in; gets out out of sync with the file contents. (I reformatted the routine to make easier to read here)

//function to read data from output file
bool readData(ifstream &in, addressType persons)
{						   // File contents:
	in  >> persons.fname	// First
		>> persons.lname	// Last
		>> persons.house	// 01234
		>> persons.street   // Street
		>> persons.state	// City <<<< missing
		>> persons.zip;	 // State  << state is string, zip is integer, OPPS!
							// ZIP5  << not read
	return in.good(); //return true statement
}


should be
bool readData(ifstream &in, addressType persons)
{
	in >> persons.fname >> persons.lname >> persons.house
	   >> persons.street >> persons.city >> persons.state
	   >> persons.zip;

	return in.good();//return true statement
}



Once the file can be read, you need to change these: :) :)
//	   |||	  ||||									||||
ofstream moOut("kansas.txt");//declaring output file for missouri residents
ofstream ksOut("missouri.txt");//declaring output file for kansas residents


Finally, you are passing the persons struct around by value. You need to change this to by reference:
...
//protype for reading the input file
bool readData(ifstream &, addressType &);
...
//function to read data from output file
bool readData(ifstream &in, addressType &persons)
{
...


Was This Post Helpful? 0
  • +
  • -

#3 Powerade33  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-April 09

Re: A while loop that doesn't work

Posted 12 April 2009 - 02:02 AM

Thank you so much n8wxs. Simple mistakes huh? I'm having a hard time understanding when to put the & for a pass by reference and when not to. I guess I just don't understand the phrase "pass by reference" but I'll get it sooner or later. Thanks again!
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: A while loop that doesn't work

Posted 12 April 2009 - 02:25 AM

A read of this page might help with this stuff
http://www.cplusplus...ial/functions2/

You have the right attitude to this stuff.
It will all become clearer in time and one day it will seem strange it was ever confusing but it all takes time.
Rome wasn't burnt in a day.
Was This Post Helpful? 0
  • +
  • -

#5 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 277
  • View blog
  • Posts: 1,783
  • Joined: 20-September 08

Re: A while loop that doesn't work

Posted 12 April 2009 - 02:36 AM

You might like to see the approach below also ... (and you may like to see the syntax for the various sorts) ...

Shalom,
David
http://developers-he.../index.p...opic,127.0.html


#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <sstream> // for istringstream
#include <list> // using a list container ...
#include <algorithm> // for sort ...

using namespace std;

struct MailData
{
    string fname, lname, city, state, street;
    int zip, house;
};

// to sort by zip code ...
bool compareZip(const MailData& md1, const MailData& md2)
{
    return md1.zip < md2.zip;
}

// to sort by last name, then first name ...
bool compareName(const MailData& md1, const MailData& md2)
{
    if( md1.lname == md2.lname )
        return md1.fname < md2.fname;
    return md1.lname < md2.lname;
}

// to sort by city, then by street, then by (house) number ...
bool compareCityStreetHouse(const MailData& md1, const MailData& md2)
{
    if( md1.city == md2.city && md1.street == md2.street)
        return md1.house < md2.house;
    if( md1.city == md2.city )
        return md1.street < md2.street;
    return md1.city < md2.city;
}


void printHeader(ofstream& out);
void printData(ofstream& out, const MailData& persons);


int main()
{
    ifstream fin  ("program5.txt");
    ofstream moOut("kansas.txt");
    ofstream ksOut("missouri.txt");
    printHeader(moOut);
    printHeader(ksOut);
    
    string tmp;
    list < MailData > md;
    while( getline(fin, tmp) )
    {
        MailData person;
        istringstream iStr(tmp);
        iStr >> person.fname >> person.lname
             >> person.house >> person.street
             >> person.city >> person.state >> person.zip;

        md.push_back( person );
    }

    //md.sort( compareZip );
    //md.sort( compareName );
    md.sort( compareCityStreetHouse );
    
    list< MailData >::iterator it;
    for( it = md.begin(); it != md.end(); ++it )
    {
        if ( it->state == "MO")
            printData(moOut, *it);
        else
            printData(ksOut, *it);
    }

    fin.close();
    moOut.close();
    ksOut.close();
    
    // if using a Windows OS ... take a look at the new files ...
    system("notepad kansas.txt");
    system("notepad missouri.txt");
}

void printHeader(ofstream &out)
{
    out << left << setw(15) << "First-Name" << setw(15) << "Last-Name"
        << right << setw(8) << "House #" << " " << left << setw(13) << "Street"
        << setw(13) << "City" << right << setw(6) << "State" << "   "
        << setw(8) << "Zip-Code" << endl
        << setfill('=') << setw(82) << '=' << setfill(' ') << endl;
}

void printData(ofstream &out, const MailData& persons)
{
    out << left << setw(15) << persons.fname << setw(15) << persons.lname
        << right << setw(8) << persons.house << " " << left << setw(13) << persons.street
        << setw(13) << persons.city << right << setw(6) << persons.state << "   "
        << setw(8) << persons.zip << endl;
}

This post has been edited by David W: 13 April 2009 - 01:19 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1