I'm running into this error that I can't figure out. The program runs and outputs everything correctly, but then I get this at the end:
terminate called after throwing an instance of 'std::out_of_range;
what(): basic_string::replace: __pos (which is 21) > this ->size(). Aborted (core dumped)
I understand that the issue is with the size going out of bounds on the line with the replace statement. But I'm looking at it, and I'm just not seeing why it's acting this way. Can anyone shed some light?
//Triangle upper right
//This program draws a triangle in a square
//12-4-19
//Sources:
#include <iostream>
#include <string>
usingnamespace std;
constint MAX = 30;
void drawTriangle(string triangle, string star, int size, int &indent);
int main()
{
string star = "*";
string triangle;
int size;
int indent = 0;
cout << "This program accepts a square size from the user " << endl;
cout << "and draws a triangle in the upper right corner " << endl;
cout << "of the square." << endl;
cout << endl;
cout << "Enter what size you would like the square to be (less than 30): ";
cin >> size;
while(size > 30 || size < 1)
{
cout << "Out of range. Try again: ";
cin >> size;
}
drawTriangle(triangle, star, size, indent);
}
void drawTriangle(string triangle, string star, int size, int &indent)
{
int i = 0;
int j = 0;
int rows = 0;
for(rows = 0; rows < size; rows++)
{
for (i = 0; i < size; i ++)
{
triangle.insert(0, size, ' ');
triangle.replace(indent, 1, size - indent, '*');
indent++;
cout << triangle;
triangle.clear();
cout << endl;
}
}
}