Homework assignment 90% complete but stuck

The purpose of this project is to give you an opportunity to apply your knowledge of looping structures in C++.
You must create a program that will display a christmas tree shape using the asterisk character. This program will prompt the user for a number of lines in the tree, then based on the user's input, it will use the asterisk character to display a christmas tree shape with that number of lines. So, for example, if the user enters 10, a christmas tree like this will be shown:

..........*
........***
......*****
.....*******
....*********
...***********
..*************
.***************
*****************
each line will contain an odd number of asterisks (1, 3, 5, 7, etc...)
You will have to play with the number of spaces displayed on each line, to make the display correct.
For this project you must create a C++ project that will:
Ask the user for the number of lines in their christmas tree.
Use cout to display the prompt to the user.
Prompt for user to enter number of lines
Use cin to store the input.
Using loops to cycle through, use cout to display the tree image to the screen.
Thank them for using your program.
Program should then pause and wait for the user to press a key before closing.


I have this code which does create the tree, but not centered. Any ideas on how to center the tree like the example above?

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
/*
Authored By: Al Woods
Date: 2/21/2014
Class: CSC215
*/

#include <iostream>

int main()
{
	int linesToAdd;
	int lines = -1;

	std::cout << "How many lines for the tree?";
	std::cin >> linesToAdd;
	for (int x = 0; x < linesToAdd;x++)
	{
		lines = lines + 2;
		for (int y = 0; y < lines; y++)
		{
			std::cout << "*";
		}
		std::endl;
	}
	system("pause");
}


EDIT: I added the periods to center the tree in the example.
Last edited on
On each line there are two things: whitespace and asterisks. You do only the latter. You have to compute the amount of indentation and print that (whitespace) first (separate loop).
Topic archived. No new replies allowed.