Hey guys,
I justs started this code to make a diamond with stars (*), but I get confused how to print the spaces that I need, I don't know if you understant what I'm trying to say. It is something like this:

Prompt the user to enter (odd) height (of a diamond) and outputs the following:

Example height = 7

Output:

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

and this is the code I'm working on, it is very basic but I'm struggling finding the mistakes and that is just the top part of the diamond:

#include <iostream>
#include <string>

using namespace std;


int main(){

	int h;
	cout<<"Enter any positive number: ";
	cin>>h;
	
	int str = 1;
	int spc = h/2;

	for(int k=0; k<str; k++){
		cout<<"*"<<endl;
		for(int i=0; i<spc; i++)
			cout<<" "<<endl;

		str = h/2+1;
		spc--;

	}

system("PAUSE");
return 0;

}

If you have any suggestions please let me know, THANKS!

You still have the endl (s) in your code. You need to get rid of those otherwise you will be printing out stars and spaces with a carriage return line feed after them. Here's a hint - on the way down you're subtracting one space, once you get to the halfway mark you are adding a space. In other words pop in some if statements. Don't be afraid to try a bunch of different things, you'll learn a lot that way.

In the future, don't just ditch your post for a new one, as adding a reply to it will bump it up on the list anyway.

Ok, you say that I must get rid off the (endls), but as I do that I get only a line with 4 stars, with them on the code, I get 4 lines with a star in each line. Is just that I don't exactly know what to put in an if statement.

Ok, you say that I must get rid off the (endls), but as I do that I get only a line with 4 stars, with them on the code, I get 4 lines with a star in each line. Is just that I don't exactly know what to put in an if statement.

You're printing a bunch of spaces, then an endline. Don't do that. It's pointless to have the last character in a line be a space.

Here's what you need to do:

  1. Display a certain number of spaces.
  2. Display a certain number of asterisks.
  3. Display a newline/endl.
  4. Repeat steps 1 - 3 a certain number number of times.

Your job is to figure out what these "certain numbers" are. From the skeleton above, Right now you can replace them variables called x, y, and z and set up your skeleton. Then start giving them values. You need to find their relationship to the input (h) variable and each other. The key word is "variable". Their values will vary.

Here's a p-code skeleton. If you're still having trouble, figure out what it takes to print just the stars on separate lines with the right count, then figure out how to print out the spaces with one star at the end of each line. Then combine the two.

for loop (stepping through the lines)
{
          if(above the midpoint)
              {number of spaces =
                number of stars =  
                }
           else
               {  number of spaces =
                 number of stars =  
                }
          for loop (using number of spaces)
                  print spaces
          for loop (using number of stars)
                  print stars

         Now print an endl to go to the next line.
}

This is one of those times when putting it out on paper can help too.

This project interested me, and I decided to try it out for myself. I did succeed in making it, but it is not the best way to do it. I am 100% sure that there was a simpler way to do this than what I did, so if you want a good grade on the assignment, do not use my code word-for-word.

Anyway, if it helps you for reference, here is my code:

#include <iostream>
#include <string>
using namespace std;
int main()
{

	int num1;
	cout<<"Enter an odd number: ";
	cin >> num1;
	num1 = num1/2+1;
	for( int i = 1; i <= num1; i++ )
	{
		for( int c = 10; c >= i; c-- )
			cout << " ";
		for( int c = i; c >= 1; c-- )
			cout << "*";
		for( int c = i-1; c >= 1; c-- )
			cout << "*";
		cout << endl;
	}
	for( int i = 1; i <= num1; i++ )
	{
		for( int c = num1-10; c <= i; c++ )
			cout << " ";
		for( int c = num1+2; c >= i+3; c-- )
			cout << "*";
		for( int c = num1; c >= i+2; c-- )
			cout << "*";
		cout << endl;
	}
	return 0;
}

Explanation of what I did:
I figured out how to make triangles such as these:

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

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

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

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

I then combined the codes for the triangles in an order that would form the shape of the diamond. (I am sure you know how to make a diamond with triangles, and that was it!.)

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.