Newb Here needing a nudge in the right direction!

So I have this lab to do for class and I'm stuck on it. It has to do with nested loops, and it needs to output rows that are equal to the number that you put in. Each row has to output a differnt number of "." and "*" based on what number you put in. A correct one should be
....*
...**
..***
.****
*****
for the number five however I'm not so lucky. I have the nested loop set up but I have no idea how to get started on getting it to output like that mine simply outputs
.*
.*
.*
.*
.*
for the number 5 of course I know thats because of how I have the output message set up I just don't really know where to get started. Any help would be awesome its due tuesday and I've been working on it all weekend. *mostly reading the text book haha*


Here is my code

#include <iostream>

using namespace std;

int main()

{
int num;
int count;

cout << "Enter number of rows: " << endl;
cin >> num;

for (count=0;count<num;count++)
for(count=0;count<num;count++)
{cout << ".";
cout << "*" << endl;
if (count==num) break;
}


}
Thanks again, I'm new to C++ :)
Try this :) You just needed to add a conditional check to check to see if '*' should be printed or '.'. Also I would avoid using the same variable as the conditional variable in two nested loops. That can be dangerous and confusing!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main(){	
	int num;
	cout << "Enter number of rows: " << endl;
	cin >> num;

	for(int i = 0; i <= num; i++){
		for(int j = 0; j <= num; j++){
			if(j >= num - i)
				cout << "*";
			else
				cout << ".";
		}
		cout << endl;
	}
}
Last edited on
Thats extremely close. I didn't even think about using an IF I feel even more noob now lol. The only problem with the output is that it has one extra character. The output ends up being
.....*
....**
...***
..****
.*****
******
Where is our instructions are it has to have the max characters of the number put in.
so it would be like
....*
...**
..***
.****
*****
Thats pretty impressive how you got it to do it so quickly I've been looking at this text book for hours and it just never occured to me to ALSO use a IF haha.
Ah yes sorry I clearly didn't check that properly. That can be fixed with a few minor changes :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main(){	
	int num;
	cout << "Enter number of rows: " << endl;
	cin >> num;

	for(int i = 0; i < num; i++){ // Changed the '<=' operator to '<'
		for(int j = 0; j < num; j++){ // changed the '<=' operator to '<'
			if(j >= num - (i + 1)) // changed 'num - i' to 'num - (i + 1)'
				cout << "*";
			else
				cout << ".";
		}
		cout << endl;
	}
}
That is so epic, I was working on it myself and was thinking about the <= becoming < but I had no idea about the (i+1) Tyvm for all your help :)
Topic archived. No new replies allowed.