Write a function IsoTriangle() that takes a size parameter and dispays an isoceles triangle with that many lines. Use the DrawBar() function as a basis. (Hint: Use another function Spaces() to display the leading spaces before the start of the asteriks on each line.) For example, the function call IsoTriangle(4) displays:

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

P.S.<The diagram above should look like a pyramid.>

Please Help me with a basic code. I tried typing one of my own and it came up with 72 errors =) Some fo the errors I didn't know of.....Like Unautorized use of binary operators? and Telling me to end lines at the beginning of line.

Recommended Answers

All 4 Replies

Piece 'O cake:

#include <cstdlib>
#include <iomanip>
#include <iostream>

using namespace std;

int main()
{
  int n;

  cout<<"Height: ";
  if (!(cin>> n)) {
    cerr<<"Invalid input"<<endl;
    return EXIT_FAILURE;
  }

  for (int i = 0; i < n; i++) {
    cout<< setw(n - i - 1) << setfill('-') <<""
        << setw(i * 2 + 1) << setfill('*') <<""<<endl;
  }

  return EXIT_SUCCESS;
}

But that will surely get you a failing grade for cheating. Maybe if you posted the code you've already written we could help you with it. Lots of errors doesn't necessarily mean lots of problems. Usually a single problem will cascade down and cause more than one error message.

Piece 'O cake:

#include <cstdlib>
#include <iomanip>
#include <iostream>

using namespace std;

int main()
{
  int n;

  cout<<"Height: ";
  if (!(cin>> n)) {
    cerr<<"Invalid input"<<endl;
    return EXIT_FAILURE;
  }

  for (int i = 0; i < n; i++) {
    cout<< setw(n - i - 1) << setfill('-') <<""
        << setw(i * 2 + 1) << setfill('*') <<""<<endl;
  }

  return EXIT_SUCCESS;
}

But that will surely get you a failing grade for cheating. Maybe if you posted the code you've already written we could help you with it. Lots of errors doesn't necessarily mean lots of problems. Usually a single problem will cascade down and cause more than one error message.

I have used your code as a guidline and I thank you for it!

now I have a question ...I know this sounds dumb but what does return EXIT_FAILURE; is that some kinda of windows thing like system cls?

Also what does cerr<<"Invalid input"<<endl;

the cerr do?

>I know this sounds dumb but what does return EXIT_FAILURE;
C++ allows you to return values from main. This way, the calling program can use that return value as an exit code. The only explicit value that standard C++ allows is 0, for successful termination. It also allows for two macros in cstdlib: EXIT_SUCCESS, which is equivalent to 0; and EXIT_FAILURE, which is implementation defined, but always means failure. Assuming EXIT_FAILURE is defined as 1

return EXIT_FAILURE;

is the same thing as saying

return 1;

Of course, using any value other than 0 or one of the two macros is nonportable. Though technically you can return any integral value for the calling process to use (in a system dependent way).

>Also what does cerr<<"Invalid input"<<endl
You would have the same effect if you replaced cerr with cout. cerr is just another output stream designed for effective error output if cout is redirected (such as to a log file). The only difference is that cerr isn't buffered by default and cout is. It's equivalent to C's stdout and stderr streams.

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.