iteration, nested loops. I must be missing something.

I'm currently doing a beginners course in c++. They've asked me to write a program that asks the user for any number of lines and then writes it according to the input. After the figure is done it should ask for another number of lines. If you enter "0" the program should quit.
Here's my code so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
	cout<<"Enter the number of lines: ";
	int n;
	cin>>n;
	if(n!=0)
	{
	for (int i=1; i<n ;i++);
	}
	{
		for (int j=0; j<i; j++);
	}
	{
		cout<<('*');
	}
	cout<<endl;
	else
	cout<<"Quit";
	cout<<endl;
}


I get 2 errors in visual studio express;
Error 1 error C2065: 'i' : undeclared identifier
3 IntelliSense: identifier "i" is undefined
(line 14)

Error 2 error C2181: illegal else without matching if
4 IntelliSense: expected a statement
(line 20)

I've been staring myself blind on this. What am I missing here?
Thanks!
You have missed that a for loop ENDS at the semi-colon or the closing brace.

So here is your first for loop:
for (int i=1; i<n ;i++) /* HERE IS THE INSIDE OF THE FOR LOOP - THERE IS NOTHING HERE */;

Your braces also don't make much sense. Here is your code written how I think you meant it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if(n!=0)
{
  for (int i=1; i<n ;i++) // NO SEMI-COLON
  { // OPENING BRACE
  	for (int j=0; j<i; j++) // NO SEMI-COLON
  	{
  	       	cout<<('*');
         }
  }
}
else
{
  // some other code
}
here's an example of how they want it;


Enter the number of lines (0 to quit): 8

*
**
***
****
*****
******
*******
********

Enter the number of lines (0 to quit):

so basically it keeps on looping; asking the question, answers and asks the same question again and again until the user enters "0" and quits.
Looking over my previous post I'm not even sure I made sense or even if it's remotely close to what I want to achieve.


Thanks
edit: and sorry for being confusing :=)
Last edited on
you can try do while loop

1
2
3
4
do
{
//things you want to do
}while(n!=0);    //ends the loop when 0 is entered 
Last edited on
Topic archived. No new replies allowed.