setw c++ help

Discussion in 'Mac Programming' started by chris200x9, Oct 15, 2007.

  1. chris200x9 macrumors 6502a

    Joined:
    Jun 3, 2006
    #1
    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;
    }
     
     
  2. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #2
    What's wrong with this picture?
    Code:
    int length;
    int numspace;
    numspaces = length;
    cin >> length;
    
    Todd
     
  3. chris200x9 thread starter macrumors 6502a

    Joined:
    Jun 3, 2006
    #3
    I don't know?
     
  4. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
  5. chris200x9 thread starter macrumors 6502a

    Joined:
    Jun 3, 2006
    #5
    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
     
  6. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #6
    Two questions.

    1) What controls the field width?

    2) How can you inspect that value of what controls the field width at run time?
     
  7. chris200x9 thread starter macrumors 6502a

    Joined:
    Jun 3, 2006
    #7
    1. numspace witch is tied into length controls field width

    and 2. I dont get your qestion
     
  8. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #8
    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?
     
  9. chris200x9 thread starter macrumors 6502a

    Joined:
    Jun 3, 2006
    #9
    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
     
  10. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #10
    The first time through the loop, what do you want numspace to be equal to?
     
  11. chris200x9 thread starter macrumors 6502a

    Joined:
    Jun 3, 2006
    #11
    length but I tried putting numspace = length in the loop and i got the same output
     
  12. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #12
    ok, good. Does it equal length before the loop? Do a "cout" to it and see.
     
  13. chris200x9 thread starter macrumors 6502a

    Joined:
    Jun 3, 2006
  14. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
  15. chris200x9 thread starter macrumors 6502a

    Joined:
    Jun 3, 2006
    #15
    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;
    }
    
     
  16. chris200x9 thread starter macrumors 6502a

    Joined:
    Jun 3, 2006
    #16
    but i gtg to bed I'm just turning in this code but thank you so much for everything you are awesome!
     
  17. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #17
    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
     
  18. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #18
    Chris, Did you ever figure out what you needed to do to fix it? Did you slap yourself upside the head and say "Oh, MAN! I was SO close!"?
     
  19. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #19
    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;
    }
     

Share This Page