For my class assignment, I need to be able to expand postal state abbreviations into the full state name. The program reads an input file containing some abbreviations, and writes an output file with the abbreviations expanded. An
abbreviation must be 2 capital letters, i.e. MA. We must use c-strings to accomplish this.
Example input file:
Ellen moved from 45 Broadway, New York,
NY to 10 Lake Shore Drive, Chicago, IL.
Generated output file:
Ellen moved from 45 Broadway, New York,
New York to 10 Lake Shore Drive, Chicago, Illinois.
I'm stuck and I don't know what to do. I'm not sure how to check if something is two letters long, and from there check if those two are something in specific. As you can see, I tried to use a switch statement, but that doesn't work. Also, I tried to use if-else statements like
1 2
|
if(word=='AL')
return 1;
|
and still no luck. I don't know what to do, and any help would be greatly appreciated!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
bool check(char word[]);
int state(char word[]);
void replace(char word[]);
int main()
{
ifstream in;
char filename[200];
cout<< "Enter name of input file: ";
cin.getline(filename,200);
in.open(filename);
ofstream out;
char outname[200];
cout<< "Enter name of output file: ";
cin.getline(outname, 200);
out.open(outname);
if(!in.is_open())
{
cout<<"ERROR failed to open input file " << filename << " BYE\n";
exit(-1);
}
if(!out.is_open())
{
cout<<"ERROR failed to open output file " << filename << " BYE\n";
exit(-1);
}
char word[200];
bool result;
while(in>>word)
{
result=check(word);
}
}
bool check(char word[])
{
int yes(0);
if(strlen(word)==2)
{
yes=state(word);
if(yes==1)
return true;
else
return false;
}
else
{
return false;
}
}
int state(char word[])
{
/*switch(word)
{
case 'AL':case 'AK':case 'AZ':case 'AR':case 'CA':
case 'CO':case 'CT':case 'DE':case 'FL':case 'GA':
case 'HI':case 'ID':case 'IL':case 'IN':case 'IA':
case 'KS':case 'KY':case 'LA':case 'ME':case 'MD':
case 'MA':case 'MI':case 'MN':case 'MS':case 'MO':
case 'MT':case 'NE':case 'NV':case 'NH':case 'NJ':
case 'NM':case 'NY':case 'NC':case 'ND':case 'OH':
case 'OK':case 'OR':case 'PA':case 'RI':case 'SC':
case 'SD':case 'TN':case 'TX':case 'UT':case 'VT':
case 'VA':case 'WA':case 'WV':case 'WI':case 'WY':
return 1;
break;
default:
return -1;
}
*/
}
|