Name framing

Hi all!

I'm new to both this forum and to programming. I'm having problem with this name framing-program. The asterisk after the 'greeting' doesn't align with the last column. Why? The solution is probably obvious, but please do help me :)

Here's the code:

#include <iostream>
#include <string>

using namespace std;

int main()
{
cout << "Please enter your name: ";

string name;
cin >> name;

const string greeting = "Hello, " + name + "!";

int pad = 2;

int rows = pad * 2 + 3;

string::size_type columns = greeting.size() + pad * 2 + 2;

cout << endl;

for (int r = 0; r != rows; ++r)
{
string::size_type c = 0;

while (c != columns)
{
if (r == 0 || r == rows - 1 || c == 0 || c == columns - 1)
{
cout << "*";
}

else if (r == pad + 1 && c == pad + 1)
{
cout << greeting;
c += greeting.size();
}

else
{
cout << " ";
}
++c;
}
cout << endl;
}

cin.get();
cout << "\n\n Press ENTER to continue...";
cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

return 0;
}
The problem is that you increment c after you print out the greeting. Try putting a "continue" statement after the c += greeting.size() statement.
Thanks for the reply kooth. Using a continue statement worked wonders!
You're most welcome, Kakashi333!
Topic archived. No new replies allowed.