State abbreviations

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2004
Posts: 1
Reputation: jags is an unknown quantity at this point 
Solved Threads: 0
jags jags is offline Offline
Newbie Poster

Re: c++ help

 
0
  #1
Oct 4th, 2004
C++ Help Please!!!!!!!!!!!

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.

THANK YOU VERY MUCH!!!!!!!!!!!
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,047
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 129
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: State abbreviations

 
0
  #2
Oct 4th, 2004
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*
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 27
Reputation: fahad is an unknown quantity at this point 
Solved Threads: 0
fahad fahad is offline Offline
Light Poster

Re: State abbreviations

 
0
  #3
Oct 5th, 2004
Hi,
We can help you by giving a pseudocode for ur programme
input : TWO Characters
OutPut: State Name
The aslgo goes like this
Ask the user to enter two chracters
store the characters in lets say variable ch,
now check if the input is valid or not by an if statement giving right parameters

if the input do not have right parameters i-e it is input is not valid , ask the user to give input again or simply quit the programme showing an error message

if the input is valid use if-else statements to show out put
e.g
*******************************************
if(ch=='NC' || ch == 'nc' )
cout<<"North Carolina\n";
else if(ch=='SC' || ch =='sc' )
cout<<"South Carolina\n";
else.......................

*******************************************

I hope this would help u , if still there are some ambiguities , feel free to ask again,,,but do try it by ur own b4 asking next question.....

Fahad
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,771
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 743
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Code Goddess

Re: State abbreviations

 
0
  #4
Oct 5th, 2004
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 17
Reputation: lara_ is an unknown quantity at this point 
Solved Threads: 0
lara_'s Avatar
lara_ lara_ is offline Offline
Newbie Poster

Re: State abbreviations

 
0
  #5
Oct 5th, 2004
Originally Posted by Narue
>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.
actually i also will use if else.
how to use the table driven you mention above? can u teach me?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,771
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 743
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Code Goddess

Re: State abbreviations

 
0
  #6
Oct 5th, 2004
>actually i also will use if else.
Your loss. Have fun typing all of that.

>how to use the table driven you mention above?
  1. #include <cctype>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. struct {
  6. std::string abbr;
  7. std::string name;
  8. } state[] = {
  9. "AL", "Alabama",
  10. "FL", "Florida",
  11. "GA", "Georgia",
  12. };
  13.  
  14. const int nstates = 3;
  15.  
  16. std::string get_state ( std::string abbr )
  17. {
  18. // Make abbr upper case
  19. for ( std::string::size_type i = 0; i < abbr.size(); i++ )
  20. abbr[i] = toupper ( (unsigned char)abbr[i] );
  21.  
  22. for ( std::string::size_type i = 0; i < nstates; i++ ) {
  23. if ( abbr == state[i].abbr )
  24. return state[i].name;
  25. }
  26.  
  27. return "NOT FOUND";
  28. }
  29.  
  30. int main()
  31. {
  32. std::cout<< get_state ( "GA" ) <<std::endl;
  33. std::cout<< get_state ( "ME" ) <<std::endl;
  34. std::cout<< get_state ( "fl" ) <<std::endl;
  35. }
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 27
Reputation: fahad is an unknown quantity at this point 
Solved Threads: 0
fahad fahad is offline Offline
Light Poster

Re: State abbreviations

 
0
  #7
Oct 5th, 2004
Hi;
I actually assume that the person asking a question is a beginner ,so it will be difficult for him to keep hold of arrays or pointers ,, thats why I have suggested that if-else method otherwise no doubt this is a silly approach for bigger problems.

Fahad
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,771
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 743
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Code Goddess

Re: State abbreviations

 
0
  #8
Oct 5th, 2004
> 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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC