### "Expected expression" before return

So after putting the nHigh/nLow example (5.9) in Xcode (like I do with all examples), it gave me a warning - expected expression - whilst pointing to return.

Here's the code:

 ``1234567891011121314`` ``````#include #include // Rand and srand using namespace std; int main() { // Random number between nHigh and nLow unsigned int GetRandomNumber(int nLow, int nHigh) { return (rand() % (nHigh - nLow + 1)) + nLow; // Here's "return" }; cout << GetRandomNumber(1, 6); }``````

What do I have to put in front of return?
Last edited on
Define the function outside of main()

 ``1234567891011121314151617`` `````` #include #include // Rand and srand using namespace std; // Random number between nHigh and nLow unsigned int GetRandomNumber(int nLow, int nHigh) { return (rand() % (nHigh - nLow + 1)) + nLow; // Here's "return" }; int main() { ... }``````
Move the whole function outside of `main`. Either above it or below it with a prototype above it.
Last edited on
Ah, works fine now! This is my current code:
 ``1234567891011121314`` ``````#include #include // Rand and srand using namespace std; // Random number between nHigh and nLow unsigned int GetRandomNumber(int nLow, int nHigh) { return (rand() % (nHigh - nLow + 1)) + nLow; }; int main() { cout << GetRandomNumber(1, 6); }``````

However, I still have a problem: every time I compile it, it gives the same number. 1 and 6 gives 2 every time, 1 and 20 gives 8 every time, etcetera. Is this intentional?
Last edited on
You need to seed the PRNG first.

 ``1234`` ``````#include // Then, in main... srand( time( NULL ) );``````
@iHutch, Why ctime, and not time.h?

@Prodevus, like iHutch said. But you only ever need to seed it once. i.e. Don't use `srand( time( NULL ) );` in a loop. Just add it to the start of main() etc.
They're the same thing. Including ctime is the standard C++ convention.

The same reason you include iostream and not iostream.h.
Last edited on
Oh, I never knew that! Thanks! I've always included time.h... lol (:
Topic archived. No new replies allowed.