Click to See Complete Forum and Search --> : "return" error on function


jordantk
June 7th, 2008, 12:09 PM
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

laserlight
June 7th, 2008, 12:15 PM
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 bbcode tags.

VladimirF
June 7th, 2008, 12:18 PM
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.

jordantk
June 7th, 2008, 12:21 PM
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!

laserlight
June 7th, 2008, 12:30 PM
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.

VladimirF
June 7th, 2008, 12:36 PM
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.
I got to remember this is C++ and not Java.That would help :)