how to eliminate use of goto in switch case

  • A+
Category:Languages

Basically I want to accept a particular character from a user and then using a switch case it passes the string related with that character case to another function .for eg.

case i:strcpy(str,"ice-cream");     other_function(str);     break; 

If any default character got printed by the user then it should print default statement and again take character from user and check its case. I did this with goto, but is there any other option available to avoid or replace goto in this code.

p:       {         cout<<"Choose account type:-/n"<<"Enter/ns :-saving/nc :-current/nf :-fixed/nr :-recurring"<<endl;         char c;         cin>>c;         switch(c)         {             char t[20];         case 's':              strcpy(t,"saving");              a[i].setype(t);              break;         case 'c':              strcpy(t,"current");              a[i].setype(t);              break;          case 'f':              strcpy(t,"fixed");              a[i].setype(t);              break;          case 'r':              strcpy(t,"reccurring");              a[i].setype(t);              break;          default:              cout<<"Enter valid account type"<<endl;              goto p;          }      } 

 


The whole switch should be hived off into a function, and its return value used to determine what happens to the loop next.

while (true) {     std::cout << "Choose account type:/n" << std::flush;     char c;      if (cin >> c)     {        const bool result = HandleUserInput(a[i], c);        if (result)           break;        else           std::cout << "Enter valid account type/n";     }     else     {        // Input error - you'll want to do something about that     } } 

And:

// Given a character representing an account type, sets // the type of 'account' as appropriate. Returns false // on failure. bool HandleUserInput(Account& account, const char c) {     switch (c)     {         case 's':            a[i].setype("saving");            return true;          case 'c':            a[i].setype("current");            return true;          case 'f':            a[i].setype("fixed");            return true;          case 'r':            a[i].setype("recurring");            return true;          default:            return false;     } } 

(Notice how I got rid of the strcpy, which doesn't appear to be necessary? Depends what setype [which is misspelled] does I suppose. Also, for a bonus point, consider using a map instead of the switch, if you don't care about a bit of a performance hit.)

Bathsheba's suggestion is a valid alternative, though I suggest that a return will look much clearer inside a switch than will a continue, since the latter has meaning within other kinds of control flow statements, whereas the former never does.

Also note that, if you decide not to use a function for some good reason, there's actually nothing particularly wrong with your goto, and don't let the cargo cultists tell you otherwise!

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: