We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,599 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

C++ triangle using nested for loops

Hey everyone,

I am new to this site, basically my problem is this: The user inputs an integer, and then the user inputs a character, the program is supposed to make a triangle out of the character, with the max width of the user inputed integer.

for example;

user inputs integer 5
user inputs character *

output:
*
**
***
****
*****
****
***
**
*

the following code that I have written only takes me about half way there and gets me to:
*
**
***
****
*****

#include "StdAfx.h"
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
     int num;
	 int rows;
	 char x;
     do
	 {
		 cout << "Enter an integer" << endl;
		 cin >> num;
		 cout << "Enter symbol" << endl;
		 cin >> x;
	 
		for(rows=0;rows<num;rows++)
		 {
		 for (int axis=0; axis<=rows;axis++)
		 {
            cout<<x;
            }
            for (int len=num;len>rows+1;len--)
            cout<<" ";
            cout<<endl;
            }
            cin.ignore();
            cin.ignore();
            } while(cin);
	return 0;
}

Any help/response is much appreciated, thanks!

5
Contributors
8
Replies
2 Days
Discussion Span
2 Years Ago
Last Updated
11
Views
infiniteloop56
Newbie Poster
8 posts since May 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

the following code that I have written only takes me about half way there

Excellent. Now do the same thing again, but this time reverse your starting and ending points, and count down instead of up.

Narue
Bad Cop
Team Colleague
15,460 posts since Sep 2004
Reputation Points: 6,483
Solved Threads: 1,407
Skill Endorsements: 55

Hi, thank you for the response, I tried doing what I thought you said to do and I just ended up in an infinite loop. Could you explain in a little more detail about reversing my starting and ending points? Thank you.

infiniteloop56
Newbie Poster
8 posts since May 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hi, thank you for the response, I tried doing what I thought you said to do and I just ended up in an infinite loop. Could you explain in a little more detail about reversing my starting and ending points? Thank you.

the reverse part is simple,
your original code was something like this
for(row=0;row<num;row++)
{ for(axis=0;axis<=row;axis++)
{ cout<<x; }
endl;
}
just change the row and axis to its max value and decrease through the loop
row = num, row-- through each loop, axis = num, >0, axis--
something like that will make another reverse triangle

hmm, one question, why do you need the spaces?
usually that appears when the question asking you to build a triangle like this
...x
..xxx
xxxxx
..xxx
...x
you wont need that in this one i think

bigwhiteegg
Light Poster
41 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

@bigwhiteegg
-Thank you for your comment, I tried what you said but I couldn't crack it. cheers.
I am still having some difficulties,I was however, able to re-format my code. This made it a little more easier to follow.

#include "StdAfx.h"
#include <iostream>
using namespace std;

int main()
{
	int numb;
	int numb2;
	int numb3;
	int numb4;
	int input;
	char x;

	do
	{
		cout << "Enter an integer"<< endl;
		cin >> input;
		if(input == 0)
		{
			cout << "Thank you come again" << endl;
			break;
		}
		else
		{
			cout << "Enter a character" << endl;
			cin >> x;
			numb = input;
			numb2 = input;
			for(input = 0; input < numb; input++)
			{
				for(numb2 = 0; numb2 <= input; numb2++)
						cout << x;
						cout << endl;
					for(numb3 = numb-1; numb3 > 0; numb3--)
					{
						for(numb4 = numb +1; numb4 <= numb3; numb4++)
						cout << x;
						
					}
	
				}
			
		}
	}while(cin);
	return 0;
}

Again any help is appreciated, thanks!

infiniteloop56
Newbie Poster
8 posts since May 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

@bigwhiteegg

Thank you for you help, I tried you suggestion but I was not able to do the bottom half. I only ended up in the same place. I was however able to reformat my code to make it easier to follow. Again it basically outputted the same material as before.

This is an example run with my current code:


Please enter an integer.
5
Please enter a character.
+

+
++
+++
++++
+++++
--------------------------
This is what the program is supposed to return:

please enter an integer.
5
please enter a character
+

+
++
+++
++++
+++++
++++
+++
++
+
------------

I can get the top, but not the bottom.

Reformatted code:

#include "StdAfx.h"
#include <iostream>
using namespace std;

int main()
{
	int numb;
	int numb2;
	int numb3;
	int numb4;
	int input;
	char x;

	do
	{
		cout << "Enter an integer"<< endl;
		cin >> input;
		if(input == 0)
		{
			cout << "Thank you come again" << endl;
			break;
		}
		else
		{
			cout << "Enter a character" << endl;
			cin >> x;
			numb = input;
			numb2 = input;
			for(input = 0; input < numb; input++)
			{
				for(numb2 = 0; numb2 <= input; numb2++)
						cout << x;
						cout << endl;
					for(numb3 = numb-1; numb3 > 0; numb3--)
					{
						for(numb4 = numb +1; numb4 <= numb3; numb4++)
						cout << x;
						
					}
	
				}
			
		}
	}while(cin);
	return 0;
}

I have attached to c++ file (.cpp) for anyone who would like it.
Again any help is appreciated. Thank you!

Attachments Source1.cpp (0.72KB)
infiniteloop56
Newbie Poster
8 posts since May 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

@bigwhiteegg

Thank you for you help, I tried you suggestion but I was not able to do the bottom half. I only ended up in the same place. I was however able to reformat my code to

what you did in the beginning was correct
now it totally doesn't make sense
what you should do is have two separate nested for loop, not inside it
one build the top
one build the bottom

bigwhiteegg
Light Poster
41 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hello infiniteloop56,
Use separate nested for loops for top and bottom patterns. Have one nested for loop for the top pattern and have separate nested for loop for the bottom pattern.
The one below is for bottom pattern...

for(c = 0; c < numb; c++)
{
  for(d = numb-1; d > c; d--)
   {
     cout << x; 
   }
}

In your code the for loop in line 36 will not function . numb4 will always be greater than numb3. Check the condition in that for loop and accordingly increment or decrement.

Arbus
Practically a Master Poster
615 posts since Dec 2010
Reputation Points: 45
Solved Threads: 31
Skill Endorsements: 0
#include <iostream>                          
#include <conio>
#include <process>
void main(){
int s;
cout<<"Enter Your Choice :"<<endl;
cout<<" Select 1 to draw a triangle"<<endl;
cout<<"Select 2 to Exit"<<endl;
cin>>s;
switch(s){
case 1:
{char c;
int num;
cout<<"Enter Integer Num:";
cin>>num;
cout<<endl;
cout<<"Enter symbol:";
cin>>c;
cout<<endl;
for(int j=0;j<=num;j++)
{for(int i=0;i<=num;i++ )
if(i<=j)
  cout<<c;

    cout<<endl;

   }
   for(int x=0;x<=num-1;x++)
   {for(int y=0;y<=num-1;y++)
   if(x<=y)
   cout<<c;
   cout<<endl;}}

  getch();  } }
goldzero
Newbie Poster
5 posts since Sep 2009
Reputation Points: 11
Solved Threads: 2
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0900 seconds using 2.69MB