Jump to content

Switch statement in string returning method...

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
ozyabm

ozyabm

    Newbie

  • Members
  • Pip
  • 1 posts
Hi,

Here is a method inside a string returning function:

string computeSin(argument 1, argument 2)
{

if ( strcmp(icmop->icmo_addl_infop->retval_str, "USA") == 0 )
	    {         
	      string newIsin = (string)"US" + icmop->icmo_tranche_cusips[trancheNum]; 
	      int d1, d2, sum, multiply, i;
	      
	      for (sum = 0, multiply = 1, i = 10; i > -1; --i) {
		switch (i) {
		case 0:
		case 1:
		  if (isupper(newIsin[i]))
		    d1 = newIsin[i] - 'A' + 10;
		  else
		    return 0;
		  break;
		default:
		  if (isupper(newIsin[i]))
		    d1 = newIsin[i] - 'A' + 10;
		  else if (isdigit(newIsin[i]))
		    d1 = newIsin[i] - '0';
		  else
		    return 0;
		  break;
		}
		
		if (d1 < 10) {
		  d1 *= (multiply ? 2 : 1);
		  multiply = !multiply;
		} else {
		  d2 = d1 / 10;
		  d1 %= 10;
		  d1 *= (multiply ? 2 : 1);
		  d2 *= (multiply ? 1 : 2);
		  sum += (d2 % 10) + (d2 / 10);
		}
		sum += (d1 % 10) + (d1 / 10);
	      }
	      
	      sum %= 10;
	      sum = 10 - sum;
	      sum %= 10;
	      
	      std::stringstream isinSs;
	      isinSs << newIsin << sum;
	      const std::string &checkedIsin = isinSs.str();
	      
	      return checkedIsin;  
	    }
   return argument 2;
}//end of string method

My Question:
If in the switch statement section of this code I reach return 0 (appears twice: once in case 1 and other in default), would I be taken out of this computeSin method or would I keep looping till the for loop finishes?

Normally, a return statement takes you out of a function but in this case if I reach return 0 which is in a switch statement that will keep running till the for loop ends, would I exit the whole method?

Thanks.

Edited by ozyabm, 30 June 2008 - 11:06 AM.


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I believe Return 0 will exit the function completely.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
Yes, it will. If you want to continue looping, use the continue statement.

#4
nutario

nutario

    Newbie

  • Members
  • PipPip
  • 23 posts
If qou don't want to use "return 0;", than you can throw a exception instead.
This exception can be caught at the calling point of the function in your programm.