Thread: Need some help..

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    2

    Question Need some help..

    Well, i have just bought Accelerated c++.
    atm im at chapter 2(not that it matters )
    I have just rewritten a program and i have gotten this code:
    Code:
    #include <iostream>
    #include <string>
    
    // say waht standard library names we will use
    using std::cin;		using std::endl;
    using std::cout;	using std::string;
    
    int main ()
    { 
    	// ask for the persons name
    	cout << "Please enter your first name: ";
    
    	// read the name
    	string name;
    	cin >> name;
    
    	// build the message we intend to write
    	const string greeting = "Hello, " + name + "!";
    
    	// ask for how many lines from the greeting to the top and the bottom.
    	cout << "How many lines do you want from the greeting to the frame?: ";
    	
    	// read the lines
    	string::size_type lol;
    	cin >> lol;
    
    	const int pad = lol;
    
    	// ask for how many spaces from greeting left to right
    	cout << "How many spaces between the greeting and the frame?: ";
    	
    	//read the spaces
    	string::size_type omg;
    	cin >> omg;
    
    	
    	const int cols = greeting.size() + omg * 2 + 2;
    
    	// the number of rows and columns to write
    	const int rows = pad * 2 + 3;
    	
    
    	//write a blank line to seperate the output from the input
    	cout << endl;
    
    	string::size_type c = 0;
    
    	//write a blank line to seperate the output from the input
    	cout << endl;
    
    	// write rows rows of output
    	// invariant: we have written r rows so far.
    	for (int r = 0; r != rows; ++r) {
    
    		string::size_type c = 0;
    
    			// invariant: we have written c characters so far in the current row.
    			while (c!= cols) {
    				
    				//it is time to write the greeting?
    				if (r == pad + 1 && c == pad + 1) {
    					cout << greeting;
    					c += greeting.size();
    				} else {
    					
    					// are we on the border?
    					if (r == 0 || r == rows - 1 || c == 0 || c == cols -1)
    						cout << "*";
    					else 
    						cout << " ";
    					++c;
    				}
    			}
    
    			cout << endl;
    	}
    	return 0;
    }
    Then this exercise came up:
    The framing program writes the mostly blank lines 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.

    Im not sure what I should do right now.
    I need some explanation..

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    I think you went a bit overkill on the comments.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Code:
    cout << " ";
    That code writes out a single space. Instead of writing a single space, you should add code to figure out how many spaces need to be written and write them all at once.

    For example, if r = 1, then you are on the second line. The second line is made up of a single '*' then cols - 2 spaces, then another '*'. So when the loop reaches r = 1 and c = 1, instead of outputting a single space, you can output cols-2 spaces all at once. You then have to update c appropriately, since ++c would increment it once instead of the number of spaces output.

    Try adding that scenario, and when you get it to work, do something similar for each row that is padding, and for the spaces before and after the greeting.

    >> I think you went a bit overkill on the comments.
    Not really, at least for a learning exercise. No need to change them.

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    2
    u dont say? :P ill think ill have to edit it a bit....

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    I think you went a bit overkill on the comments.
    Much-much, better to overdo it, than to underdo it! do it! Most beginners (most programmers, actually) don't use enough comments. When you're reading someone else's code it's really nice if everything is explained, like your're reading an example out of a textbook.

    The idea is to explain what you are doing, but there is no need to explain the obvious.

    When I write a program, I usually write the comments first... an outline of the program in informal psudo-code. Then, as I write the actual code, I edit the comments to make them more "comment-like"... That is, I'll make them say what the program is doing, instead of what the program is supposed to do. Sometimes there's more detail in the final comments, sometimes less.

    Code:
    // ask for the persons name
    	cout << "Please enter your first name: ";
    With my approach, I'd remove that comment after the line of code was written and tested. If there is more than one "person" I might use a comment to explain (or remind) who the person is.

Popular pages Recent additions subscribe to a feed