Incrementing sign and lines

Hi, english is not my mother tongue so bear with me please.

I have an assignment where we're going to write a program with the following effect:

Enter lines: 2
*
**

Enter lines: 4
*
**
***
****

...and so on.

I can't really figure out how to do this though. I'm guessing I should use a another loop inside a for loop, but I don't know how to implement it without making the nestled one infinite.

One way I could think of is storing the *'s in a string and print that, but that feels somewhat "ugly".

I'd obviously ask my teacher if possible, but since it's a distance class he's been hard to get hold of and I'd really like to figure this out before the weekend.

I also don't want a complete solution, just some hints.

Thanks!
Last edited on
You need two for loops. One to write each line and another inside it to write '*' symbols.
Or you can use the setfill and setw manipulators




Regarding the loops, what code did you try?
Last edited on
Thank you, I got it now. I figured I might as well show what I wrote in case anyone would want to point out something I'm doing wrong, or for others to learn from (even if it was indeed a simple task :P).

1
2
3
4
5
6
7
8
9
10
11
12
13
int val;
do
{
	cout << "Enter lines (0 to quit): ";
	cin >> val;
	for(int i=1;i<=val;i++)
	{
		for(int a=1;a<=i;a++)
			cout << "*";
		cout << endl;
	}
}while(val!=0);
cout << "Done!";


Thanks again!
Conditions check if a value is equal or not to 0 so in line 12 you can use }while(val); which is shorter
Last edited on
You could also start with a string initialized to "*", perform the outer loop, and append the string to itself each time; although, I don't think this is what the teacher would be after...

EDIT: Scratch that idea; I was thinking of the similar assignment where each line of asterisks doubles in length.

ANOTHER EDIT: You could still start with an empty string and append a single "*" each time.
Last edited on
Topic archived. No new replies allowed.