/// 
// Program; Project2.cpp
// Date; 
// Purpose: 
// Description: 
// Input: 
// Output: 
// Use: This program is compiled and executed using 
//   Microsoft Visual Studio C++.net 2003 Version 7.1


#include "stdafx.h"
#include <iostream>  // access input/output stream.
#include <string>
#include <cctype>

using namespace std;

int StateNumber(char, char);
void StateText(string);

int main()
{
       int a;                // to hold the output screen
    char firstLetter;   // can you set char's = 0?
    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;
    
    while ( (firstLetter != 'E') && (secondLetter != 'X') )

    {
        switch (StateNumber(firstLetter, secondLetter))
        {
            case 6575:
                StateText("Alaska");
                break;
            case 6576:
                StateText("Alabama");
                break;
            case 6582:
                StateText("Arkansas");
                break;
            case 6590:
                StateText("Arizona");
                break;
            case 6765:
                StateText("California");
                break;
            case 6779:
                StateText("Colorado");
                break;
            case 6784:
                StateText("Connecticut");
                break;
            case 6869:
                StateText("Delaware");
                break;
            case 7076:
                StateText("Florida");
                break;
            case 7165:
                StateText("Georgia");
                break;
            case 7273:
                StateText("Hawaii");
                break;
            case 7365:
                StateText("Iowa");
                break;
            case 7368:
                StateText("Idaho");
                break;
            case 7376:
                StateText("Illinois");
                break;
            case 7378:
                StateText("Indiana");
                break;
            case 7583:
                StateText("Kansas");
                break;
            case 7589:
                StateText("Kentucky");
                break;
            case 7665:
                StateText("Louisiana");
                break;
            case 7765:
                StateText("Massachusetts");
                break;
            case 7768:
                StateText("Maryland");
                break;
            case 7769:
                StateText("Maine");
                break;
            case 7773:
                StateText("Michigan");
                break;
            case 7778:
                StateText("Minnesota");
                break;
            case 7779:
                StateText("Missouri");
                break;
            case 7783:
                StateText("Mississippi");
                break;
            case 7784:
                StateText("Montana");
                break;
            case 7867:
                StateText("North Carolina");
                break;
            case 7868:
                StateText("North Dakota");
                break;
            case 7869:
                StateText("Nebraska");
                break;
            case 7872:
                StateText("New Hampshire");
                break;
            case 7874:
                StateText("New Jersey");
                break;
            case 7877:
                StateText("New Mexico");
                break;
            case 7886:
                StateText("Nevada");
                break;
            case 7889:
                StateText("New York");
                break;
            case 7972:
                StateText("Ohio");
                break;
            case 7975:
                StateText("Oklahoma");
                break;
            case 7982:
                StateText("Oregon");
                break;
            case 8065:
                StateText("Pennsylvania");
                break;
            case 8273:
                StateText("Rhode Island");
                break;
            case 8367:
                StateText("South Carolina");
                break;
            case 8368:
                StateText("South Dakota");
                break;
            case 8478:
                StateText("Tennessee");
                break;
            case 8488:
                StateText("Texas");
                break;
            case 8584:
                StateText("Utah");
                break;
            case 8665:
                StateText("Virginia");
                break;
            case 8684:
                StateText("Vermont");
                break;
            case 8765:
                StateText("Washington");
                break;
            case 8773:
                StateText("Wisconsin");
                break;
            case 8786:
                StateText("West Virginia");
                break;
            case 8789:
                StateText("Wyoming");
                break;
            default:
                cout << '\n' << firstLetter << secondLetter << " is not a valid Postal Code!\n\n";
        }


        cout << "Enter another State Postal Code or \"EX\" to exit\n\n";
        cin >> firstLetter >> secondLetter;
    }

    cout << "\nGOOD BYE!";
    for(a = 1; a < 3000000000; a++);  // acts as a pause before the output screen goes away
    return 0;
}

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;
}

void StateText(string stateName)
{
    cout << "The State you selected is " << stateName << "!" << endl;
        

}

Recommended Answers

All 9 Replies

you could use a map c++ container.

how would that work

google for "c++ map". Here is one example

thanks , but I already did that


Thanks again I'll give it a try

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 [I]cin.get( )[/I] 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;
}

school homework and we can't use strings it has to be two char

but thanks

BTW, just wanted to tell you that I ran your code and it worked fine for me. Any specific you are encountering ?

Member Avatar for iamthwee

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.

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;

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