you could use a map c++ container.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
google for "c++ map". Here is one example
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Okay, I am going to ask you why rely on characters if you have strings at your disposal. I havent as such seen the whole code but I think what you are trying to do is to accept the input from the user in string form as abbr and display the corresponding state.
The thing I dont understand is why work at the character level. Why not make use of the string class which greatly simplifies a lot of things.
Also as Mr. Dragon suggested, try using maps which will greatly simplify your task
int main()
{
int a = 0; // initialize variables always
// dont use two characters, use string as you have already used instead
char firstLetter = '\0' ; // setting char to null char
char secondLetter;
string stateName;
cout << " STATE POSTAL CODE CONVERTER\n\n";
cout << "Enter the two character Postal Code to see\n"
<< "it's corresponding State. (for example AK)\n\n";
cin >> firstLetter >> secondLetter;
// replace this cryptic while loop and switch
// use string compare operator == instead
while ( (firstLetter != 'E') && (secondLetter != 'X') )
{
<--snipped-->
cout << "Enter another State Postal Code or \"EX\" to exit\n\n";
// you have to press enter two times and the program is case
// sensitive and works for only uppercase characters.
cin >> firstLetter >> secondLetter;
}
// dont use this loop, use <em>cin.get( )</em> instead
for(a = 1; a < 3000000000; a++); // acts as a pause before the output screen goes away
return 0;
}
// dont preform cryptic calculations at character level
// use std::string instead.
int StateNumber(char chr1, char chr2)
{
int stateValue = 0;
int A = 0;
int B = 0;
A = (int)chr1;
B = (int)chr2;
stateValue = (A * 100 + B);
return stateValue;
}
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
BTW, just wanted to tell you that I ran your code and it worked fine for me. Any specific you are encountering ?
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
If he/she can't use std::strings it is unlikely they will be allowed to use std::map.
Personally, if it works I don't see how that will affect the grading of your assignment, but I wouldn't know fo'sho.
for(a = 1; a < 3000000000; a++); // acts as a pause before the output screen goes away
That's funny.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
if you can't use a std::map or std::string, then you can use two string arrays and replace that switch statement with a simple loop, something like this:
char *state_abbr[]= {"AL","OK",... , NULL}
char *state_names = {"ALASKA","OKLAHOMA", ..., NULL}
...
...
char *abbr = "OK";
for(int i = 0; state_abbr[i] != NULL; i++)
{
if( strcmp(state_abbr[i],abbr) == 0)
{
// found it!
cout << state_names[i] << endl;
}
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343