return in switch statement within a string returning method...

This is a discussion on return in switch statement within a string returning method... within the C++ Programming forums, part of the General Programming Boards category; Hi, Here is a method inside a string returning function: Code: string computeSin(argument 1, argument 2) { if ( strcmp(icmop->icmo_addl_infop->retval_str, ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    3

    Lightbulb return in switch statement within a string returning method...

    Hi,

    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
    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.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,773
    The use of a switch has no effect on the usual properties of a return statement.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,348
    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.

  4. #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()?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,348
    >> 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.

  6. #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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Alegro closes out on me
    By campsoup1988 in forum C++ Programming
    Replies: 8
    Last Post: 04-03-2006, 10:40 AM
  3. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21