-
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;
}
-
you can just make a string, and everytime you want to add something to it instead of using cout
-
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