C++ Expected Primary-Expression Error

Feb 8, 2012 at 8:53am
This is even/odd problem.

This is my code:

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
// Lab 1 - Review
#include <iostream>
using namespace std;

const int number = 3;

// Function prototypes
bool parityArray(int [], int);

int main ()
{
    int array[number];
    bool check = parityArray(int [], int);

    for(int i = 0; i < number; i++)
        {
            cout << "Please enter a positive number: ";
            cin >> array[i];
        }

    for(int i = 0; i < number; i++)

            cout << array[i] << endl;

    for(int i = 0; i < number; i++)
        {
            if(check == true)
                cout << array[i] << " is even." << endl;
            else
                cout << array[i] << " is odd." << endl;
        }

    return 0;
}

bool parityArray(int array[], int number)
{
    for(int i = 0; i < number; i++)
    {
         if(array[i] % 2 == 0)
            return true;
        else
            return false;
    }

}


The error: Line 13: expected primary-expression before 'int'

Thanks so much in advance for any help.

Vu Dinh
Last edited on Feb 8, 2012 at 8:54am
Feb 8, 2012 at 9:44am
The coding is hopeless....

It is obvious that once parityArray returns a value ( i=0 )then the function is never accessed again and hence nature of the following numbers cannot be known. By the way,how do you expect the program to tell weather odd or even before even accepting the values????

I am sorry to be rude but you have to think well before posting it here...
Feb 8, 2012 at 10:50am
Sorry for the confusion, my code is just hopeless as much as I'm right now. I will redo it.
Feb 8, 2012 at 11:10am
OK, this is my original code which is working just fine.

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
// Lab 1 - Review
#include <iostream>
using namespace std;

const int number = 3;

int main ()
{
    int array[number];

    for(int i = 0; i < number; i++)
        {
            cout << "Please enter a positive number: ";
            cin >> array[i];
            cout << "\n";
        }

    for(int i = 0; i < number; i++)

            cout << "\n" << array[i] << endl;

    for(int i = 0; i < number; i++)
        {
            if(array[i] % 2 == 0)
                cout << "\n" << array[i] << " is even." << endl;
            else
                cout << "\n" << array[i] << " is odd." << endl;
        }

    return 0;
}


The problem is the assignment requires a second function along with main function. I'm just confused.

Write a program that contains 2 functions in addition to main. The first one takes 2 arguments (an int array and an int) and returns nothing. It prints the entire contents of the array argument, one element per line. You MUST use a for loop to print the array contents. The second function takes 1 argument (an int) and returns a bool indicating whether or not the argument was even or odd.

Your main function should:
+Declare an int array of size 3.
+Prompt the user for the contents of each array element. Store each input in the array that you declared in Step 1. The user prompt and assignment statements MUST be inside the body of a for loop.
+Call the first function that prints an array, passing it the filled array and the array size.
+Repeatedly call (using a for loop) the second function each time passing it a different element in the array until all elements have been passed. Based on the return value of the function, display a message like: " ______ is odd" or "_____ is even"
Feb 8, 2012 at 1:20pm
Just shift the checking condition into the fuction and pass a[] and i as arguments.
Make the function do the printing work.
Feb 8, 2012 at 1:32pm
This is what I come up with. It's working but it won't pause after executing.

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
// Lab 1 - Review
#include <iostream>
using namespace std;

const int number = 3;

bool parity(int);

int main ()
{
    int array[number];

    for(int i = 0; i < number; i++)
        {
            cout << "Please enter a positive number: ";
            cin >> array[i];
        }
    cout << "\n";
    for(int i = 0; i < number; i++)
        {    
            cout << array[i] << endl;
        }   
    cout << "\n";
    for(int i = 0; i < number; i++)
        {    
             if(parity(array[i]) == true)
                   cout << array[i] << " is even." << endl;
             else
                   cout << array[i] << " is odd." << endl;                   
        }
    return 0;
}

bool parity(int n)
{
     if(n%2 == 0)
            return true;
     else
            return false;
}
Feb 8, 2012 at 1:36pm
Never mind. I just put system("pause"); before return 0; and it's working.

Thanks.
Topic archived. No new replies allowed.