CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2008
    Posts
    2

    "return" error on function

    Hi everyone,
    I get the following error when I try to build my console program:

    "error C2059: syntax error : 'return'"

    #include <iostream>
    #include <string>

    using namespace std;

    // Function Prototypes
    double getValidValue(string question, double max, double min);

    int main()
    {
    double loan = 0;
    loan = getValidValue("Enter a loan amount", 10000000, 100);
    }

    double getValidValue(string question, double max, double min)
    {
    bool goodAnswer = false;
    double answer = 0.0;

    while(!goodAnswer);
    do
    {
    cout << question << " (" << max << " <-> " << min << ") : ";
    cin >> answer;
    if (answer <= max && answer >= min)
    {
    goodAnswer = true;
    }
    }
    return answer;
    }


    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: "return" error on function

    You appear to have a do while loop without the while keyword. Also, note that the semi-colon after the while is probably unintended.

    Also, properly indent your code and post it in [code][/code] bbcode tags.
    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
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: "return" error on function

    Quote Originally Posted by jordantk
    Code:
    while(!goodAnswer);
    do
    {	
    ...
    }
    return answer;
    Look at the first line. The ";" ends the while loop. The "do" statement must be ended with "while" condition. You have "return" there - so this is your syntax error.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  4. #4
    Join Date
    Jun 2008
    Posts
    2

    Smile Re: "return" error on function

    Thats strange that you can't place the while before the do, I switched them around and no longer have the issue.

    I got to remember this is C++ and not Java.

    Thanks for the help!

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: "return" error on function

    Thats strange that you can't place the while before the do, I switched them around and no longer have the issue.
    You can, but then it would be a separate while loop.

    I got to remember this is C++ and not Java.
    As far as I know, the C++ and Java syntax do not differ in this case.
    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
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: "return" error on function

    Quote Originally Posted by jordantk
    Thats strange that you can't place the while before the do...
    Yes, you can. The "do" in that case is implied. And as noted above, you got an extra ";" in your while statement.
    Quote Originally Posted by jordantk
    I got to remember this is C++ and not Java.
    That would help
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

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