Error: expected primary-expression before ')' token

I have encountered a problem with my program. I want to receive a number from the user and then display a triangle with the amount of stars equal to that number, eg. input of 5 should be:
* * * * *
* * * *
* * *
* *
*
* *
* * *
* * * *
* * * * *

Can anyone please help?

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
#include <iostream>
using namespace std;
int main()
{

    int number = 0;
    int test = 0;
    int i = 0;
    int k = 0;
    int lines_left = 0;

    cout << "Enter an odd number: ";
    cin >> number;

    test = number%2;
    i = 1;
    k = number;
    lines_left = k + 4;

    if (test == 0)
    {
        cout << "You have entered an invalid number! Please try again." << endl;
        exit(-1);
    }

    for (k >= 0;)
    {
        for (int i = 1; i <= k; i++)
        {
            cout << "*\t";
            cout << "\n";
            k = k - 1;
        }
    }

    for (k = 1; k <= number; k++)
    {
        for (i = 1; i <= k; i++)
        {
            cout << "*\t";
            printf("\n");
        }
    };

return 0;
}
If you're looping on a simple condition like (k >= 0)
don't do,
for (k >= 0;)
do,
while (k >= 0)

(for loop syntax requires two-semi-colons within the parentheses)
Note that you're incrementing i at the same time you're decrementing k.

And you never use your 'i' variable on line 8 in a meaningful way, so you should just remove that.
Last edited on
for statements like this need three parts and two colons. So change line 26
for (k >= 0; )
to
for ( ; k >= 0; )

That gets it to run. But to give something like your answer you need to move lines 31 and 32 out of the inner loop and also line 41 out of the inner loop.


You would do better to build programs slowly.
The error is on L26. A for statement has 3 parts separated by ; (eg as L36) If any part is not required it can be empty but the ; is still required. do you mean:

 
for (; k >= 0; )


or even:

 
while (k >= 0)

Topic archived. No new replies allowed.