quick question how will i go about setting setting up characters to draw a shape such as a triangle. but the user must pick an odd # that equals to the shape .

the one i have is simple but idk how to input the number the user choosen to draw a shape. which is a right triangle or upside right triangle or rectangle or plus sign.

char symbol;
int number;
cout << "draw a triangle " << endl;
cin >> symbol;
cout << " enter an odd number between 3 and 9 to draw shape " << endl;
cin >> number;   

i know this is not right

    if (number = 3 || 5 || 7 || 9)
        {
            cout << "you have choosen " << number << endl;
        }
    }

this is the basic one but my proffessor doesnt want it like this the user must choose a number and then the program will execute the shape.

char symbol;
int number;
cout << "draw a triangle " << endl;
cin >> symbol;
cout << " enter an odd number between 3 and 9 to draw shape " << endl;
cin >> number;
cout << symbol << symbol << symbol << endl;
cout << symbol << symbol << endl;
cout << symbol << endl;
return 0;

if you can point me in the right direction that will be great

Recommended Answers

All 40 Replies

You need to refine your question before we go all out w/ a bunch of code to give you what may or may not be the correct answer.

You unclear in some aspects of your question; for example, you state that you require an 'odd number' input from the user, but then later give 'rectangle' and 'plus sign' as possible shapes that could be derived from user input (which have even number sides).

Additionally, when the user enters an odd number, does this input correspond to the number of sides of the shape, or the number of characters needed per side to create a triangle?

sorry , basically im trying to create this shape: a triangle. the user must make up there own char. (it can be watever) and then type any odd number bw 3-9. so here the user TYPED 3.

so it will show up like this if the user choose the char. W

w
ww
www __ that is 3 horizantal and 3 verticle. so any odd numbe bw

3-9 will draw the same thing like 5. wit da char. $

$
$$
$$$
$$$$
$$$$$ and so forth with

<<<< this is if the user picks 3

char symbol;                                       
cin >> symbol << endl;
cout << symbol << endl;
cout << symbol << symbol << endl;
cout << symbol << symbol << symbol << endl

;


i get this on my screen

$$$$$$$$
$$
$ WHY IS THAT???

after the user picks the number and the shape how can i make that into a loop and and make the characters how i want them to be which is what these codes are in the begining of this thread

You are attempting to implement the answer using a linear method. Try using a nested loop and I think you'll have the results ye' be looking for:

int lines = 0;
char c = '\0';

//Prompt for user input
do{
     cout << "\nEnter an odd number of lines of triangle to be drawn: ";
     cin >> lines;

     //Test user input
     if(! lines%2)
     {
          cout << "\n\aNumber entered is even.  Please enter an odd number.";
     }
}while(! lines%2);

cout << "\nEnter chararcter to be used to draw triangle: ";
cin >> c;

//Draw ye' precious triangle
for(int i=0; i<lines; i++)
{
     for(int j=0; j<i; j++)
     {
          cout << c;
     }
     
     cout << endl;
}
commented: Arg matey, Cap'n Portis is back +2

clinton portis :: did you get a whole bunch of errors when you ran it ? maybe im doing something wrong idk. but thanks for ur reply

it is possible. i did not include my usual disclaimer: "The code provided is uncompiled, untested and may contain simple, easy to fix errors." The theory is sound. I did most of the thinking for you. Please make at least some sort of attempt to resolve any simple syntax errors. Any effort on your part = good.

this is what im trying to do:

im trying to write a menudriven program that allows the user to select from a list of options. when an option is selected the program must complete the selected task and re display the menu. the options are
draw a triangle, upsidedown triangle, rectangle, or plus sign . the user must choose an odd number 3 - 9 to do this. the user can choose watever character they want to make these shapes. the shapes are too look like my previouse thread.

what is a way to write this program so when a user chooses a different shape other than a triangle it will make that shape. cuz the way i have it is only for triangle and i use alot of cout statements to draw the triangle. and i know it has to be a better way to go about making shapes without using a whole lot of cout statements .

