Jump to content
  • Advertisement
Sign in to follow this  
Raxis

Frame program trouble

This topic is 3665 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
}



Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!