Can any one help or give some good advice on where I need to go from a point in my code. I am building an asterisks tree and I managed to get that part done, but there are three thing I need to input to complete the assignment.

1. And the line numbers to the asterisks.
2. Limit the asterisks tree to only 30 lines
3. If user does input more than 30 lines, program defaults to printing 30 asterisk lines with an message saying 'limit is 30 lines'

I pretty much have everything else done. here is the code:

****p.s please do include stdio library..I am not that far yet****

#include <iostream>
#include <iomanip>
using namespace std;
const int min_lines = 1;	//We want a minimum and maximum set 
const int max_lines = 30; 

int main()
{
int lines,                //This is the amount of lines you want
     spaces;             //This is for your spaces
char answer;        //To continue later, your answer will be in a for of 
                             //a letter
	
	//Beginning of our program...Let's make a tree! 

        cout << "We are going to make a tree of sterisks.\n"  
                <<  endl;			
	cout << "Please enter in your desired amount of lines for your 
                << "tree." 	        
	        << "\nNote: Your input must be between 1 and 30 lines." 
                << endl;
	cin >> lines;	          //Input the amount of lines desired.

	//The loop variable will begin here.

	for (spaces = 0; spaces < lines; spaces++ ) 
{		cout << setfill (' ') << setw (lines - spaces - min_lines) << 
                        << "";
		cout << setfill ('*') << setw (spaces * 2 + min_lines) 
                        <<""<< endl;
}                  //end of 'for' loop

	cout << "\n" << endl;		//To continue or end the 
                                                       //program
	cout << "Do you wish to try another set of lines?\n" << endl;
	cout << "Type 'y' to continue and 'n' to quit\n" << endl;
	cin >> answer; 
	cout <<	"" << endl;	   //Below is just in case 
                                                  //someone does not press 'y' or 'n' 

	if (answer >= 'a' && answer <= 'm' || answer >= 'o' && 
             answer <= 'x' || answer == 'z')
{		cout << "Invalid entry.\n" << endl;
		cout << "Please type in 'y' to continue or 'n' to end the 
                       << "program" << endl;
		cin >> answer;
		cout << "" << endl;
}
	while (answer != 'n' )	//The 'while' loop is needed to 
                                               //continue asking to 'continue'
{					       //with the program
	cout << "Please enter in your desired amount of lines for your 
                << "tree." << endl;
			cout << ' ' <<endl;
			cin >> lines;
			cout << "\n" <<endl;
		for (spaces = 0; spaces < lines; spaces++ ) 
{			 cout << setfill (' ') << setw (lines - spaces - 
                         << min_lines) << "";
			 cout << setfill ('*') << setw (spaces * 2 + 
                         << min_lines) <<""<< endl;
}
	cout << "\n" << endl;
	cout << "Do you wish to try another set of lines?\n" << endl;
	cout << "Type 'y' to continue and 'n' to quit\n" << endl;
	cin >> answer;
	
} //end of 'while' loop

return 0;

}

Recommended Answers

All 16 Replies

Oh... I forgot to how the asterisks tree should look like

1____*
2___***
3__*****
4_*******
5*********
(without the underscores)
Cheers

your code is hard to read use the button to organize the code

3- If user does input more than 30 lines, program defaults to printing 30 asterisk lines with an message saying 'limit is 30 lines'.

You don't have the limitation of the 30 lines??

here is the code i fixed and maybe its not the most simple one. Maybe you could make it more simple.
Add the 30 lines limitation:

#include <iostream>
#include <iomanip>
using namespace std;
const int min_lines = 1; //We want a minimum and maximum set
const int max_lines = 30;

