Thread: Diamond diplay

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    1

    Diamond diplay

    I'm having trouble getting this to work with only 2 cout statements. I have 4 at the moment. This is my first class in programming. Any help would be appreciated, to shine some light on my frustrated head.

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int const MAXSTARS = 9;
    int i , j ;
    
    class Diamond
    {
    private:
    
    public:
    	Diamond(){
    		
    		// top half of diamond
    		for(i = 0 ; i < (MAXSTARS/2 + 1) ; i++)
    		{
    		for(j = i ; j < (MAXSTARS/2) ; j++)    // used to align, putting in spaces
    			cout << " " ;
    
    		for(j = 1 ; j <= (i*2 + 1) ; j++)  // puts in the actual astericks
    			cout << "*" ;
    
    		cout << endl ;
    		}
    
    	// bottom half of diamond
    		for(i = (MAXSTARS/2) ; i > 0 ; i--)
    		{
    		for(j = (MAXSTARS/2 + 1) ; j > i ; j--)  // used to align, putting in spaces
    			cout << " " ;
    
    		for(j = (i*2 - 1) ; j > 0 ; j--) // puts in the actual astericks
    			cout << "*" ;
    
    		cout << endl ;
    		}
    
    	}
    
    
    
    };
    int _tmain(int argc, _TCHAR* argv[])
    {
    	Diamond DM;
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    you can just make a string, and everytime you want to add something to it instead of using cout

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello,

    Maybe you get a idea from this code which made a triangle.
    Your Diamond is two triangles.

    Code:
    #include <string>
    
    using namespace std ;
    
    
    int main()
    {
    
    
      // hoe groot moet de driehoek worden
      int height = 31 ;
    
      // Hoeveel rijen heeft de driehoek nodig
    
      int rijen = height - (height/2);
    
      for (int r = 0; r != rijen; ++r) {
    
    		string::size_type c = 0;
    
    		// invariant: we have written `c' characters so far in the current row
    		while (c != height ) {
    
                    // are we on the border?
    				if (r==0 || c==r || c == height -r -1)
    				    {
    				       cout << "*";
                            c++ ;
    				    }
    				else
    				    {
                          cout << ' ' ;
                          c++ ;
    				    }
    		}
    
    		cout << endl;
    	}
    
    }
    Roelof

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how can I complete this code ?, diamond shape
    By cucumber in forum C++ Programming
    Replies: 1
    Last Post: 11-11-2009, 03:50 PM
  2. Simple Blackjack Program
    By saber1357 in forum C Programming
    Replies: 1
    Last Post: 03-28-2009, 03:19 PM
  3. Bitwise Unwanted Output
    By pobri19 in forum C++ Programming
    Replies: 4
    Last Post: 09-15-2008, 04:07 AM
  4. making a diamond using functions
    By Nikisha in forum C++ Programming
    Replies: 4
    Last Post: 03-30-2003, 09:05 PM
  5. Diamond
    By C++Nerd in forum C++ Programming
    Replies: 1
    Last Post: 10-04-2002, 04:36 AM