Frame program trouble

Started by
-1 comments, last by Raxis 15 years, 4 months ago
I'm learning from a book recommended to me called Accelerated C++. The 2nd chapter is about making a framing program. I figured most of it out but one of questions that the author uses at the back of the chapter is: "The framing program writes out mainly black lines that separate the borders from the greeting one character at a time. Change the program so that it writes all the spaces needed in a single output expression." I don't quite understand how to do that. I'm probably just tired but help would be nice. Code:

#include <iostream>
#include <string>


using std::cin;         using std::endl;
using std::cout;        using std::string;


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

	
	string name;
	cin >> name;


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

	
	const int pad = 1;

	
	const int rows = pad * 2 + 3;
	const string::size_type cols = greeting.size() + pad * 2 + 2;
			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;
	}

	return 0;
}



This topic is closed to new replies.

Advertisement