954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

After 2nd element read... cout is "scattered"

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 ASSN_3.cpp (0.97KB) people3.txt (0.45KB) Correct_Output.txt (2.35KB)
ToySoldier
Newbie Poster
20 posts since Oct 2005
Reputation Points: 18
Solved Threads: 0
 

Change the last line of your while loop to this

infile>>first>>last>>ss>>st_code;
WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

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;
   }
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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.

ToySoldier
Newbie Poster
20 posts since Oct 2005
Reputation Points: 18
Solved Threads: 0
 

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

ToySoldier
Newbie Poster
20 posts since Oct 2005
Reputation Points: 18
Solved Threads: 0
 

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;
   }
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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;
}
ToySoldier
Newbie Poster
20 posts since Oct 2005
Reputation Points: 18
Solved Threads: 0
 
while (!infile.eof())

Avoid loop control using eof .

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
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.

ToySoldier
Newbie Poster
20 posts since Oct 2005
Reputation Points: 18
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You