Need help in finishing this problem. I was doign ok at first but somehow ran into 2 compile errors. Now I'm tired and going in circles. I need a fresh pair of eyes to look it over. This is my first time doing arrays.

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



int main()

string first, last, code, name;
int number;
outfile.open("report.txt");
infile.open("people3.txt");

{string state_codes 
[10]={"AL","CA","FL","GA","IA","KS","MO","MI","MS","NM",};

string state_names 
[10]={"ALABAMA","CALIFORNIA","FLORIDA","GEORGIA","IOWA","KANSAS","MISSOURI","MICHIGAN","MISSISSIPPI","NEW 
MEXICO"};

cout<<"                                  PEOPLE REPORT"<<endl;

cout<<"                                    FALL 2006"<<endl;

cout<<"FIRST "<<"       LAST "<<"         SS "<<"          STATE "<<"         

STATE"<<endl;

cout<<"NAME  "<<"       NAME "<<"       NUMBER"<<"         CODE  "<<"         

NAME"<<endl;

outfile<<"                                PEOPLE REPORT"<<endl;

outfile<<"                                  FALL 2006"<<endl;

outfile<<"FIRST "<<"        LAST "<<"         SS "<<"          STATE 

"<<"         STATE"<<endl;

outfile<<"NAME  "<<"        NAME "<<"       NUMBER"<<"         CODE 

"<<"          NAME"<<endl;
 
for(i = 0; i<16; i++)
 {cout <<left <<setw(2) <<state_codes[i] << " "
       <<setw(15) << state_names[i] << endl;
 }
 return 0;
}

My output should look something like this... any help with finding my 2 compile errors would be appreciated.

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

Here is my infile:

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

Dani AI

Generated

Two quick, concrete fixes will get this compiling, plus a few runtime gotchas to watch.

The two immediate compile stoppers in the posted snippet are: (1) the program calls outfile.open(...) and infile.open(...) but never declares those stream objects nor includes <fstream> (as noted), and (2) the int main() header has no opening brace in the right place — the compiler will complain about a missing { or a function-definition error. After those are fixed, also check for a split string literal (the "NEW MEXICO" in the post is broken across lines) and any undeclared loop variables (the original for(i = 0; ...) needs int i).

Beyond compile errors, fix these logic/correctness issues before testing:

  • Don’t iterate past an array’s size. Your state arrays are size 10 but some loops/prints use 16 — that causes out-of-bounds reads. Use a named constant like const int NUM_STATES = 10; or use a container that knows its size.
  • Add file-open checks (if (!infile) { cerr << "open failed"; }) so you don’t silently work with an empty stream.
  • Handle unknown codes: search the state-code array and fall back to "UNKNOWN" when not found (your sample expected output shows this).
  • Check the input data for typos (the sample infile has KA for Robert but the expected output shows KS — fix the data or treat it as “unknown”).

A compact pattern to read each person and resolve the code (adapt to your variable names):

while (infile >> first >> last >> ssn >> code) {
    int found = -1;
    for (int j = 0; j < NUM_STATES; ++j)
        if (state_codes[j] == code) { found = j; break; }
    string stateName = (found == -1) ? "UNKNOWN" : state_names[found];
    cout << left << setw(8) << first << setw(15) << last
         << setw(16) << ssn << setw(6) << code << setw(15) << stateName << '\n';
    outfile << /* same formatted line */ << '\n';
}

Thanks to for spotting the undeclared streams and to for the formatting tip — using setw/left (or \t where appropriate) will make the output predictable. ’s post already shows the basic syntax fixes; use the checklist above to finish the file-reading and mapping logic.

Recommended Answers

All 8 Replies

could it be that infile and outfile are not defined? It would help if you posted the compile errors.

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

First off, I would suggest adding some tab characters "\t" instead of explicitly adding all the white spaces in. It makes your code really hard to read like it is.

Here Is The Corrected Code

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

int main()
{

string first, last, code, name;

ofstream outfile;

ifstream infile;

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

string state_codes[10]={"AL","CA","FL","GA","IA","KS","MO","MI","MS","NM",};

string state_names[10]={"ALABAMA","CALIFORNIA","FLORIDA","GEORGIA","IOWA","KANSAS","MISSOURI","MICHIGAN","MISSISSIPPI","NEW MEXICO"};

cout<<"                                  PEOPLE REPORT"<<endl;

cout<<"                                    FALL 2006"<<endl;

cout<<"FIRST "<<"       LAST "<<"         SS "<<"          STATE "<<" STATE"<<endl;

cout<<"NAME  "<<"       NAME "<<"       NUMBER"<<"         CODE  "<<"  NAME"<<endl;

outfile<<"                                PEOPLE REPORT"<<endl;

outfile<<"                                  FALL 2006"<<endl;

outfile<<"FIRST "<<"        LAST "<<"         SS "<<"          STATE "<<"         STATE"<<endl;

outfile<<"NAME  "<<"        NAME "<<"       NUMBER"<<"         CODE "<<"          NAME"<<endl;
 

for(int i = 0; i<16; i++)
 {
	cout <<left <<setw(2) <<state_codes[i] << " "<<setw(15) << state_names[i] << endl;
 }
 return 0;
}

could it be that infile and outfile are not defined? It would help if you posted the compile errors.

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

I think that was part of my problem. Thanks... let me get back to it and figure the rest out. I wish I had taken better notes on arrays :o

First off, I would suggest adding some tab characters "\t" instead of explicitly adding all the white spaces in. It makes your code really hard to read like it is.

At first I didnt know what you ment but I looked up \t's in my book. AWESOME... I did ask my teacher for an easier way besides spaces but he said for me to just use spaces. I'll surprise him with this program with tabs.

Here Is The Corrected Code

Thanks.. I was getting a real headache. Let me get back to it... clean it up with tabs and figure out the rest of it.

After this program, I'm gonna go back to easier examples and start again from the beginning over Thanksgiving break.

Spaces over tabs -- always.

Ahh the good old printf("Name: %5s", value)...
I am sure I saw a format function somewhere in the stream classes but i have missplaced it!!!

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.