Jump to content
  • Advertisement
Sign in to follow this  
musafir2007

Please help with little C++ program...

This topic is 3772 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!!!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
The problem is it print this...
*
***
*****
*******
*********
*******
*****
***
*
but first i am trying to print this..

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

thanks!

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
Actually my program does this..
*
**
***
****
*****
******
*******
********
*********

Not this..
*
***
*****
*******
*********
***********
*************
***************
*****************

Share this post


Link to post
Share on other sites
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 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;
for (int l = 0; l < i; l++)
{
cout << ch;
}
}
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!