"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
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.
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.
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!
Re: "return" error on function
Quote:
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.
Quote:
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.
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 :)