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".

Change the last line of your while loop to this

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.