Please help with little C++ program...

Started by
11 comments, last by TheUnbeliever 16 years, 1 month ago
I have to output a diamond. But first I am trying to output this... diagonal length: 9 Character: * * *** ***** ******* ********* *********** ************* *************** ***************** My code..

#include <iostream>
using namespace std;

//Introduction to Functions
int GetLength ();
char GetCharacter ();
void PrintChars (int m, char ch);

int main()
{
	int dLength = 0;
	char symbol = '*';
	
	dLength = GetLength();
	symbol = GetCharacter();
	PrintChars (dLength, symbol);
	
	return 0;
}

int GetLength ()
{
	int dLength = 0;
	
	cout << "Please enter the diogonal Length: ";
	cin >> dLength;
	
	return dLength;
}

char GetCharacter ()
{
	char character = '*';
	
	cout << "Enter a character for the diamond: ";
	cin >> character;
	
	return character;
}

void PrintChars (int m, char ch)
{
	for (int i = 0; i < (m+1); i++)
	{
		for (int a = 0; a < i; a++)
		{
			cout << ch;
		}
		cout << endl;
	}
}

Please point me in the right direction. Thanks!!!
Advertisement
So what exactly is the problem?

BTW, you can make the program a bit simpler by creating a new function PrintLine(ch, count) that prints a line of 'ch' characters of length 'count'. This will make the PrintChars function a bit simpler.
The problem is it print this...
*
***
*****
*******
*********
*******
*****
***
*
but first i am trying to print this..

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

thanks!
Your program does exactly what you want. Are you sure you're using the code you posted? (I noticed you edited your post and removed the while loop)
Actually my program does this..
*
**
***
****
*****
******
*******
********
*********

Not this..
*
***
*****
*******
*********
***********
*************
***************
*****************
Oh I got closer to what I want. I get this now...
*
***
*****
*******
*********
***********
*************
***************
*****************
*********

any idea how I get rid of the last row?
Thanks a lot!!

#include <iostream>using namespace std;//Introduction to Functionsint GetLength ();char GetCharacter ();void PrintChars (int m, char ch);int main(){	int dLength = 0;	char symbol = '*';		dLength = GetLength();	symbol = GetCharacter();	PrintChars (dLength, symbol);		return 0;}int GetLength (){	int dLength = 0;		cout << "Please enter the diogonal Length: ";	cin >> dLength;		return dLength;}char GetCharacter (){	char character = '*';		cout << "Enter a character for the diamond: ";	cin >> character;		return character;}void PrintChars (int m, char ch){	for (int i = 0; i < (m+1); i++)	{		for (int a = 0; a < i; a++)		{			cout << ch;		}		cout << endl;		for (int l = 0; l < i; l++)		{			cout << ch;		}	}}
Sorry, I didn't notice that the lines grow by two characters and not by one.

Notice how there's a pattern to the number of characters in a line:

line 1 has 1 character.
line 2 has 3 characters.
line 3 has 5 characters.
line 4 has 7 characters.
etc...

You can figure out the equation that gives the number of characters given a line number, and use that as the upper limit of the inner loop.
So each row needs two extra characters instead of one?
In that case try this:

void PrintChars (int m, char ch){  // iterate over [0, m) (not m + 1), you want to do this m times and you start at 0  for (int i = 0; i < m; ++i)  {    // calculate the length of the current line    int length = 1 + i * 2;    // create a string with this length    string line(length, ch);    // output the string and end the line    cout << line << endl;  }}


This way you also avoid the innerloop, which is just a bit faster and it gives nicer (easier to read) code.
First, this is probably a homework question, so you are not allowed to just post a solution. It's also more educational for the OP if he figures it out for himself.

Second, the main point of this exercise is to practice loops, and by using std::string, you're bypassing that completely.
^^^ Thanks a lot man!! but can you please tell me a little bit more about.. string line(length, ch);?
thanks!!

This topic is closed to new replies.

Advertisement