example:
this makes a triangle like the one i want to draw:
when you compile it ?

char symbol;                                       
cin >> symbol << endl;                            
cout << symbol << endl;
cout << symbol << symbol << endl;
cout << symbol << symbol << symbol << endl

basically this : if the user were to choose to draw a rectangle and instead of width of it being 3 like this triangle above they want the width to be 9. the way im doing it will take forever and take up a lot of space. IS THERE A BETTER WAY TO DO IT?

i hope you get what im trying to say it is hard 2 explain

Learn how to program with the opengl or allegro libraries. It's alot better than this.

IS THERE A BETTER WAY TO DO IT?

Look at lines #20 through #28 in my previous post for a better way to do this.

Yes, you will have to learn to use nested loops, which I believe is the goal of your assignment.

As for the overall design of your assignment, the important pieces of your program could look like this:

display_menu();

cout << "Enter shape to draw: ";
cin >> shape_selection;

cout << "Enter character to use to draw shape: ";
cin >> char_selection;

cout << "Enter odd number of lines to draw: ";
cin >> lines;

switch(shape_selection)
{
     case 1:  draw_tri(char_selection, lines);
     break;
     case 2:  draw_upsidedown_tri(char_selection, lines);
     break;
     case 3:  draw_rectange(char_selection, lines);
     break;
     case 4:  draw_plus_sign(char_selection, lines);
     break;
     default:  menu_error();
}

//Function definitions
void draw_tri(int char_selection, int lines)
{
     //Use a nested loop to draw a triangle, line at a time, top to bottom
     //Copy and paste the triangle algorithm from my previous post
     //No thinking required.
}

void draw_upsidedown_tri(char_selection, lines)
{
     //Use a nested loop to draw an upside-down triangle
     //Refer to my previous post for a triangle drawing algorithm
     //Alter the loop conditions to draw the triangle upside-down.
}

void draw_rectangle(char_selection, lines)
{
     //draw top of rectangle
     //Using a loop, draw left side, bunch of spaces, then draw right side.  Newline each loop iteration.
     //draw bottom of rectangle
}

void draw_plus_sign(char_selection, lines)
{
     //use a loop to draw vertical top half of plus, newline each loop iteration.
     //draw the horizontal row of the plus.  
     //use a loop to draw the vertical lower half of the plus, newline each loop iteration.
}

You have a spoon fed outline of your program, so all you have to do now is fill in the blanks. Give it a try, come on back and show us what ye' got.

in lines 12- 23 i like the way you did that. i was wondering if i could do somethig like that. WHat does the case mean? i got another question can you put an if statement in a switch statement.

the if statement will state the shape and number for width and height

example:
// numb = width and height
// shape = the shape the user wants
#include<iostream>
#include<string>
using namespace std;
int main()
int numb, shape;
char symb;

if (shape == triangle) && (numb == 3)
    {
        cout << " you have choosen " << number <<  " and shape " << symbol << endl << endl;
        cout << symbol << endl;
        cout << symbol << symbol << endl;
        cout << symbol << symbol << symbol << endl;
        break;
    }
    if (shape == triangle) && (numb == 5)
        {
            cout << "You have choosen " << number << "and shape " << symbol << endl << endl;
                    cout << symbol;
                    cout << symbol << symbol << endl;
                    cout << symbol << symbol << symbol << endl;
                    cout << symbol << symbol << symbol << symbol << endl;
                    cout << symbol << symbol << symbol << symbol << symbol << endl;
        }
        if (shape == triangle) && (numb == 7)
        {
            cout << "You have choosen " << number << "and shape " << symbol << endl << endl;
            {
                    cout << symbol;
                    cout << symbol << symbol << endl;
                    cout << symbol << symbol << symbol << endl;
                    cout << symbol << symbol << symbol << symbol << endl;
                    cout << symbol << symbol << symbol << symbol << symbol << endl;
                    cout << symbol << symbol << symbol << symbol << symbol << symbol << endl;
                    cout << symbol << symbol << symbol << symbol << symbol << symbol << symbol << endl;
            }
            if (shape == triangle) && (numb == 9)
        
            cout << "You have choosen " << number << "and shape " << symbol << endl << endl;
            {
                    cout << symbol;
                    cout << symbol << symbol << endl;
                    cout << symbol << symbol << symbol << endl;
                    cout << symbol << symbol << symbol << symbol << endl;
                    cout << symbol << symbol << symbol << symbol << symbol << endl;
                    cout << symbol << symbol << symbol << symbol << symbol << symbol << endl;
                    cout << symbol << symbol << symbol << symbol << symbol << symbol << symbol << endl;
                    cout << symbol << symbol << symbol << symbol << symbol << symbol << symbol << symbol << endl;
                    cout << symbol << symbol << symbol << symbol << symbol << symbol << symbol << symbol << symbol << endl; 
            }
        }
