![]() |
| | #1 |
| Registered User Join Date: Jun 2008
Posts: 3
| Here is a method inside a string returning function: Code: 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
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. |
| ozyabm is offline | |
| | #2 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,368
| The use of a switch has no effect on the usual properties of a return statement.
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is offline | |
| | #3 |
| Registered User Join Date: Jan 2005
Posts: 7,137
| Only break applies to the switch, return applies to the function. BTW, I would use string("US") instead of (string)"US" and consider the string's == operator instead of strcmp. |
| Daved is offline | |
| | #4 |
| Registered User Join Date: Jun 2008
Posts: 3
| Thanks for the replies. I will make those changes and compile to see results. I understand that break applies to switch and return applies to the function but if I reach a return within a switch clause, would I exit the function and return a 0 as my result to calling computeIsin()? |
| ozyabm is offline | |
| | #5 |
| Registered User Join Date: Jan 2005
Posts: 7,137
| >> would I exit the function and return a 0 as my result to calling computeIsin()? Yes, just as would happen with a return 0 anywhere else in the function. |
| Daved is offline | |
| | #6 |
| Registered User Join Date: Jun 2008
Posts: 3
| Thanks a lot everyone. That really helped. Last edited by ozyabm; 06-30-2008 at 02:54 PM. |
| ozyabm is offline | |
![]() |
| Tags |
| return, string method, switch |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| OOP Question DB Access Wrapper Classes | digioz | C# Programming | 2 | 09-07-2008 04:30 PM |
| Alegro closes out on me | campsoup1988 | C++ Programming | 8 | 04-03-2006 10:40 AM |
| opengl help | heat511 | Game Programming | 4 | 04-05-2004 01:08 AM |
| Linked List Help | CJ7Mudrover | C Programming | 9 | 03-10-2004 10:33 PM |
| Request for comments | Prelude | A Brief History of Cprogramming.com | 15 | 01-02-2004 10:33 AM |