New to C++ programming. I am supposed to write a program that will ask the user if they want to print an ascending or a descending triangle. Based on user input, print the triangle on the screen. Only one cout statment may be used and it has to read: cout << '*'. :confused: << '*'.

Program should be a nested while loop. Suggestions.

Re: Program to display ascending or descending triangle 80 80

New to C++ programming. Program should be a nested while loop. Suggestions.

Resolved part of my programing. Will work on it. In case others need to know.

for(int i = 1; i < 7; i++)
{
for(int a = 0; a < i; a++)
{
cout << "*";
}
cout << "\n";
}

Re: Program to display ascending or descending triangle 80 80

Just change the for loops into while loops!

#include<iostream.h>


void main()
{
	int i=1;
	while(i<7){
			int a=0;
			while(a<i){
			cout<<"*";
			a++;
		}
	cout<<"\n";
	i++;
	}

}
Re: Program to display ascending or descending triangle 80 80

>Just change the for loops into while loops!
And this change is supposed to solve what, exactly? It makes no difference what kind of loop it is as long as they do the same thing, which they do. In this case a for loop is the better choice anyway because it shows the intention of the code more clearly.

>#include<iostream.h>
This is not a standard C++ header. I highly recommend using iostream and qualifying standard library names with std:: (one way or another) if you want your code to be correct. If your compiler doesn't allow that then get a newer compiler. Standard C++ offers many advantages over pre-standard C++.

>void main()
main has never returned void and probably never will. This invokes undefined behavior according to the C++ standard. If your compiler accepts it and you really want to, you can use it in your own programs, but please refrain from posting such drivel around here and trying to palm it off as good code.

Re: Program to display ascending or descending triangle 80 80
int x=1;
    while(x<7){
            int y=0;
            while(y<x){
            cout<<"*";
            y++;
        }
    cout<<endl;
    x++;
    }
Re: Program to display ascending or descending triangle 80 80

>#include<iostream.h>
This is not a standard C++ header.

As long it works why do u want to change it?

Re: Program to display ascending or descending triangle 80 80

As long it works why do u want to change it?

Because it is not guaranteed to work in the future.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.