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:
* * * * *
* * * *
* * *
* *
*
* *
* * *
* * * *
* * * * *
#include <iostream>
usingnamespace 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;
}
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.
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: