Sign in with
Sign up | Sign in
Your question

Drawing with C++

Last response: in Applications
Share
March 26, 2012 1:49:00 AM

ok so I have
ok so I have

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{


{
cout << "****";
cout << "***";
cout << "**";
cout << "*";
}
{
cout << "*";
cout << "**";
cout << "***";
cout << "****";
}
{
cout << "*";
cout << "***";
cout << "*****";
cout << "***";
cout << "*";
}


system("pause");
return 0;
}

****my goal is I amtrying to make a program thats displays 3 triangles****

*
**
***
****

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

****
***
**
*
what am I doing wrong ?

More about : drawing

a b L Programming
March 26, 2012 6:02:19 AM

You're not printing any end of line characters, so all your *s will appear on one line.
m
0
l
March 26, 2012 9:51:54 AM

Ijack said:
You're not printing any end of line characters, so all your *s will appear on one line.


+1

just add \n at end of each cout
m
0
l
Related resources
a b L Programming
March 26, 2012 11:25:51 AM

Do all beginner programming classes do the asterisk exercise? It's almost as common as Hello World it seems.
m
0
l
a b L Programming
March 26, 2012 12:05:18 PM

\n isn't the proper way to do itin C++.
m
0
l
March 27, 2012 8:10:37 PM

ok re wrote got some info to use loops insread but in trying to recreate the backwards or inverted triangle it is just an infinite loop :(  any help on this?

#include <iostream>
using namespace std;
int main()
{
int no_lines, // height of triangle.
width; // width of rectangle.
short choice;
// ADDITIONAL VARIABLES

// ADD A do-while LOOP TO THE PROGRAM SO THAT THE USER CAN REPEATEDLY
// DISPLAY THE MENU, MAKE A CHOICE, AND HAVE THE APPROPRIATE STEPS FOR
// THAT CHOICE CARRIED OUT.
// THE LOOP SHOULD CONTINUE ITERATING UNTIL THE USER ENTERS 3 FOR THE MENU CHOICE.
// HAVE THE PROGRAM PRINT SEVERAL BLANK LINES AFTER EACH SHAPE IS DRAWN.
do
{
// Display menu.
cout << "THIS PROGRAM DRAWS TRIANGLES.\n" << endl;

cout << "1. Filled Triangle" << endl;
cout << "2. Filled Inverted Trianlge" << endl;
cout << "3. Exit\n" << endl;
cin >> choice;
// ASK, READ AND VALIDATE CHOICE WITH while LOOP.

// Process choice.
switch (choice)
{
case 1: // Filled Triangle
// Ask, read and validate number of lines.
cout << "Enter the height for the filled triangle (2 - 25): ";
cin >> no_lines;
while (no_lines < 2 || no_lines > 25)
{
cout << "Number of lines has to be between 2 and 25.\n";
cout << "Enter the height for the filled triangle (2 - 25): ";
cin >> no_lines;
}
cout << endl;
// Draw shape.
// An example of a filled triangle with height 5
// *
// **
// ***
// ****
// *****
int i, j;
for (i = 1; i <= no_lines; i++)
{
for (j = 1; j <= i; j++)
cout << "*";
cout << endl;
}
break;
case 2: // Inverted filled triangle
cout << "Enter the height for the triangle (2 - 25): ";
cin >> no_lines;
while (no_lines < 2 || no_lines > 25)
{
cout << "Number of lines has to be between 2 and 25.\n";
cout << "Enter the height for the triangle (2 - 25): ";
cin >> no_lines;
}
cout << endl;
// ASK, READ AND VALIDATE HEIGHT.
// DRAW INVERTED TRIANGLE
// AN EXAMPLE OF AN INVERTED TRIANGLE OF HEIGHT 5
// *****
// ****
// ***
// **
// *
for (j = 1; j <= no_lines; j--)
{
for (i = 1; i <= j; i--)
cout << "*";
cout << endl;
}

break;
case 3:
cout << "Exiting ...\n";
return 0;
break;
}
} while (choice >= 1 && choice <= 3);
system("pause");
return 0;
}


m
0
l
!