CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 26 of 26
  1. #16
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Function problem

    Quote Originally Posted by fuzzylr View Post
    I would like some advice. I would like to reduce the use of goto in this function even though it's fairly simple. What if I wanted to reuse bits and pieces of code? Can I do that or once I get into If / else or While loops do I need to move the code pieces inside the loop till it breaks out or what not. It seems kind of pointless to duplicate the code. I guess that is why the goto is so appealing in this type of instance.

    Code:
    int add()
    {
            char answer;
            int a,b;
            int total;
     
            start:
     
            std::cout<<"Enter first number: ";
            std::cin>>a;
            std::cout<<"Enter Second number: ";
            std::cin>>b;
     
            total = a+b;
            std::cout<<"\nSum: "<< total <<"\n\n";
     
            question:
            std::cout<<"Do you wish to continue: Y/N ";
            std::cin>>answer;
     
            switch (answer)
            {
                    case 'y': goto start;
                    case 'Y': goto start;
                    case 'n': goto end;
                    case 'N': goto end;
                    default:
                    std::cout<<"Invalid characters.  Use Y/N or y/n.\n\n";
                    goto question;
            }
     
            end:
            std::cout<<"Returning to menu";
            sleep(3);
            return 0;
    }
    would it be bad if I did cases like this?

    Code:
     case 'y' || 'Y': code sample here
    My Goal is to consolidate the code down as small as possible while still being functional.
    My god, this is C++ not BASIC. Remove those gotos and replace them with loops and conditionals.
    C++ supports 3 types of loop, they are counted (for loop), top tested (while loop ) and bottom tested (do/while loop).
    Code:
    for( int i = 0; i < 10; ++i )
    {
       // executed 10 times
    }
     
    while( variable != value )
    {
       // executes while variable is not equal to value
    }
     
    do
    {
     // always executes once, and maybe more
    } while ( variable != value );
    All better choices than a goto!
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  2. #17
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Function problem

    Quote Originally Posted by fuzzylr View Post
    I would like some advice. I would like to reduce the use of goto in this function even though it's fairly simple.
    No, you should eliminate all of the goto's, not just reduce them.

    Secondly, a program riddled with goto's are not "simple". It's simple because you put them in, but most programmers who look at your code will not put any effort into understanding it if it has goto's in it.

    Look at this example (not compiled):
    Code:
    #include <iostream>
    
    char getResponse()
    {
       char answer;
       bool inputOK = false;
       do
       {
            std::cout<<"Do you wish to continue: Y/N ";
            std::cin>>answer;
            switch (answer)
            {
               case 'y': 
               case 'Y':
               case 'n':
               case 'N':
                     inputOK = true;
                break;
                default:
                    std::cout<<"Invalid characters.  Use Y/N or y/n.\n\n";
             }
         } while ( !inputOK );
         return answer;
    }
    
    int add()
    {
            char answer;
            int a,b;
            int total;
            
            do
            {  
                std::cout<<"Enter first number: ";
                std::cin>>a;
                std::cout<<"Enter Second number: ";
                std::cin>>b;
    
                total = a+b;
                std::cout<<"\nSum: "<< total <<"\n\n";
                answer = getResponse();
            } while (answer == 'Y' || answer == 'y');
            std::cout<<"Returning to menu";
            return 0;
    }
    Just add a function to get the response. Then the code doesn't turn into spaghetti using goto's.

    http://en.wikipedia.org/wiki/Spaghetti_code

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; May 5th, 2011 at 02:27 PM.

  3. #18
    Join Date
    Feb 2008
    Location
    Virginia
    Posts
    109

    Thumbs up Re: Function problem

    Quote Originally Posted by Paul McKenzie View Post
    No, you should eliminate all of the goto's, not just reduce them.

    Secondly, a program riddled with goto's are not "simple". It's simple because you put them in, but most programmers who look at your code will not put any effort into understanding it if it has goto's in it.

    Look at this example (not compiled):
    Code:
    #include <iostream>
    
    char getResponse()
    {
       char answer;
       bool inputOK = false;
       do
       {
            std::cout<<"Do you wish to continue: Y/N ";
            std::cin>>answer;
            switch (answer)
            {
               case 'y': 
               case 'Y':
               case 'n':
               case 'N':
                     inputOK = true;
                break;
                default:
                    std::cout<<"Invalid characters.  Use Y/N or y/n.\n\n";
             }
         } while ( !inputOK );
         return answer;
    }
    
    int add()
    {
            char answer;
            int a,b;
            int total;
            
            do
            {  
                std::cout<<"Enter first number: ";
                std::cin>>a;
                std::cout<<"Enter Second number: ";
                std::cin>>b;
    
                total = a+b;
                std::cout<<"\nSum: "<< total <<"\n\n";
                answer = getResponse();
            } while (answer == 'Y' || answer == 'y');
            std::cout<<"Returning to menu";
            return 0;
    }
    Just add a function to get the response. Then the code doesn't turn into spaghetti using goto's.

    http://en.wikipedia.org/wiki/Spaghetti_code

    Regards,

    Paul McKenzie
    Thank you. That is definitely some food for thought. Great examples.
    Sean
    Programing 1
    Programing 2 (Spring this year)
    Eager Game Developer.

  4. #19
    Join Date
    Feb 2008
    Location
    Virginia
    Posts
    109

    Unhappy Re: Function problem

    I used your code as a base. Pretty much used it entirely. For practice. I have seem to hit a snag. Not sure why. The last part of the code is being skipped.

    Code:
    int add()
    {
            char answer;
            int a,b;
            int total;
    
            do
            {
                    std::cout<<"Enter first number: ";
                    std::cin>>a;
                    std::cout<<"Enter Second number: ";
                    std::cin>>b;
    
                    // Add to Int's together.
                    total = a+b;
                    std::cout<<"\nSum: "<< total <<"\n\n";
    
                    answer = getResponse();
            } while (answer == 'Y' || answer == 'y');
            std::cout<<"Returning to menu";
            sleep(3);
            return 0;
    }

    This code is only partically being processed...

    Code:
    while (answer == 'Y' || answer == 'y');
            std::cout<<"Returning to menu"; <---- this piece
            sleep(3);
            return 0;
    }
    What is odd is the sleep is being processed but the cout is not.
    Sean
    Programing 1
    Programing 2 (Spring this year)
    Eager Game Developer.

  5. #20
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: Function problem

    std::cout<<"Returning to menu" << std::endl;

  6. #21
    Join Date
    Feb 2008
    Location
    Virginia
    Posts
    109

    Re: Function problem

    Quote Originally Posted by Richard.J View Post
    std::cout<<"Returning to menu" << std::endl;
    Is there any difference between

    Code:
     std::endl
    and

    Code:
    \n
    When should which be used?

    http://www.cplusplus.com/forum/beginner/7350/

    I found the link above but I can't seem to find anything saying that they are really all that different.
    Sean
    Programing 1
    Programing 2 (Spring this year)
    Eager Game Developer.

  7. #22
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: Function problem

    they both add a newline to the buffer, but std::endl flushes the buffer. I.e. when using \n, you'll never know when the buffer is actually sent to the console (assuming std::cout is used) whereas std::endl enforces this.

  8. #23
    Join Date
    Feb 2008
    Location
    Virginia
    Posts
    109

    Re: Function problem

    Quote Originally Posted by Richard.J View Post
    they both add a newline to the buffer, but std::endl flushes the buffer. I.e. when using \n, you'll never know when the buffer is actually sent to the console (assuming std::cout is used) whereas std::endl enforces this.
    Thank you very much.
    Sean
    Programing 1
    Programing 2 (Spring this year)
    Eager Game Developer.

  9. #24
    Join Date
    Apr 2008
    Posts
    725

    Re: Function problem

    Quote Originally Posted by fuzzylr View Post
    This code is only partically being processed...

    Code:
    while (answer == 'Y' || answer == 'y'); <---- semi colon here is bad
            std::cout<<"Returning to menu"; <---- this piece
            sleep(3);
            return 0;
    }
    What is odd is the sleep is being processed but the cout is not.
    .....

  10. #25
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Function problem

    In most cases it doesn't really matter when the output buffer gets flushed, since it will certainly be flushed at the end of the program or upon trying to get any user input. There are a few cases where it does matter (inter-process communication, for instance), so it's good to know what flushing is all about.

  11. #26
    Join Date
    Feb 2008
    Location
    Virginia
    Posts
    109

    Post Re: Function problem

    Quote Originally Posted by Amleto View Post
    .....
    Yes it does belong there. It will throw compiler errors or should I say it did toss compiler errors.

    also note stated reference below that do-while loops are built that way.

    http://www.cplusplus.com/doc/tutorial/control/
    Sean
    Programing 1
    Programing 2 (Spring this year)
    Eager Game Developer.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured