I am creating a diamond but I am having problem. Right now, my output prints out 2 triangles underneath each other (is not displaying in the post correctly). I want to flip the 2nd triangle around to create a diamond shape. However, when I try to mess up with counters in the bottom function, the display messes up. Come someone give me any hints to help me figure out what is the problem? Appreciate it.


Current Output:

-*-
-**-
-****-
-******-
-*-
-**-
-****-
-******-

Expected Output

-*-
-**-
-****-
-******-
-****-
-***-
-*-

#include <iostream>
#include <iomanip>

using namespace std;

int top(int value, int userNumber);
int bottom(int value, int userNumber);

int main()
{
int userNumber;
int value;
int k;
int m;

cout << "Please enter a number: ";
cin >> userNumber;

int j = userNumber;
int o = userNumber;

k = top(j,userNumber);
m = bottom(o,userNumber);

system("PAUSE");
return 0;
}

int top(int value,int userNumber)
{
int k = userNumber;

if (value > 0)
{
cout << setw(value) << "*";

while (k > value)
{
cout << " " << "*";
k--; 
}
cout << endl;
value--;
return top(value,userNumber);
}
}

int bottom(int value,int userNumber)
{
int m = userNumber;

if (value > 0)
{
cout << setw(value) << "*";

while (m > value)
{
cout << " " << "*";
m--; 
}
cout << endl;
value--;
return bottom(value,userNumber);
}
}

Recommended Answers

All 4 Replies

Please repost the expected and actual output in code tags. All spacing gets stripped out otherwise so we don't know if that happened here. I suspect so because neither figure looks like a diamond. The expected output looks like a half-diamond. Is that what you want?

I believe the first figure is supposed to be 2 triangle, one on top of the other. The second figure is supposed to be a diamond. This is the output he wants. The format got messed up because he didn't enclose the figures in code tags.

Sorry... The current output is 2 triangles underneath each other. I am trying to turn the 2nd triangle upside down, so I can get an output of a diamond.

Current Output:

- *-
   -**-
  -****-
-******-
    -*-
   -**-
 -****-
-******-

Expected Output

- *-
    -**-
  -****-
-******-
  -****-
   -***-
     -*-

Sorry... The current output is 2 triangles underneath each other. I am trying to turn the 2nd triangle upside down, so I can get an output of a diamond.

Current Output:

- *-
   -**-
  -****-
-******-
    -*-
   -**-
 -****-
-******-

Expected Output

- *-
    -**-
  -****-
-******-
  -****-
   -***-
     -*-

Preview your post and correct the triangle before posting. Keep in mind that code tags post in Courier font, but when you type it in, it isn't. You should design it in a word processor in Courier font, then when you get it right, copy and paste it with code tags. "Triangle" is meaningless. Is it a right triangle or an equilateral triangle? With the code tags it looks like two right triangles on top of each other, but when I hit "Reply to", it looks almost, but not quite, like two equilateral triangles on top of each other. In other words, a diamond. But the * count is also somewhat off. There are also no hyphens in your code. Why are there hyphens in the figures? Please proofread and fix next time so we don't have to guess.

Do you want this?

*
  ***
 *****
*******
 *****
  ***
   *

I see no need for iomanip and setw. You can use them, I suppose, but there are no columns and you don't set "right" or "left", so you're stuck with the default, which you can obviously make work. But I'd do some plain old looping and displaying spaces and asterisks. I imagine you're not supposed to use iomanip anyway since this is a logic problem.

You should have way more descriptive variable names. It'll help you in your design. What do userNumber and value represent? Number of stars to display? Number of spaces to indent before displaying the first asterisk? Then name them that (or whatever they represent). Also, why bother to return anything? What's the return value represent? Do you use it? If not, make them void.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.