Well, we don't blatantly do homework questions here. Show some effort and help us to help you. How far into this program have you gotten so far? Have you prompted the user to enter a state abbreviation? Have you done an error check to make sure the abbreviation entered was valid? What technique are you using to correspond a state abbreviation with a full state? Are you printing the full state name out to the screen? ;) *hint hint hint*
cscgal
The Queen of DaniWeb
19,421 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 229
>now check if the input is valid or not by an if statement giving right parameters
This is okay if the OP is forced to use such a braindead approach. Otherwise a series of 50 if statements is silly, and a table driven approach is far better.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>actually i also will use if else.
Your loss. Have fun typing all of that.
>how to use the table driven you mention above?
#include <cctype>
#include <iostream>
#include <string>
struct {
std::string abbr;
std::string name;
} state[] = {
"AL", "Alabama",
"FL", "Florida",
"GA", "Georgia",
};
const int nstates = 3;
std::string get_state ( std::string abbr )
{
// Make abbr upper case
for ( std::string::size_type i = 0; i < abbr.size(); i++ )
abbr[i] = toupper ( (unsigned char)abbr[i] );
for ( std::string::size_type i = 0; i < nstates; i++ ) {
if ( abbr == state[i].abbr )
return state[i].name;
}
return "NOT FOUND";
}
int main()
{
std::cout<< get_state ( "GA" ) <<std::endl;
std::cout<< get_state ( "ME" ) <<std::endl;
std::cout<< get_state ( "fl" ) <<std::endl;
}
The best part is that the table can be filled from a file, thus keeping the code very short and easy to follow as opposed to your 50 if statements. And if the USA decides to grow, which is probable, you would have to figure out where to change your code, modify it, and then test it to make sure you didn't make a mistake I would only have to add a single quick entry to my input file.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
> I actually assume that the person asking a question is a beginner
All the more reason to mention better alternatives. Even if they don't understand, they still learn that other options exist.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401