The Question is:

Write a program that asks a user to enter one of the following state abbreviations: NC, SC, GA, FL, or AL. The program should then display the name of the state that cooresponds with the abbreviation entered ( North Carolina, South Carolina, Georgia, Florida, or Alabama).

Input Validation: Accept abbreviations with both letters in uppercase or both in lowercase. Display an error message if an abbreviation other than what is listed is entered.

So far this is what I got.

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

int main()
{
    string sa;  //State Abbreviation
    string NC = "North Carolina";
    string SC = "South Caronlina";
    string GA = "Georgia";
    string FL = "Florida";
    string AL = "Alabama";

    cout << "Enter one of the following State Abbreviations for full name:" << endl;
    cout << "NC, SC, GA, FL, or AL. ";
    cin >> sa;

    if (sa == NC)
        NC = "North carolina";

    else if (sa == SC)
        SC = "South Carolina";

    cout << "The State Abbreviation you entered stands for " << sa << endl;

    return 0;
}

I have tried several methods and it either gives me no output after I enter in the NC or SC or I get an error about bool if I use the following: if (sa == NC) || (sa == nc)

I have not finished entering the rest of my problem/answer because its not functioning the way I need it to.

I did get it to display "The State Abbreviation you entered stands for NC, or SC" Depending on which of the 2 I entered. Thanks in advanced!!

Recommended Answers

All 7 Replies

cin >> sa;

if (sa == NC)
NC = "North carolina";

else if (sa == SC)
SC = "South Carolina";

cout << "The State Abbreviation you entered stands for " << sa << endl;

Error lies here.You take sa as the input string and then you even match it improperly and finally you output sa only, that is what ever you have entered will be displayed at the output...

Try this:

cin >> sa;

if (sa == "NC")
sa = NC;

else if (sa == "SC")
sa = SC ;

cout << "The State Abbreviation you entered stands for " << sa << endl;

Perfect! Thanks for the help. Now I have tried to use the bool operand to give an error message if they have not entered in one of the listed abbreviations and its not working.

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

int main()
{

    string sa;
    string NC = "North Carolina";
    string SC = "South Caronlina";
    string GA = "Georgia";
    string FL = "Florida";
    string AL = "Alabama";
    bool err = false; //User enters NA State Abbreviation

    cout << "Enter one of the following State Abbreviations for full name:" << endl;
    cout << "NC, SC, GA, FL, or AL. ";
    cin >> sa;

    if (sa == "NC")
        sa = NC;

    else if (sa == "SC")
        sa = SC;
    else if (sa == "GA")
        sa = GA;

    else if (sa == "FL")
        sa = FL;

    else if (sa == "AL")
        sa = AL;

    else
        err = false; //Wrong entry

    if (err)
    cout << "You have entered an Invalid State Abbreviation" << endl;

    else
    cout << "The State Abbreviation you entered stands for " << sa << endl;

    return 0;
}

I have tried making bool err = true and the command at the end false, that did give error message but it was only when I entered a valid abbreviation, also How to I add it to accept lowercase? I have tried if (sa == "NC") || (sa == "nc") but I get an error on that as well. I know I can manually type in nc = North Carolina but isnt there an easier way of doing this?

you need to use code tags.

I was able to run your code successfully by using:

bool err = false; //User enters NA State Abbreviation

cout << "Enter one of the following State Abbreviations for full name:" << endl;
cout << "NC, SC, GA, FL, or AL. ";
cin >> sa;

if (sa == "NC")
sa = NC;

else if (sa == "SC")
sa = SC;
else if (sa == "GA")
sa = GA;

else if (sa == "FL")
sa = FL;

else if (sa == "AL")
sa = AL;

else
err = true; //Wrong entry

if (err)
cout << "You have entered an Invalid State Abbreviation" << endl;

else
cout << "The State Abbreviation you entered stands for " << sa << endl;

Start the error as false until you recieve an invalid input, then make it true and output the message.

As for your second question involving lowercase, I did the following and the code ran fine:

if (sa == "NC" || "nc")
sa = NC;

else if (sa == "SC" || sa == "sc")
sa = SC;
else if (sa == "GA" || sa == "ga")
sa = GA;

else if (sa == "FL" || sa == "fl")
sa = FL;

else if (sa == "AL" || sa == "al")
sa = AL;

You were on the right track. Extra parentheses are not needed.

As for your second question involving lowercase, I did the following and the code ran fine:

if (sa == "NC" || "nc")
sa = NC;

else if (sa == "SC" || sa == "sc")
sa = SC;
else if (sa == "GA" || sa == "ga")
sa = GA;

else if (sa == "FL" || sa == "fl")
sa = FL;

else if (sa == "AL" || sa == "al")
sa = AL;

You were on the right track. Extra parentheses are not needed.

Awesome, thanks for the input everyone! I was wondering why I couldnt get it to work w/ lowercase, i was suppose to enter in || with in the ().

Glad I could be of some help. Please mark the thread solved if you have no further questions.
Thanks.

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.