• Advertisement

Archived

This topic is now archived and is closed to further replies.

Argh. Problem number 2-4 in chapter 2 in Accelerated C++.

This topic is 5103 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

Ok, so, the problem is the question asks me to make the program presented in the chapter to print all of the spaces (the padding in between the Hello, [name]! and the border) in one expression instead of one at a time. I've tried for an hour now and can't get it for the life of me. Here's the source:
#include <iostream>
#include <cstring>
#include <cstdlib>

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

int main()
{
    // ask for the person's name.

    cout << "Enter your first name: ";
    
    // read the name.

    string name;
    cin >> name;
    
    // user defined blanks surrounding the greeting.

    cout << "Enter the amount of padding you would like: ";
    int pad;
    cin >> pad;
    
    // hold the message that we intend to write.

    string greeting = "Hello, " + name + "!";
    
    // the number of rows and columns to write.

    const int rows = pad * 2 + 3;
    const string::size_type cols = greeting.size() + pad * 2 + 2;
    
    // 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) {
            
            // is it time to write the greeting?

            if(c == pad + 1 && r == pad + 1) {
                cout << greeting;
                c += greeting.size();
            } else {
                
                // are we on the border?

                if (c == 0 || r == rows - 1 ||
                    r == 0 || c == cols - 1)
                    cout << "*";
                else
                    cout << " ";
                ++c;
            }
        }
        
        cout << endl;
    }
    
    system ("PAUSE");
    return 0;
}

[edited by - Drevay on March 7, 2004 4:46:38 PM]

Share this post


Link to post
Share on other sites
Advertisement
Yes, I've already done that, succeeded as long as the pad was 1, and 1 only. But as soon as it gets bigger, it screwed up.

Here's a sample output if pad was two: clickay.

Anyway, help is much appreciated.

EDIT - Ignore the "^- where pad is 1" comment.




[edited by - Drevay on March 7, 2004 5:03:46 PM]

Share this post


Link to post
Share on other sites
Actually, it's this according to somebody on #gamedev (and it works fine) - this is just the main part of it:

std::cout<< std::string(greeting.size() + padlen *2 + 2, '*')<< std::endl
<<"*" + std::string(greeting.size() + padlen * 2, ' ') + "*"<< std::endl
<<"*" + std::string(padlen, ' ')< << "*" + std::string(greeting.size() + padlen * 2, ' ') + "*"<< std::endl
<< std::string(greeting.size() + padlen *2 + 2, '*')<< std::endl;

I was confused since I thought I had to use a loop of some sort since it was in the chapter that introduced loops.

So happy. ^_^




[edited by - Drevay on March 7, 2004 5:18:39 PM]

Share this post


Link to post
Share on other sites

  • Advertisement