Hi everyone... I'm learning arrays and cant figure out why my cout is showing "scattered" output... I'll upload my infile and source code.

I'll also upload the example of the correct output my teacher wants us to show on execution.

I'm struggling with the "UNKNOWN" State Name listing too. I tried taking that out of my program and just focus on "KNOWN" state names but my output still comes out "scattered".

Attachments
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

ofstream outfile;
ifstream infile;

int main()
{
string first, last, ss, st_code, st_name;

string st_codes [10]={"AL","CA","FL","GA","IA","KS","MO","MI","MS","NM"};
string st_names [10]={"Alabama", "California", "Florida", "Georgia",
					  "Iowa", "Kansas", "Missouri", "Michigan", "Mississippi", "New Mexico"};

infile.open("people3.txt");
outfile.open("report3.txt");

infile>>first>>last>>ss>>st_code;

while (!infile.eof())

{
	for(int x=0; x<10; x++)
	{if(st_code==st_codes[x]) 
	{st_name=st_names[x];}
	
	
	}
 
	cout<<left<<setw(10)<<first<<left<<setw(12)<<last<<left<<setw(15)
		<<ss<<left<<setw(10)<<st_code<<left<<setw(10)<<st_name<<endl;
 
	outfile<<left<<setw(10)<<first<<left<<setw(12)<<last<<left<<setw(15)
	    <<ss<<left<<setw(10)<<st_code<<left<<setw(10)<<st_name<<endl;

infile>>first>>last>>ss>>st_code>>st_name;


}
return 0;
}
PEOPLE REPORT
                                                              FALL 2005
 
FIRST                        LAST                    SS                                  STATE                        STATE
NAME                         NAME                    NUMBER                              CODE                         NAME
 
JOE                          JONES                   123-45-6789                         NM                           NEW MEXICO
BOB                          SMITH                   111-11-1111                         AL                           ALABAMA
JOHN                         DOE                     999-12-1234                         AA                           UNKNOWN
JEB                          BUSH                    111-11-1112                         FL                           FLORIDA
JIMMY                        CARTER                  222-22-2222                         GA                           GEORGIA
JANE                         DOE                     999-22-1111                         ZZ                           UNKNOWN
ROBERT                       WHEATFIELD              888-88-0000                         KS                           KANSAS
JUNE                         HAYSEED                 222-22-3456                         IA                           IOWA
ARNOLD                       SWARTZ                  678-88-1111                         CA                           CALIFORNIA
HENRY                        FORD                    121-21-2121                         MI                           MICHIGAN
VIDA                         BLUE                    234-00-1111                         CA                           CALIFORNIA
JOHN                         WAYNE                   666-66-0000                         CA                           CALIFORNIA
WILLIAM                      SWAMPRAT                555-55-0987                         MS                           MISSISSIPPI
MATTHEW                      BLUNT                   777-00-1111                         MO                           MISSOURI
DANIEL                       BOONE                   111-99-0000                         MO                           MISSOURI
ZEB                          WASHINGTON              999-99-9999                         X1                           UNKNOWN
JOE JONES 123-45-6789 NM
BOB SMITH 111-11-1111 AL
JOHN DOE 999-12-1234 AA
JEB BUSH 111-11-1112 FL
JIMMY CARTER 222-22-2222 GA
JANE DOE 999-22-1111 ZZ
ROBERT WHEATFIELD 888-88-0000 KS
JUNE HAYSEED 222-22-3456 IA
ARNOLD SWARTZ 678-88-1111 CA
HENRY FORD 121-21-2121 MI
VIDA BLUE 234-00-1111 CA
JOHN WAYNE 666-66-0000 CA
WILLIAM SWAMPRAT 555-55-0987 MS
MATTHEW BLUNT 777-00-1111 MO
DANIEL BOONE 111-99-0000 MO
ZEB WASHINGTON 999-99-9999 X1

Do this:

while ( infile>>first>>last>>ss>>st_code )
   {
      for ( int x=0; x<10; x++ )
      {
         if ( st_code==st_codes[x] )
         {
            st_name=st_names[x];
            break;
         }
      }
      cout<<left<<setw(10)<<first<<left<<setw(12)<<last<<left<<setw(15)
      <<ss<<left<<setw(10)<<st_code<<left<<setw(10)<<st_name<<endl;

      outfile<<left<<setw(10)<<first<<left<<setw(12)<<last<<left<<setw(15)
      <<ss<<left<<setw(10)<<st_code<<left<<setw(10)<<st_name<<endl;
   }

