0

The code is:

int euler4 ()
{
    for (int i = 999; i > 100; i= i - 1; ){
        for (int j = 999; j > 100; j--;){
            int n = i*j;
            bool ptest = ispalindrome(n);
            if (ptest){return n;}
        }
    }
    return 0;
}

The error is on the first line, int euler(), and it reads: "error: expected primary-expression before ')' token."

I am new to C++, but I have other for loops in the same code, and have had zero problems. I'm stumped.

Chris
  • 28,822
  • 27
  • 83
  • 158
  • 4
    One semicolon too much in the for-loop. Basic typo – Deduplicator Jan 19 '15 at 18:56
  • 1
    Pro-tip for debugging: if it mentions a specific line (and character), then look at the line or characters before what's in the error, as the parser usually errors out after the erroneous character, not on it, simply due to how parsing generally works. – ajp15243 Jan 19 '15 at 18:58
  • 1
    Yep - compiler thinks you're terminating the line and then starting the next one with a ')'. This is never correct syntax, thus the error you're seeing. Afaik this is basically compiler-speak for "...But what comes before this? Something is supposed to." – Conduit Jan 19 '15 at 18:58
  • 1
    When posting questions about errors, always include the complete and unedited error output. That error you show is not complete. Also, the error output shows that the error is *in* the `euler4` function, but it's not *at* the first line of the function. Lastly, the error output have line numbers, so you can see for yourself which exact line the error is on. – Some programmer dude Jan 19 '15 at 19:05
  • (spiritual @all) thanks guys, that was really great advice – Chris Jul 10 '20 at 19:23

2 Answers2

6

Remove the semicolon after i = i - 1 and after j--, like so

for (int i = 999; i > 100; i= i - 1 ){
    for (int j = 999; j > 100; j--){
Ishamael
  • 12,583
  • 4
  • 34
  • 52
1

You can do it this way

for (int i = 999; i > 100; i--){
    for (int j = 999; j > 100; j--){
        if (isPalindrome(i*j)){return n;}

Also try not to declare a variable in a loop. You can declare outside the loop and reassign its value in the loop.

user3437460
  • 17,253
  • 15
  • 58
  • 106
  • 1
    I disagree with assignment outside the loop in this case - [this answer sums my feelings up nicely](http://stackoverflow.com/a/7959658/1732865) – Conduit Jan 19 '15 at 19:18