else (shape != triangle) && (numb == 0)
    {
        cout << "end" << endl;
    }
    return 0;
}

do i need to put a else statement after every if statement.
and i will change pretty much the if and else statements for every different shape. it will be long.

r is that why u made those cases like that?

wait wait, why are you not using nested 'for' loops to make the triangles? It will make your code much shorter/efficient.

If this was assigned by your teacher, I am sure that this is what he expects you to do.

commented: Indeed! +12

clinton portis: I dont get it? line 14, 16,18, 20 .

where am i putting those statements and what do they mean ?

idk what this means and i see that im sa post to use it but how and where. i am a beginner programmer

for(int i=0; i<lines; i++)
{
     for(int j=0; j<i; j++)
     {
          cout << c;
     }
     
     cout << endl;
}

Run and compile this little program. Go through it line by line. Proceed no further until you understand the basics of using a simple loop. Then when you are comfortable, do experimentation with changing the loop conditions and loop contents to make it do new and exciting things:

#include iostream
using namespace std;

int main()
{
     int lines = 0;
     char c = '\0';

     cout << "Enter number of lines: ";
     cin >> lines;

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

     //Learn to use simple loops
     //Don't be afraid to try new things here
     // (make it loop backwards for example)
     //Your imagination is the only limitation.
     for(int i=1; i<lines; i++)
     {
          for(int j=0; j<i; j++)
          {
               cout << c;
          }
     
          cout << endl;
     }

return 0;
}

i keep getting an error message and i dont know why. message reads

