View Single Post
  #10 (permalink)  
Old 07-27-2007, 02:27 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
If you use functions for each case, the only advantage would be in a large switch statement. As long as code is indented enough, there shouldn't be much problem. Of course, if the issue is redundant code, that usually means that you can structure your code better. For example:
Code:
printf("The area of the ");
if (shape == 'r')
    printf("rectangle");
else if (shape == 'c')
    printf("circle");
// etc.
printf(" is %d\n", area);
While it is effective, it isn't exactly ideal. Why not have a variable that holds the name of the shape? Inside the switch statement when figuring out the area of the shape, you could set a string to the value of "rectangle" or "square" or whatever, though the easy way of doing this requires <string.h> (<cstring> in C++, though std::string is better in C++) to be #included:
Code:
char shape_name[10]; //"rectangle" is 9 characters, plus the null terminator makes 10
switch (shape) {
    case 'r':
        strcpy(shape_name, "rectangle");
        // rest of code for rectangle here
    break;

    case 'c':
        strcpy(shape_name, "circle");
        // rest of code for circle here
    break;

    //other cases and default
}
total = num1 * num2;
printf("The area of the %s is %lf\n", shape_name, total);
Just an example of how it might be used. It simplifies the output and doesn't require very much extra code either. That way, there is no need to create your own function and no need to complicate the output.

__________________
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."
-- Bjarne Stroustrup, creator of what is now known as C++
For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that.
Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to rpgfan3233 For This Useful Post:
HelloWorld (07-27-2007)