Expected Unqualified-id Before '{' Token

I don't see what's wrong with this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  #include <iostream>
#include <cmath>
using namespace std;

int main()
{

}
int isPerfect;
{
    int n, sum = 0, f;
cout << "Enter a number: " << endl;
cin >> n;
while (n < 0)
{
cout << "Enter a number: " << endl;
cin >> n;
}

bool result;

for( f = 1; f <= n/2; f++)
if ( n % f == 0)
sum += f;

if ( sum == n)
result = true;
else
result = false;


if (result = true)
cout << "The number " << n << " is a perfect number." << endl;
else if (result = false)
cout << "The number " << n << " is not a perfect number." << endl;
}
Line 9.

When you're defining a function, do not put a semi-colon after the brackets.

Also, I can only guess what is meant to be inside your for and if and else blocks. Use braces.
Last edited on
I did all that but somehow I got even more errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <cmath>
using namespace std;

int main()
{

}
int isPerfect
{
    int n, sum = 0, f;
cout << "Enter a number: " << endl;
cin >> n;
while (n < 0)
{
cout << "Enter a number: " << endl;
cin >> n;
}

bool result;

for( f = 1; f <= n/2; f++)
{
  if ( n % f == 0)
  {
      sum += f;
  }
}


if ( sum == n)
{
   result = true;
}

if else
{
    result = false;
}

if (result = true)
{
    cout << "The number " << n << " is a perfect number." << endl;
}
else if (result = false)
{
    cout << "The number " << n << " is not a perfect number." << endl;
}
}


It now says that I have the same error I had before in line 11
line 9 needs parenthesis if it is a function: int isPerfect()

line 36 - just else, not if else

line 41 - if you're checking equality, use == operator, not assignment operator =

line 45 - can just be else with no condition in ()
Last edited on
This:
1
2
3
4
5
6
int main()
{

}
int isPerfect;
{
should read more like this:
1
2
3
4
5
6
7
8
9
void  isPerfect();    // declare function prototype

int main()
{
    isPerfect();      // call the funxction
}

void  isPerfect()     // actually define the function
{


I didn't check all the logic. But be careful about using a single = (assignment operator) where you meant to use == (compare operator).
Something isn't right with line 47, with the else and I can't figure it out.
1
2
3
4
5
6
7
8
if (result == true)
{
    cout << "The number " << n << " is a perfect number." << endl;
}
else // just else, no condition
{
    cout << "The number " << n << " is not a perfect number." << endl;
}
Topic archived. No new replies allowed.