int main()
{
int lines,spaces; //This is for your spaces
char answer;

//Beginning of our program...Let's make a tree!

cout << "We are going to make a tree of sterisks.\n"<< endl;

cout << "Please enter in your desired amount of lines for your"<< endl;
cin >> lines; 

if(lines>max_lines)
{
	cout<<"limit is 30 lines,please enter again\n";
    cin>>lines;
}



for (spaces = 0; spaces < lines; spaces++ )
 { 
  cout << setfill (' ') << setw (lines - spaces - min_lines)<<"";
  cout << setfill ('*') << setw (spaces * 2 + min_lines)<<""<< endl;
 } //end of 'for' loop


 cout << "\n" << endl; //To continue or end the
 cout << "Do you wish to try another set of lines?\n" << endl;
 cout << "Type 'y' to continue and 'n' to quit\n" << endl;
 cin >> answer;
 cout << "" << endl;

 if (answer >= 'a' && answer <= 'm' || answer >= 'o' && answer <= 'x' || answer == 'z')
{ 
 cout << "Invalid entry.\n" << endl;
 cout << "Please type in 'y' to continue or 'n' to end the program" << endl;
 cin >> answer;
 cout << "" << endl;
}

 do  //The 'while' loop is needed to
{ //with the program

  cout << "Please enter in your desired amount of lines for your tree." << endl;
  cout << ' ' <<endl;
  cin >> lines;
  cout << "\n" <<endl;

 if(lines>max_lines)
{
	cout<<"limit is 30 lines,please enter again\n";
    cin>>lines;
}

 for (spaces = 0; spaces < lines; spaces++ )
 { 
  cout << setfill (' ') << setw (lines - spaces - min_lines) << "";
  cout << setfill ('*') << setw (spaces * 2 + min_lines) <<""<< endl;
 }

  cout << "\n" << endl;
  cout << "Do you wish to try another set of lines?\n" << endl;
  cout << "Type 'y' to continue and 'n' to quit\n" << endl;
  cin >> answer;

 }while(answer != 'n'); //end of 'while' loop


return 0;

}
#include <iostream>
#include <iomanip>
using namespace std;
const int min_lines = 1;	//We want a min and max 
const int max_lines = 30;

int main()
{
int lines,  
     spaces; 
char answer; 
	
	//Beginning of our program...Let's make a tree! 

	cout << "We are going to make a tree of sterisks.\n" 
               << endl;			
	cout << "Please enter in your desired amount of lines for your  
                << "tree." 	        
	     << "\nNote: Your input must be between 1 and 30 lines.\n" << endl;
	cin >> lines;	//Input the amount of lines desired.
	cout << ' ' << endl;   //So that it look neat

	while (lines <= 0)	
                                            //Beginning of first 'while' loop.
{	cout << "Your input is invalid." 
		 << "Please enter in a number between 1 and 30" << endl;
	cin >> lines;

}   //End of first 'while' loop.

	//The loop variable will begin here.

	for (spaces = 0; spaces < lines; spaces++ ) 
{		cout << setfill (' ') << setw (lines - spaces - min_lines) << "";
		cout << setfill ('*') << setw (spaces * 2 + min_lines) <<""<< endl;

} //end of 'for' loop

	cout << "\n" << endl;		
	cout << "Do you wish to try another set of lines?\n" << endl;
	cout << "Type 'y' to continue and 'n' to quit\n" << endl;
	cin >> answer; 
	cout <<	"" << endl;	 

if (answer >= 'a' && answer <= 'm' || answer >= 'o' && answer <= 'x' ||  
    answer == 'z')

{
		cout << "Invalid entry.\n" << endl;
		cout << "Please type in 'y' to continue or 'n' to end the program"
         	        << endl;
		cin >> answer;
		cout << "" << endl;
}
	while (answer != 'n' )	//The second 'while' loop  

{							
	cout << "Please enter in your desired amount of lines for your tree." 
                << endl;
	cout << ' ' <<endl;
	cin >> lines;
	cout << "\n" <<endl;
	
	for (spaces = 0; spaces < lines; spaces++ ) 
{
	 cout << setfill (' ') << setw (lines - spaces - min_lines) << "";
	 cout << setfill ('*') << setw (spaces * 2 + min_lines) <<""<< endl;

}

	cout << "\n" << endl;
	cout << "Do you wish to try another set of lines?\n" << endl;
	cout << "Type 'y' to continue and 'n' to quit\n" << endl;
	cin >> answer;
	
}            //End of second 'while' loop

return 0;

}
}

sorry about that people

Thanks for using [code]

[/code] tags, but your formatting could still use some work :)

If you use [code=c++] [/code] tags, the code will have line numbers and syntax highlighting.

The first section trying to validate input:

cout << "Please enter in your desired amount of lines for your"<< endl;
cin >> lines; 

if(lines>max_lines)
{
	cout<<"limit is 30 lines,please enter again\n";
    cin>>lines;
}

needs to be in a loop so it can keep checking.

For this type of application either use a do { /* ... */ } while ( test ); or a while (test) { /* ... */ } where you make sure the test passes at least once.

This is an example of the second case:

