Hi, I am just starting to learn C++ and am working through Accelerated C++. I wrote the following code to practice creating strings. I keep getting error messages on the first two string constructions (in the two if statements). The last one (in the else statement) works fine. My question is, is it because the variables are type int and not type size_t? Or is the problem something else?
I know I could just use nested for loops, but I'd love to know why this method doesn't work.
int main()
{
// ask user for desired shape
cout << "Do you want to draw a SQUARE, a RECTANGLE, or a TRIANGLE?";
// process user input of shape name
string shape; // define shape
cin >> shape; // read shape
// draw square
if (shape = "SQUARE")
{
cout << "How tall/wide should the square be?"; // ask user for value
int base; // define base
cin >> base; // read value
// draw square
for (int i = 0; i < (base - 1); ++i)
{
const string row(base,'*');
cout << row << endl;
}
}
if (shape = "RECTANGLE")
{
cout << "What is the base of the rectangle?";
int base; // define base
cin >> base; // read value
cout << "What is the height of the rectangle?";
int height; // define height
cin >> height; // read value
const string row(base, '*'); // build a string
// draw rectangle
for (int i = 0; i < (height - 1); ++i)
{
const string row(base, '*'); // build a string
cout << row << endl; // display the string
}
}
else
{
cout << "what is the base of the triangle?";
int base; // define base
cin >> base; // read value
// draw triangle
for (int i = 0; i < (base - 1); ++i)
{
const string row(i + 1, '*'); // build a string of *'s
cout << row << endl;
}
}
// hold window open
char holdExecutionWindowOpen;
std::cin >> holdExecutionWindowOpen;
// return
return 0;
}
Also, the compiler isn't returning any errors for my if statements. Are non-nested if {} if {} else {} viable?