-
Please Help!!
The program gives gives me this output:
* * * * *
* * * *
* * *
* *
*
Can anyone tell me PLEASE how to, instead, get the blank spaces first instead at the end. In other words, the second line would have 1 blank space followed by 4 stars, third line would have 2 blank spaces followed by 3 stars, and so on. So that the vertical line (with 5 stars) appears at the end rather than at the beginning.
I will really appreciate it a lot. Thanks a million in advance.
Arooj
void main()
{
clrscr();
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= i; j--)
cout << " *" ;
cout << endl;
}
getch();
}
-
a) Don't use void main(), use int main()
b) Don't use getch(), use cin.get()
c) Don't use Depricated headers like <iostream.h>
Code:
#include <iostream>
int main()
{
for (int i = 5; i >= 1; --i)
for (int j = i; j > 0; --j)
std::cout << '*' << std::endl;
return 0;
}
Edit: He edited his post, it's now much different than the one I replied to.
-
This is what u want..
and yes, cin.get() does not work as getch() does.
Code:
#include<iostream.h>
int main()
{
int i,j;
for(i=1;i<=10;i++)
{
for(j=1;j<=i;j++)
cout<<" ";
for(j=1;j<=(10-i);j++)
cout<<"*";
cout<<endl;
}
return 0;
}
-
Thanks both of you very much. However, after posting the question I tried very hard and came up with this which also worked. But your codes were better.
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int length;
cout << "Enter length: ";
cin >> length;
cout << endl;
for (int i = 1; i <= length; i++) {
for (int k = 2; k <= i; k++ )
cout << " ";
for (int j = length; j >= i; j--)//{
cout << " *" ;
cout << endl;
}
getch();
}
-
use code tags while posting code, man. neway glad it worked.