I'm having trouble because when i try to compile i get this comment : error: ‘else’ without a previous ‘if’
I think its something related to the { } but not so sure now.... any help appreciated
Pair up every opening { with it's closing }. You will find where is the one misplaced. Also, until you're not fully comfortable with the syntax, don't omit the {} (as you do at the if statement on line 20).
will try and let you know, but first need to read a bit more on what and how the { } should be used cause i think i misunderstood something.....
anyway thanks
int main()
{
cout << "Please enter your name : " << endl;
string name;
cin >> name;
const string greeting = "Hello, " +name+ " !";
constint pad = 1;
constint rows = pad * 2 + 3;
const string::size_type cols = greeting.size() + pad * 2 + 2;
cout << endl;
for (int r = 0; r != rows; ++r)
{
string::size_type c = 0;
while (c != cols)
{
if (r == pad +1 && c == pad +1)
cout << greeting;
c += greeting.size();
}
else
{
if (r == 0 || r == rows -1 || c == 0 || c == cols -1)
cout << "*";
else
cout << " ";
++c;
}
}
cout << endl;
} // This is not proper usage, you have not opened all that you try to close.
return 0;
}
The proper usage of brackets with if and else is as follows:
1 2 3 4 5 6 7 8
if (expression)
{
// execute your statements here
}
else
{
// execute your statements here
}
Of course, the spacing is free to change to your liking.