Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

chris200x9

macrumors 6502a
Original poster
Jun 3, 2006
906
0
hi I am trying to make a program to create a triangle of #'s but I can't get setw to work

here is my code:
Code:
          #include <iostream>
#include <iomanip>
using namespace std;
int main () {
    int row = 0;
	int numSymbols = 1;
	int counter;
	int length;
	int numspace;
	numspaces = length;
	cin >> length;
	while( row < length )
	{
		// output spaces
		cout << setw( numspace );

		// output pound signs
		counter = 0;
		while( counter < numSymbols ) 
		{
			cout << "#";
			counter++;
		} // end while        
		cout << endl;

		// decrease number of spaces
		numSpaces--;

		// increase number of symbols to display
		numSymbols = numSymbols + 1;

		// go to next row
		row++;
		}


    return 0;
}
 
yes it runs it prints #'s like so

#
##
###

I need
Code:
      #
    ###
  #####
#######

arg it wont format right on the forum but its supposed to be a triangle
 
1. numspace witch is tied into length controls field width

and 2. I dont get your qestion
 
Correct. numspace (or numspaces or numSpaces - whatever you settled on), controls the field width.

To see what the value of numspace is each loop, what can you add to the program?
 
ok i feel really dumb but I don't know I thought of adding numspace into the loop but that didnt work hmmmmm..... sorry

by the way thanx for being so cool
 
length but I tried putting numspace = length in the loop and i got the same output
 
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main () {
    int row = 0;
	int numSymbols = 1;
	int counter;
	int length;
	int numspace;
	numspace = length;
	cout << numspace;
	 cin >> length;
	while( row < length )
	{
		// output spaces
		cout << setw( numspace );

		// output pound signs
		counter = 0;
		while( counter < numSymbols ) 
		{
			cout << "#";
			counter++;
		} // end while        
		cout << endl;

		// decrease number of spaces
		numspace = numspace - 2;

		// increase number of symbols to display
		numSymbols = numSymbols + 2;

		// go to next row
		row++;
		} 


    return 0;
}
 
but i gtg to bed I'm just turning in this code but thank you so much for everything you are awesome!
 
OH MAN!!! YOU ARE SOOOOO CLOSE to having it worked out.

Change your cout to this:

cout << "numspace = >" << numspace << "<" << endl ;

And ask yourself, is it what I expect it to be? (which is the same value as length)

Todd
 
I talked with Chris, and he didn't figure it out and turned in his assignment as was. In a private msg, I told him I would post the code here for him to see the solution.

Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main () {
    int row = 0;
    int numSymbols = 1;
    int counter;
    int length;
    int numspaces;
//  numspaces = length;
    cin >> length;
    numspaces = length; //[color=red]This line was out of order!![/color] 

    cout << "numspaces = " << numspaces << endl ; 
    while( row < length )
	{
		// output spaces
		cout << setw( numspaces );

		// output pound signs
		counter = 0;
		while( counter < numSymbols ) 
		{
			cout << "#";
			counter++;
		} // end while        
		cout << endl;

		// decrease number of spaces
		numspaces--;

		// increase number of symbols to display
		numSymbols = numSymbols + 1;

		// go to next row
		row++;
		}
    return 0;
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.