I'm trying to build a triangle for a program but I can't seem to get it built the way I want. I feel that I have part of my code right, but something definitely needs to be added. Here is my code:
I'm having quite a difficult time trying to interpret the code you've just given, especially since you said you haven't tried it yourself. I've looked into nested for loops but can't seem to figure them out.
Look for different types of loops, I am not very familiar with them, but I don't think For is the correct loop to use. Look at Do-Whiles, Whiles, and such... but that's just my inexperienced suggestion.
Thank you for all the help guys, but I still can't seem to figure it out. I either output nothing or garbage. I think I'm ready for a solid answer on how to do this.
#include <iostream>
#include <iomanip>
usingnamespace std;
//Declare your variables.
int i; //Loop iterator.
char stick='|'; //Stick character
int rows; //Number of rows.
int main()
{
cout << "Enter Rows: ";
cin >> rows;
for (i=1; i<=rows; i++) //Start rows.
{
cout << i; //Print row number.
//Print a field of spaces equal to the number of rows minus half the width of the triangle, plus 2 spaces
//between the row number and the triangle,
cout << setfill (' ') << setw(rows-i+2) << " ";
//Print a field of pipes equal to the number of row number*2 then subtract 1 to keep the width odd and centered.
cout << setfill ('|') << setw((i*2-1)) <<"|";
cout << "\n\n"; //Print carriage return.
}
return 0;
}
Thanks for your help there HooklessFastener but I found out today that using only for loops would make my project way harder, so a friend suggested arrays.
I've gotten this so far but I've hit a wall and have been trying to get it to display. (Arrays were never really my strong point) Can anyone shed a little light onto the situation?