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.

Recommended Answers

All 6 Replies

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";
}

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++;
	}

}

>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.

int x=1;
    while(x<7){
            int y=0;
            while(y<x){
            cout<<"*";
            y++;
        }
    cout<<endl;
    x++;
    }

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

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

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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.