Search code examples
c++accelerated-c++

Accelerated C++ Exercise 2.4


I've been following Accelerated C++ for a couple of weeks now but I have been stuck at exercise 2.4 for a while and finally I thought I found it out but after trying giving it different dimensions I found out that it doesn't really work and I don't really understand why

The code initially prints a framed message, in this particular exercises I was supposed to change how the code prints the blanks from one character at a time into writing all the planks at once

here is the code:

// [2-0, 2-4] Exercises
#include<iostream>
#include<string>

// saying what standard-library names we use
using std::cout;        using std::endl;
using std::cin;         using std::string;

int main()
{
    // asking for the name
    cout << "Please enter your first name: ";

    // reading the name
    string name;
    cin >> name;

    // building the message that we intend to write
    const string greeting = "Hello, " + name + "!";

    //  2.2 & 2.3 asking for inpadY
    cout << "Please enter the number of padY (Vertical padding): ";

    // 2.2 & 2.3 reading the inpadY
    int inpadY;
    cin >> inpadY;

    //  2.2 & 2.3 asking for inpadX
    cout << "Please enter the number of padX (Horizontal padding): ";

    // 2.2 & 2.3 reading the inpadX
    int inpadX;
    cin >> inpadX;

    // the number of planks surrounding the greeting
    // 2.2 & 2.3 added inpadY as the number of planks;
    const int padY = inpadY;

    // 2.2 & 2.3 added inpadX
    const int padX = inpadX;

    // 2.4 pad size
    const int pad = inpadX + inpadY;

    // the number of rows and columns to write
    const int rows = padY * 2 + 3;
    const string::size_type cols = greeting.size() + padX * 2 + 2;

    // 2.4 creating a padding string left and right and top and bottom
    const string LeftRightPad(padY, ' ');
    const string TopBottomPad(cols - 2, ' ');

    // write a blank line to separate the output and 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 (r == padY + 1 && c == padX + 1)
            {
                cout << greeting;
                c += greeting.size();
            } else {

                // are we on the border?
                if (r == 0 || r == rows - 1 ||
                    c == 0 || c == cols - 1)
                    {cout << "*";
                    ++c;}
                else
                    // 2.4 typing out the spaces at once
                    {cout << LeftRightPad;
                    c += LeftRightPad.size();}
            }
        }

        cout << endl;
    }

    return 0;
}

Edited to have the input and output

Please enter your first name: Estrogen
Please enter the number of padY (Vertical padding): 2
Please enter the number of padX (Horizontal padding): 2

**********************
*                    *
*                    *
*  Hello, Estrogen!  *
*                    *
*                    *
**********************

Process returned 0 (0x0)   execution time : 3.281 s
Press any key to continue.
Please enter your first name: Estrogen
Please enter the number of padY (Vertical padding): 2
Please enter the number of padX (Horizontal padding): 5

****************************
*                          *
*                          *
*                          *
*                          *
*                          *
****************************

Process returned 0 (0x0)   execution time : 5.098 s
Press any key to continue.
Please enter your first name: Estrogen
Please enter the number of padY (Vertical padding): 3
Please enter the number of padX (Horizontal padding): 2

**********************
*
*
*
*
*
*
*
**********************

Process returned 0 (0x0)   execution time : 4.333 s
Press any key to continue.

Update: I've rewritten the code and the output is an infinite loop of asterisks here is the new code

#include<iostream>
#include<string>

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

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

   string name;
   cin >> name;

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

   cout << "Enter the length: ";

   int length;
   cin >> length;

   cout << "Enter the height: ";

   int height;
   cin >> height;

   const int rows = height * 2 + 3;
   const string::size_type cols = message.size() + length * 2 + 2;

   const string TopBottom(cols, '*');
   const string Blank(cols - 2, ' ');
   const string messageblank(cols - 3 - message.size(), ' ');

   cout << endl;


    for (int r = 0; r != rows; ++r) {

        string::size_type c = 0;
        while (c != cols) {

                if ( r == height + 1 && c == length + 1)
                {
                    cout << messageblank << message << messageblank;
                    c += Blank.size();
                } else
                if (r == 0 && c == 0 || r == rows - 1 && c == cols -1)
                {
                    cout << TopBottom;
                    c += TopBottom.size();

                } else
                if ( r != 0 && c == 0 || r != rows -1 && c == cols - 1)
                {
                    cout << "*";
                    ++c;
                } else
                    cout << Blank;
                    c += Blank.size();
        }

    cout << endl;
    }


    return 0;
}

Thank you guys for help in advance


Solution

  • ok so it took me 3 days but I finally figured it out here is the working code

    #include<iostream>
    #include<string>
    
    using std::string;      using std::endl;
    using std::cout;        using std::cin;
    
    int main()
    {
       cout << "Please enter your first name: ";
    
       string name;
       cin >> name;
    
       cout << "Enter the length: ";
    
       int length;
       cin >> length;
    
       cout << "Enter the height: ";
    
       int height;
       cin >> height;
    
       const string message = "Hello, " + name + "!";
    
       const int rows = height * 2 + 3;
       const string::size_type cols = message.size() + length * 2 + 2;
    
       const string TopBottom(cols, '*');
       const string Blank(cols - 2, ' ');
       const string messageblank(length, ' ');
    
       cout << endl;
    
       for (int r = 0; r != rows; ++r) {
    
            string::size_type c = 0;
            while (c != cols) {
    
                    if ( r == height + 1 && c == 0)
                    {
                        cout << "*" << messageblank << message << messageblank << "*";
                        c += TopBottom.size();
    
                    } else
                    if (r == 0 && c == 0 || r == rows - 1 && c == 0)
                    {
                        cout << TopBottom;
                        c += TopBottom.size();
    
                    } else
                    if ( c == 0 && r != 0 || c == 0 && r != rows - 1)
                    {
                        cout << "*" << Blank << "*";
                        c += TopBottom.size();
    
                    }
            }
    
    
        cout << endl;
        }
    
        return 0;
    }