I do not understand what I am doing wrong... I need the second triangle to print the character the user inputs, but instead - and I have no clue as to why it is doing this - it prints the second triangle with smiley faces and other symbols.

Any help would be appreciated, or an explaination to where I am making this happen so I can figure it out and get it to print the users character in the second triangle.

#include "stdafx.h"
#include <iostream>

using namespace std;

void printTriangle(int);
void drawBar(int);
void drawBar(int, char);


int main()
{

int base(0);
char sign(' ');
cout << "This program draws a Triangle" << endl;

cout << "\nEnter the triangle base size: ";
cin >> base;

cout << "\nEnter a character: ";
cin >> sign;

printTriangle(base);


}

void printTriangle(int bs)
{
	for(int i = 0; i <= bs; i++)
	{
	drawBar(i);
	cout << endl; 
	}

	for(int i = 0; i <= bs; i++)
	{
	drawBar(i,i);
	cout << endl; 
	} 

	cout << endl;
	system("PAUSE");
}
void drawBar(int b)
{
	for(int i = 0; i < b; i++)
	{
	cout << "*";
	}
}

void drawBar(int b, char ch)
{
	for(int i = 0; i < b; i++)
	{
	cout << ch;
	}
}

Yes sorry.
The first triangles output is based on the users input of the size of the triangle.
Example:
If the user enters 4, the output would be:

*
**
***
****

If the user enters 6, the output would be:

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

and so on based on the user input of the size of the triangle.

Now the second triangle is similar.

In the program the user is asked 2 questions - The triangle base size (like above) & the character to use.

Example:

User enters 4 and ^ (for the character), the output of the second triangle is:

^
^^
^^^
^^^^

User enters 3 and #, the output would be:

#
##
###
_______________________________________________

The program needs to print out 2 triangles, like this:

The user enters 5 and ^

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

^
^^
^^^
^^^^
^^^^^

The first triangle always prints with *, the second triangle prints with whatever the user enters as a character.

But here is my output with the code I have done:

________________________________________________

This program draws a Triangle

Enter the triangle base size: 5

Enter a character: ^

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


☻☻
♥♥♥
♦♦♦♦
♣♣♣♣♣

Press any key to continue . . .
______________________________________

Of course in a cmd window the second triangle is proportioned.
Does that make sense? I hope so, I am new to c++, so I hope I am explaining right.

I do not understand why is it printing the goofy characters for the second triangle, instead of ^ like entered by the user.

Well, first you should begin with the most important thing: drawing a triangle which only consists out of '*', don't care about the '#' or '^' yet, you can quickly adapt it later on, to accept other characters as well :)

To give you a little start:
(in the next examples I'll call the number the user enters the number of iterations)
If the user enters for example 3 then your triangle will look like:

[B]/* number of iterations is 3 */[/B]
*          // iteration 1 ( loop indexes )
**          // iteration 2
***          // iteration 3

And if the user enters 4 then your triangle would look like:

[B]/* number of iterations is 4 */[/B]
*          // iteration 1 ( loop indexes )
**          // iteration 2
***          // iteration 3
****          // iteration 4

So what can you derive from that?
You'll have to run two loops: one which is simply looping <number_of_iterations> times and one which each time prints a certain number of asterisks on the screen (and that number is equal to the current loop index)

Comments
Nice, subtly explained post!

That I did do... that was how this whole thing started, with a triangle of astericks to print. Which the program does print an astericks triangle as it should. My problem is with the second triangle, it does not print what the user inputs, and I am unsure why. If you run the code below, you will see the output of the second triangle is wrong. It does not use the users input, it prints random symbols. But the 2 triangle do print, just the second one is printing wrong.
I just don't know where the problem in the code is:

#include "stdafx.h"
#include <iostream>

using namespace std;

void printTriangle(int);
void drawBar(int);
void drawBar(int, char);


int main()
{

int base(0);
char sign(' ');
cout << "This program draws a Triangle" << endl;

cout << "\nEnter the triangle base size: ";
cin >> base;

cout << "\nEnter a character: ";
cin >> sign;

printTriangle(base);


}

void printTriangle(int bs)
{
	for(int i = 0; i <= bs; i++)
	{
	drawBar(i);
	cout << endl; 
	}

	for(int i = 0; i <= bs; i++)
	{
	drawBar(i,i);
	cout << endl; 
	} 

	cout << endl;
	system("PAUSE");
}
void drawBar(int b)
{
	for(int i = 0; i < b; i++)
	{
	cout << "*";
	}
}

void drawBar(int b, char ch)
{
	for(int i = 0; i < b; i++)
	{
	cout << ch;
	}
}

Dave did already mention what the problem is with your code:

You'll have to let your function know about the character you want to print instead of doing this:

drawBar(i,i);

You should read more carefully!

Yes I read it, but I just want to figure out why I am getting symbols for the second triangle instead of the user output... Are you saying that is why?
I am new to c++, so pardon my noobiness... =)

Yes I read it, but I just want to figure out why I am getting symbols for the second triangle instead of the user output... Are you saying that is why?
I am new to c++, so pardon my noobiness... =)

Actually you could treat a char variable as a small integer, each character has it's own 'integer code', which is more commonly referred to as ASCII code, what you do is the following: you just pass the loop's index as an ASCII code which will be implicitly converted by the compiler to it's equivalent character, and that will give the unexpected output :)

Wow... ok, nevermind then... I guess this is the wrong forum to ask for help. I did read what Dave wrote, but again, I am new to c++ and I have tried changing it, which then causes an error. So I don't know what to change it too.

I will figure it out and I do read carefully, sorry for trying to ask for help. I guess you forgot what it was like to be starting out in programming.

Thanks anyways.

Take a look at this code and see what I've changed:

#include <iostream>

using namespace std;

void printTriangle(int, [B]char[/B]);
void drawBar(int);
void drawBar(int, [B]char[/B]);


int main()
{

int base(0);
char sign(' ');
cout << "This program draws a Triangle" << endl;

cout << "\nEnter the triangle base size: ";
cin >> base;

cout << "\nEnter a character: ";
cin >> sign;

printTriangle(base, [B]sign[/B]);

[B]cin.get();[/B] // replace system("pause");
}

void printTriangle(int bs, [B]char sign[/B])
{
	for(int i = 0; i <= bs; i++)
	{
	drawBar(i);
	cout << endl; 
	}

	for(int i = 0; i <= bs; i++)
	{
	drawBar(i,[B]sign[/B]);
	cout << endl; 
	} 

	cout << endl;
}
void drawBar(int b)
{
	for(int i = 0; i < b; i++)
	{
	cout << "*";
	}
}

void drawBar(int b, [B]char ch[/B])
{
	for(int i = 0; i < b; i++)
	{
	cout << ch;
	}
}

:P

Awesome, thank you very much... I did try to put (i,sign) which was throwing me an error, but I was not adding char sign to the printTriangle function, which is what caused the error.

I kept the system("PAUSE") because when I put get.cin(); it just pops open a box and closes instantaniously. I must be doing something wrong with it...

Thank you, I do appreciate it, I am really trying to figure this stuff out. So I am sure this willl not be my last post. =)

This article has been dead for over six months. Start a new discussion instead.