Stuck on a class assignment

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;
		}
			*/
}
Your code should trigger many warnings from your compiler.
Single quotes can only be used for a single character. Since you need to store more than 1 character you need to use C-strings, ie double quotes.

Since you're using C++, use STL strings. They are much easier to use than C-style strings.

Also, switch doesn't work with something else than primitive type. You need to use ifs:
1
2
3
4
5
6
7
int state( std::string& word )
{
    if( (word != std::string("AL")) && (word != std::string("AK")) && (word != std::string("AZ")) && /* ... */ )
        return 1;
    else
        return -1;
}

Last edited on
@toum

The assignment called for use of C strings - so std::string is no good. You are right about the switch, but I wouldn't have an if with 52 conditions either.


However, I think the OP's approach needs to change completely.

Consider having a struct which stores the state name & abbreviation. Have an array of these.

You then need to split the input into words try strtok() for this (be careful to handle punctuation), and put them in array as well.

Next, if word length is 2, record this position in the array, then call the find function that works on the array of state names & abbreviations. This function could return a char array holding the state name, it will be empty if the state name wasn't found. The find function can use a for loop to compare the 2 letter word with the state abbreviation. This is much better than the if statement with 52 conditions. As there are several words to be replaced you will need an array to store these as well. Actually an array of structs - the word and it's position in the sentence.

Now you just need to write the new sentence to the output file.

HTH
Topic archived. No new replies allowed.