extend

if we want use different characters, we need input from user. but how we want to set user just input number, and we show it like that :

e.g : user insert number 7

#
@#
@#@
#@#@
#@#@#
@#@#@#
@#@#@#@

this is how my code :


#include <iostream>
using namespace std;

int main()
{
int size, width = 0;;
bool start = true;

cout<<" Size of triangle : ";
cin>>size;
cout<<endl;

for (int a = 1; a <= size; a++)
{
for (int b =1; b <= width; b++)
{
if(start == true)
{
cout<<" "<<"#"<<" ";
bool start = false;
}
else
{
cout<<" "<<"@"<<" ";
}
cout<<endl;
}
}
system("PAUSE");
return 0;
}


but that not give me what i want -_-
Last edited on
p/s : i had work for this problem for 12 hours yesterday and feel fedup -_-
Hi...
Can you give us your example expected program output that you should have?
And, what is your problem?
the expected output that i need are :

if user insert number 7 :

#
@#
@#@
#@#@
#@#@#
@#@#@#
@#@#@#@

if user input number 2 :

#
@#

if user input number 10 :

#
@#
@#@
#@#@
#@#@#
@#@#@#
@#@#@#@
#@#@#@#@
#@#@#@#@#
@#@#@#@#@#

if user input number 20 :

#
@#
@#@
#@#@
#@#@#
@#@#@#
@#@#@#@
#@#@#@#@
#@#@#@#@#
@#@#@#@#@#
@#@#@#@#@#@
#@#@#@#@#@#@
#@#@#@#@#@#@#
@#@#@#@#@#@#@#
@#@#@#@#@#@#@#@
#@#@#@#@#@#@#@#@
#@#@#@#@#@#@#@#@#
@#@#@#@#@#@#@#@#@#
@#@#@#@#@#@#@#@#@#@
#@#@#@#@#@#@#@#@#@#@


=="
There are a few problems with your code right now.

1
2
3
4
5
if(start == true)
{
    cout<<" "<<"#"<<" ";
    bool start = false;
}


Here you are creating a completely new variable called start instead of changing the start variable above, which isn't what you want to do as this is in local scope and nothing else will see it, just change it to start = false

You are using the width variable but that is never assigned any value except 0, meaning your loop will never run right now, you want the inner loop to be based on 'a' in this case. Here is your code with a few changes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
int size, width = 0;
bool start = true;

cout<<" Size of triangle : ";
cin>>size;
cout<<endl;

for (int a = 1; a <= size; a++)
{
	for(int b = 1; b <= a; b++) // Goes up to whatever value a happens to  be at this point
	{
		if(start == true)
		{
			cout<< " " << "#" << " ";
			start = false;
		}

		else
		{
			cout<<" "<<"@"<<" ";
			start = true; // Don't forget to change this back to true, which you forgot in your code
		}
	}

	cout<<endl;
}


This could be optimized to not use the extra variable and only show certain symbols when the number is even/odd, but you should get the idea.
the width = 0 use to give spacing between the characters?
Simply print width space character(s) between them.
Last edited on
owkeyh. thank ya. i get it what i wrong:
1) in second for looping to get @#
2) bool condition

truly, i hate looping =="
by the way, i can understand a little bit :)
i had extend the coding to while looping.
that what i get :


Size of triangle : 3


#
@ #
@ # @


Size of triangle : 3


#
@ #
@ # @


Size of triangle : 3


#
@ #
@ # @


Size of triangle : 4


#
@ #
@ # @
# @ # @


Size of triangle : 3


#
@ #
@ # @


Size of triangle : 4


#
@ #
@ # @
# @ # @


Size of triangle : 2


#
@ #


Size of triangle : 3


@
# @
# @ #


Size of triangle : 2


@
# @


Size of triangle : 3


#
@ #
@ # @

my question :

where i should place reset value for variable a and b?
to print as :

Size of triangle : 3


#
@ #
@ # @


Size of triangle : 3


#
@ #
@ # @


Size of triangle : 3


#
@ #
@ # @


Size of triangle : 4


#
@ #
@ # @
# @ # @


Size of triangle : 3


#
@ #
@ # @


Size of triangle : 4


#
@ #
@ # @
# @ # @


Size of triangle : 2


#
@ #


Size of triangle : 3


#
@ #
@ # @


Size of triangle : 2


#
@ #


Size of triangle : 3


#
@ #
@ # @
Topic archived. No new replies allowed.