| | |
loop problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2004
Posts: 7
Reputation:
Solved Threads: 0
:rolleyes: i have to write a program which ask the user the height of a pyramid between 1 and 15 and displays a pyramid of this height made up of "*" characters on screen.for example
how high would you like the pyramid?:37
pick another height(must be between 1 and 15):6
**
****
******
********
**********
************
i have written the following but its displaying something like that
**
**
**
**.........
it continues like that for the number of * that is suppose to made the pyramid
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
void main()
{
clrscr();
char ch;
int h,i;
cout<<"how high would you like the pyramid?";
cin>>h;
ch='*';
if(h>15)
cout<<"pick another height between 1 and 15"<<endl;
cin>>h;
if((h>=1)&&(h<15))
for(i=1;i<=h;i++)
{
for(int a=1;a<=i;a++)
{ cout<<ch<<ch<<endl; }
}
getch();
}
how high would you like the pyramid?:37
pick another height(must be between 1 and 15):6
**
****
******
********
**********
************
i have written the following but its displaying something like that
**
**
**
**.........
it continues like that for the number of * that is suppose to made the pyramid
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
void main()
{
clrscr();
char ch;
int h,i;
cout<<"how high would you like the pyramid?";
cin>>h;
ch='*';
if(h>15)
cout<<"pick another height between 1 and 15"<<endl;
cin>>h;
if((h>=1)&&(h<15))
for(i=1;i<=h;i++)
{
for(int a=1;a<=i;a++)
{ cout<<ch<<ch<<endl; }
}
getch();
}
>#include<iostream.h>
No, not C++.
>#include<iomanip.h>
Also not C++.
>#include<conio.h>
Not only is this not C++, it's also only available on a limited number of implementations.
>void main()
This never has been, and hopefully never will be, correct C++. main returns int. In fact, the old argument of being a lazy ******* ("void main means I don't have to use 'return 0;' at the end of main") doesn't apply anymore because main returns 0 implicitly. So doing things the right way actually saves you a keystroke!
>clrscr();
If getch is the patron saint of nonportable functions, clrscr is it's god. Not only is this functionality rarely needed in valid situations, calling it at the start of a program is almost never a valid situation.
>getch();
The patron saint of nonportable functions, but for this use you can replace it with cin.get() and have a similar effect. Sure, you require the user to hit enter instead of any key, but what do they usually hit anyway? :rolleyes:
On to the meat of the problem, unless you know of a clever way to solve the problem (I do, and I'll post it because there's no way you can turn it in as your work), you need three loops. The first loop handles the rows, the second loop is nested inside the first loop and handles leading whitespace, and the third loop comes after the second loop and prints the requisite number of stars:
Here is the nifty way to do it with a single loop:
If, on the off chance that the problem is to print a right triangle rather than an equilateral triangle, the problem is even simpler. Just remove the space printing step.
No, not C++.
>#include<iomanip.h>
Also not C++.
>#include<conio.h>
Not only is this not C++, it's also only available on a limited number of implementations.
>void main()
This never has been, and hopefully never will be, correct C++. main returns int. In fact, the old argument of being a lazy ******* ("void main means I don't have to use 'return 0;' at the end of main") doesn't apply anymore because main returns 0 implicitly. So doing things the right way actually saves you a keystroke!

>clrscr();
If getch is the patron saint of nonportable functions, clrscr is it's god. Not only is this functionality rarely needed in valid situations, calling it at the start of a program is almost never a valid situation.
>getch();
The patron saint of nonportable functions, but for this use you can replace it with cin.get() and have a similar effect. Sure, you require the user to hit enter instead of any key, but what do they usually hit anyway? :rolleyes:
On to the meat of the problem, unless you know of a clever way to solve the problem (I do, and I'll post it because there's no way you can turn it in as your work), you need three loops. The first loop handles the rows, the second loop is nested inside the first loop and handles leading whitespace, and the third loop comes after the second loop and prints the requisite number of stars:
C++ Syntax (Toggle Plain Text)
for rows to 1 do for 1 to spaces do print space loop for 1 to stars do print star loop loop
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> using namespace std; int main() { int rows; cout<<"Enter the pyramid height: "; if ( cin>> rows ) { for ( int i = 0; i < rows; i++ ) { cout<< setw ( rows - i ) << setfill ( ' ' ) <<""; cout<< setw ( i * 2 + 1 ) << setfill ( '*' ) <<""<<endl; } } }
I'm here to prove you wrong.
![]() |
Similar Threads
- perl loop problem (from a beginner) (Perl)
- Infinite loop problem need help plz (PHP)
- For loop problem (Java)
- Strange Excel infinite-loop problem (Windows Software)
- a for loop problem (Java)
- Alright this loop problem is bugging me (Java)
- I think Loop problem (Java)
Other Threads in the C++ Forum
- Previous Thread: writing a class without the class declaration?
- Next Thread: Write sentence, then return # of vowels - Need help, plz.
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