lines = 0;
while (lines < min_lines || lines > max_lines)
{
    cout << "Please enter in your desired amount of lines for your"<< endl;
    cin >> lines; 
    if (lines < min_lines)
    {
        cout << "You must have at least one line.\n";
    } else if (lines > max_lines)
    {
        cout << "You can request at most 30 lines.\n";
    }
}

(This was in the 'post' buffer while I had to go play taxi driver)

Judging from the way the code colouring goes completely out of whack at about line 20, I'd say that you're missing a " on a previous line to end a string.

Ok people forget everything earlier.... I want to make my lines numbered and displayed vertically with the asterisks to its side.

1____*
2___***
3__*****
4_*******
5*********
"each underscore is a space"

here is my code

#include <iostream>
#include <iomanip>
using namespace std;

int spaces,
	size,
	lines,
	counter,
	fills,
	stars;
int main()
{
	cout << "Eter in lines" << endl;
	cin >> lines;
	cout << "\n" << endl;

	for(counter = 1; counter <= lines; counter++)  //counter
{		cout << setw (2) << counter;
	size = lines * 2;
	  for ( fills = 1; fills < size; fills = fills + 2 ) 
{       cout << setw(spaces = lines--);
		for (stars = 0 ; stars < fills; stars++)
		 cout << "*";
	     cout << endl;
}
}
cin.ignore (2);
return 0;
}

I have been doing this for hours and I have reached my limit.
Now I am at the point where someone has to show me where I am making a mistake.

Can anyone in god's green earth help me!

about this :
1____*
2___***
3__*****
4_*******
5*********
the code you write is pretty complicate and i don't know weather there have any relation between the code you post before and the code you post in this function??

And i test this code and rewrite it in this way--not sure weather its the best choice or not but it could implement the function.

int main()
{
	cout << "Eter in lines" << endl;
	cin >> lines;
	for (spaces = 0; spaces < lines; spaces++ )
 { 
  cout << spaces<<setfill (' ') << setw (lines - spaces - min_lines)<<"";
  cout <<setfill ('*') << setw (spaces * 2 + min_lines)<<""<< endl;
 }
system("pause");
}

....

#include <iostream>
#include <iomanip>

using namespace std;


int main(int argc, char **argv)
{
	int size, lines;

	// Get Number Of Lines
	cout << "Enter in lines: "; cin >> lines;
	cout << endl << endl;

	// Set Size Variable Equal To The Number Of Lines
	size = lines;		

	// Loop For All The Lines
	for (int counter = 1; counter <= lines; counter++) {

		// Make Sure We Always Use Two Digits To Keep 
		// Formatting Clean	
		if (counter < 10) {	
			cout << "0" << counter;
		} else {
			cout << counter;
		}		

		// Display The Number Of Spaces Entered By The User
		// It's The Same As the Number Of lines
		for (int i = 1; i <= size; i++) {
			cout <<  " ";
		}

		// Display The Required Number Of Asterisks 
		for (int i = 1; i < counter; i++) {
			cout << "*";
		}

		// Break To A Newline
		cout << endl;

		// We Decrement Size So That There Is One Less Space 
		// Than There Was On The Previous Parent Interation
		size--;
	}

	cin.ignore (2);
	return 0;
}

Now find a way to make it not go over 30.

sorry about the format. post it again.
If there have anything wrong or any risk in my code tell me plz.

const int max_lines = 30;

int spaces,
	size,
	lines,
	counter,
	fills,
	stars;
int main()
{
	cout << "Eter in lines" << endl;
	cin >> lines;
	cout << "\n" << endl;


	for (spaces = 0; spaces < lines; spaces++ )
 { 
  cout << spaces<<setfill (' ') << setw (lines - spaces - min_lines)<<"";
  cout <<setfill ('*') << setw (spaces * 2 + min_lines)<<""<< endl;
 }




system("pause");
}

min_lines is not declared, and it makes a pyramid (even spaces on both sides) not a slanted angle tree thing (unless the OP's drawing just sucks, and he really needs a pyramid instead of a slanted angle tree thing)

to Comatose:
the code functionality is not like the way Himerz want.
is there any misunderstanding??

No No, I mean, his drawing is bad, so I was unsure if the code was meant to be the way you had it or the way I did. I think he needs what you did however.

comatose, I do want it the shasha has it, but the numbers on the left need to be right-aligned , i.e:
_8
_9
10
and the first number needs to be 0

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.