error: name lookup of `i' changed for new ISO `for' scoping
error: using obsolete binding at `i'

switch (letter)
{
    case 1:;
    cout << " You have choosen triangle with a height and width " << number << endl;
        for (triangle = 0 ; symbol = ' '; number++);
            for (int i = 0; i<number;i++);
                for (int m = 0; m<i; m++);
                    cout << symbol;
                        cout << "\n";
                            break;

I'm not sure why you are getting that.. it may be due to incorrect switch syntax.. try using another variable name other than i.

You are on the right track btw. You need to wrap your head around basic loop operations before even picking up your assignment.

Here's a freebe for ye':

//draw a square (just one way out of infinite possibilities)

//draw top
for(int i=0; i<10; i++)
{
     cout << '*';
}

//draw sides
for(int i=0; i<10; i++)
{
     cout << endl << '*' << "         " << '*';
}

//draw bottom
for(int i=0; i<10; i++)
{
     cout << '*';
}

tips:

  • don't overcomplicate your switch, keep it simple. Use functions calls or small blocks of code.
  • When using nested loops, be sure to indent and always use braces.

Good attempt at experimental code you got btw.. good learning process taking place. Sometimes you just gotta get in there and try stuff and see what happens.

Aside from all the rest of the situation you've got semicolons after your for statements. The loops definitely won't run then.

Just my $0.02, sit down away from the PC with your text and study the for loop because you're just going to frustrate yourself at this rate. I'm referring to things like for (triangle = 0 ; symbol = ' '; number++) . The second position there is evaluated as true or false and if it's false the for loop stops (hint: that will never be false).

ok i got the errors , but one more question why wont this print the triangles ?

yeah im getting the hang of it but the only thing is : im trying to fill in the empty spaces in the triangle or add one to each line as it goes down cuz now all it is
#
# - i want to fill those up so on the second line it will another #
###

here is what i got so far

switch (symbol)
{
    case 'a':
	case 'A':
    cout << " You have choosen triangle with a height and width " << number << endl;
	{
            for(int i=0; i< number; i++)
				cout << endl << '*' << endl;
				cout << "\n";	
	}
	{
		for(int i=0; i<number; i++)
			cout << '$';
			cout << endl;
	}
     break;
commented: Even though you are very new to coding, you do not ask us to 'do' the coding like so many in the past. For this you deserve some credit. +1

yeah im getting the hang of it but the only thing is : im trying to fill in the empty spaces in the triangle or add one to each line as it goes down cuz now all it is
#
# - i want to fill those up so on the second line it will another #
###

here is what i got so far

switch (symbol)
{
    case 'a':
	case 'A':
    cout << " You have choosen triangle with a height and width " << number << endl;
	{
            for(int i=0; i< number; i++)
				cout << endl << '*' << endl;
				cout << "\n";	
	}
	{
		for(int i=0; i<number; i++)
			cout << '$';
			cout << endl;
	}
     break;

As previously stated, in order to make a 'triangle' you need nested for loops. A nested for loop is simply a for loop within a for loop.

Why do you need this? Because each time that the line breaks, it must increase the amount of symbols in the line. Clinton gave you a prime example of this, and you should just copy the code and play around with it. (Not the code he posted to make a square, but the one to make a triangle') The only minor error he has in his code is that it prints one less shape for each line than it is supposed to. Try figuring out why it does this, and fixing it on your own. It would be good practice.

What you are currently doing is trying to make a triangle using a 'square' formula..you cannot do this. Remember to place the for loop inside the other for loop.

Alright, well based on what I have seen, you are doing this for your knowledge mostly. So I found a project that I did a while back, I would like you to look at it. Obviously don't copy it word-for-word, but check out the formulas..it may help you understand things a bit better.

Hope you achieve whatever it is you're looking to do. =)

#include <iostream>
using namespace std;
int main()
{
	int shape, size;
	char symbol;
	cout << "What symbol would you like to use?: ";
	cin >> symbol;
	system("cls");
	cout << "-------------------------------------\n"
		<< "Which shape would you like to create?\n"
		<< "1. A Triangle\n"
		<< "2. A Solid Square\n"
		<< "3. A Hollow Square\n"
		<< "-------------------------------------\n"
		<< ">> ";
	cin >> shape;
	system("cls");
	cout << "How Large would you like your shape to be?:";
	cin >> size;
	system("cls");
	switch(shape)
	{
	case 1:
		for(int s=0; s<size; s++)
		{
			for(int x=-1; x<s; x++)
				cout << symbol;
			cout << endl;
		}
		break;
	case 2:
		for(int s=0; s<size;s++)
		{
			for(int x=0; x<size;x++)
				cout << symbol;
			cout << endl;
		}
		break;
	case 3:
		for(int x=0; x<size; x++)
			cout << symbol;
		for(int x=0; x<size-2; x++)
		{
			cout << endl << symbol;
			for(int x=0;x<size-2;x++)
				cout << " ";
			cout << symbol;
		}
		cout << endl;
		for(int x=0; x<size; x++)
			cout << symbol;
		cout << endl;
		break;
	default:
		cout << "wrong answer, bro!\n";
	}
}
commented: A good post +5

Being able to use loops effectively is an essential and very powerful programming technique.

Use a loop anytime you want to do a bunch of the same stuff over and over.

I can execute a million instructions with just a few lines of code:

//Print all numbers from 0 to 1,000,000
for(long int i=0; i<1000001; i++)
{
     cout << i << ' ';
}

But what if I only want let's say, only 5 numbers per line....

//Outter loop
for(long int i=0; i<200000; i++)
{
     //Inner loop
     for(int j=0; j<5; i++, j++)
     {
          cout << i << ' ';
     }
     
     cout << endl;
}

You can do pretty much anything you want to do with loops.. your imagination is your only limitation. Get down with loops and you will have a much more enjoyable time as a CS student.

so this upside down triangle got me shaking my computer. is there a reverse function so i can print the opposite of A. when i look online all i see is reverse and copy backward functions for strings

this makes a triangle = A.

for(int i=0; i<number; i++)
           {
                for(int y=-1; y<i; y++)
                    cout << symbol;
                        cout << "\n";
           }
        break;

i want to make this into a upside triangle = B.

case 'b':
    case 'B':
        for(int k=0;k<number;k++)
            {
                for(int m=0;m>=k;m++)
                    cout << symbol;
                        cout << "\n";
            }
            break;
    }

Ok, so you want the second side to be at the top rather than the bottom?
****
***
**
*
(I'm assuming)
So for your second loop say number was 10, you'd want to have rows of length 10,9,8,7... (with the m=0, (ignore the stop condition it's not correct), m++ your loop would be going ___? (from what to what).

thats exactly what i want the screen to print but you just lost me after that

for(int i=0; i<10; i++)
{
     cout << i;
}

for(int i=10; i>0; i--)
{
     cout << i; 
}

thanks alot 4 all your help i got it ,all i have 2 do is the plus sign and error trap:

#include <iostream>
using namespace std;
int main()
{
int letter, number =1, triangle, upside_down_triangle, rectangle, plus_sign, answer, ;
char a,b,c,d,e, symbol = '\0';
cout << "Please choose a letter that corresponds to the shape of your choosing \n";
cout << " A. triangle " << endl;
cout << " B. upside down triangle " << endl;
cout << " C. rectangle " << endl;
cout << " D. plus sign " << endl;
cout << " E. quit " << endl;
cin >> symbol;
cout << " Please choose an odd number 3-9 to calculate width and height of your choosing\n";
cin >> number;
    {
    do
       {
        cout << " ready????? " << endl << endl;
        break;
       }
    while ((number=3)||(number=5)||(number=7)||(number=9));
    }
        {
        cout << "Check this out" << endl << endl;
        }    switch (symbol)
    {
    case 'a':
    case 'A':
            cout << " Thank you, you have choosen triangle and " << number << " for height and width" << endl;
                for(int i=0; i<number; i++)
                {
                for(int y=-1; y<i; y++)
                        cout << symbol;
                        cout << "\n";
                }
        break;    case 'b':
    case 'B':
            cout << " Thank you, you have choosen upside_down triangle and " << number << " for height and width" << endl;
                for(int k=number;k>=0;k--)
                {
                for(int m=1;m<=k;m++)
                        cout << symbol;
                        cout << "\n";
                }
        break;    case 'c':
    case 'C':
            cout << " Thank you, you have choosen rectangle and " << number << " for height and width" << endl;
                for(int i=0; i<number; i++)
                {
                for(int y=0; y<number; y++)
                        cout << symbol;
                        cout << "\n";
                }
        break;    }
return 0;
}

What I meant was that if you wanted to step by +1 for your loop that you could do 10-m so for 0,1,2,3 it would go 10,9,8,7 but CP's method is more straight to the point.

So what do you need on the code you have just posted (not sure what happened with the code tags it seems like they skipped)? For the + sign (surprise) you need the nested for loop again. Spaces can be used to your advantage, plot it out on paper. I'm not sure what kind of error trapping you are looking for.

so im trying to make this plus sign in this nested for loops. first question did i do the loop right. second question i keep getting errors saying setw cannot be used as a function. where am i going wrong i know its a small mistake

{
                        for(int i=5 ; i<width; i++)
                            {
                                for(int y=0; y>width;  y++)
                                cout << setw(width) << symbol;
                                cout << "\n";
                                {
                                    for(int m=0; m>width; m++)
                                    cout << setw(width) << symbol;
                                    cout << "\n";
                                    {
                                        for(int k=0; k>width; k++)
                                        cout << setw(width) << symbol;
                                        cout << "\n";
                                    }
                                }
                            }
                    }
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.