Change the last line of your while loop to this

infile>>first>>last>>ss>>st_code;

Wow... that did the trick. I now see that I was asking the infile to find something that wasnt there. Thanks.

I'll now focus on getting state codes AA, ZZ, X1 to COUT "UNKNOWN". It looks like an easy thing to do but I'll try and walk thru it with my correct output.

Do this:

while ( infile>>first>>last>>ss>>st_code )
   {
      for ( int x=0; x<10; x++ )
      {
         if ( st_code==st_codes[x] )
         {
            st_name=st_names[x];
            break;
         }
      }

Should I use the break command to insert the "UNKNOWN" for a state name? I'll mess around with it. Thanks...

I'd do an unknown like this.

while ( infile >> first >> last >> ss >> st_code )
   {
      int x;
      for ( x = 0; x < 10; ++x )
      {
         if ( st_code==st_codes[x] )
         {
            st_name=st_names[x];
            break;
         }
      }
      if (x >= 10)
      {
         st_name = "Unknown";
      }

      cout << left << setw(10) << first
           << left << setw(12) << last
           << left << setw(15) << ss
           << left << setw(10) << st_code
           << left << setw(10) << st_name << endl;

      outfile << left << setw(10) << first
              << left << setw(12) << last
              << left << setw(15) << ss
              << left << setw(10) << st_code
              << left << setw(10) << st_name << endl;
   }

I figured out my "UNKNOWN" state name problem with the following code. I've posted my correct code for anyone needing an example of a Parallel Array.

Now it's time to tackle my CLASSES assignment...

while (!infile.eof())
{st_name="UNKNOWN";
for(int x=0; x<10; x++)

{if(st_code==st_codes[x])
{st_name=st_names[x];}
}

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

ofstream outfile;
ifstream infile;

int main()
{
string first, last, ss, st_code, st_name;

string st_codes [10]={"AL","CA","FL","GA","IA","KS","MO","MI","MS","NM"};
string st_names [10]={"Alabama", "California", "Florida", "Georgia",
					  "Iowa", "Kansas", "Missouri", "Michigan", "Mississippi", "New Mexico"};

outfile.open("report31.txt");
if(!outfile) {cout<< "TEST"<<endl; return 0;}

cout <<"\t\t\t\t"<<"PEOPLE REPORT\n";
cout <<"\t\t\t\t"<<"  FALL 2005\n\n";
cout <<"FIRST"<<"\t\t"<<"LAST"<<"\t\t"<<"SS"<<"\t\t"<<"STATE"<<"\t\t"<<"STATE\n";
cout <<"NAME"<<"\t\t"<<"NAME"<<"\t\t"<<"NUMBER"<<"\t\t"<<"CODE"<<"\t\t"<<"NAME\n\n";

outfile<<"\t\t\t\t"<<"PEOPLE REPORT\n";
outfile<<"\t\t\t\t"<<"  FALL 2005\n\n";
outfile<<"FIRST"<<"\t\t"<<"LAST"<<"\t\t"<<"SS"<<"\t\t"<<"STATE"<<"\t\t"<<"STATE\n";
outfile<<"NAME"<<"\t\t"<<"NAME"<<"\t\t"<<"NUMBER"<<"\t\t"<<"CODE"<<"\t\t"<<"NAME\n\n";

infile.open("people3.txt");

infile>>first>>last>>ss>>st_code;

while (!infile.eof())

{st_name="UNKNOWN";
	for(int x=0; x<10; x++)
	
	{if(st_code==st_codes[x]) 
	{st_name=st_names[x];}	
	}
 
	cout<<left<<setw(16)<<first<<left<<setw(16)<<last<<left<<setw(17)
		<<ss<<left<<setw(15)<<st_code<<st_name<<endl;
 
	outfile<<left<<setw(16)<<first<<left<<setw(16)<<last<<left<<setw(17)
		<<ss<<left<<setw(15)<<st_code<<st_name<<endl;

infile>>first>>last>>ss>>st_code;


}
return 0;
}

Avoid loop control using eof.

Thanks. I didnt quite understand why that was bad code. I will read your hyperlink in detail.

I like surprising the Prof with stuff like that and backing it up. I also found out with messing with my assignment why \t can get you in trouble... it was messing up my COUT formatting. I had to redo the COUT and OUTPUT statements with setw in order for it format properly.

This article has been dead for over six months. Start a new discussion instead.