How to code using for or while?

Hi!

Do you have any idea how to code
the program in such that the user will enter a value and asterisks will display

example:
Enter a value: 6
*
**
***
****
*****
******

Enter a value: 2
*
**

Enter a value: 8
*
**
***
****
*****
******
*******
********

If for or while is not the right code to be use in this program other codes can also be used.
Last edited on
I'll assume you are not asking us to do your homework, I could be that naive:

Here's a simple way of achieving what you need without nested loops, you'll need to include the string header.

For "line number" 1 until "line number equals the user input":

print "line number" asterisks.

You can print multiple asterisk like this:

cout<<string(6, '*')
@eidge
It's just a practice task for the upcoming exam.
yes I am not asking you to do it for me:))
I am just getting ideas on how to do it.

Thanks for the suggestion!

what about with nested loops?

I only know how to use nested loops with numbers or integers. What about with this kind of problem?

Also I have another question about "for" function.
Can I include another "for" under a "for"?

Example:
for(x;x<3;x=x+1){
. for(y;y<3;y=y+1){

. }

}

is this possible?
Last edited on
yes you can.

You could use a for to go through each line then another one to print the number of asterisk you need.
how can I do this?

until "line number equals the user input":


is it like this?


printf("Enter a value:");
scanf("%i", &x);

for(x=0;x=x;x=x+1){


You can print multiple asterisk like this:

cout<<string(6, '*')


Can I also print asterisks witht the desired value(entered value by the user) with printf?
Last edited on
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
#include <iostream>

void DrawLines( int );

int main()
{
	int numLines;
	std::cout << "How many lines would you like to print: ";
	std::cin >> numLines;
	DrawLines( numLines );

    return 0;
}

void DrawLines( int numLines )
{
	int curLine = 1;

	for ( int i = 0; i < numLines; i ++ )
	{
		for ( int i = 0; i < curLine; i ++ )
			std::cout << "*";

		curLine++;
		std::cout << "\n";
	}

}
@TheNoobie
Whats with that?
up
I would avoid the reuse of a variable name within a function. Using distinct names for different loops variable helps human being to understand code better. In this case it also allows you to eliminate the temporary "curLine".

Being pedantic, I have also taken the opportunity to make the variable names more descriptive and replaced "\n" with endl.

1
2
3
4
5
6
7
8
9
10
void DrawLines( int numLines )
{
	for ( int idxLine = 0; idxLine < numLines; idxLine ++ )
	{
		for ( int idxStar = 0; idxStar <= idxLine; idxLine ++ )
			std::cout << "*";

		std::cout << std::endl;
	}
}


Note the tweak in the termination condition of the inner loop.
Last edited on
@TheNoobie there's a reason I didn't write the code for him.

First, because I'm too lazy to do it.

Second and most importantly he will not learn anything by copying code and compiling it.

The reason you could solve that yourself is:

a) it's not difficult.
b) Back in the days someone presented you with a simple challenge like that and you've struggled with it until you've beaten it, please let other do so as well.

Back on topic:

I suppose you're still not confortable or haven't learn functions yet.

That's almost it. Try and give more descriptive names to vars as andy told you.

So it should be something like this:

I assume that you store the input from the user to a variable called number_of_lines
1
2
3
for(int line = 1; line <= number_of_lines; line = line +1){
 //print your **** here.
}


Try using the piece of code I given you before cout<<string(6, '*') and change 6 to other numbers and see what happen. You can also use an int variable instead of a number, something like:

int myvar = 3;
cout<<string(myvar, '*'); Why don't you compile that and see what happens?

Topic archived. No new replies allowed.