• C++ Patterns Program

    From Student Project@21:1/5 to All on Mon Aug 26 18:45:28 2024
    XPost: alt.comp.lang.c++.misc

    This is worth a try to run it in Linux, Windows and MacOS:

    <*******************************************>

    #include <iostream>
    #include <string>

    using namespace std;

    int main()
    {
        string letters;

        cout << "Enter a string to create a pyramid pattern: ";

        // cin >> letters;

        getline(cin, letters);

        int len = letters.length();
        int position = 0;

        for (char c : letters)
        {
            int spaces = len - (position + 1);

            while (spaces > 0)
            {
                cout << " ";
                --spaces;
            }

            for (int i = 0; i < position; i++)
            {
                cout << letters.at(i);
            }
            cout << c;

            for (int i = position - 1; i >= 0; i--)
            {
                cout << letters.at(i);
            }
            cout << "\n";

            position++;
        }

        return 0;
    }

    <*******************************************>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paavo Helde@21:1/5 to Student Project on Wed Aug 28 12:37:14 2024
    XPost: alt.comp.lang.c++.misc

    On 26.08.2024 21:45, Student Project wrote:
    This is worth a try to run it in Linux, Windows and MacOS:

    <*******************************************>

    #include <iostream>
    #include <string>


    There are some minor issues with this program.

    using namespace std;

    Better not to get used to this bad habit.


    int main()
    {
        string letters;

        cout << "Enter a string to create a pyramid pattern: ";

        // cin >> letters;

        getline(cin, letters);

    Error handling is missing. This call may fail for various reasons and
    failure should be checked.


        int len = letters.length();
        int position = 0;

    In real code the same type should be used for such variables as is used
    by the interface functions (size_t instead of int, in this case), even
    though it will make writing the later backward loop a bit more
    cumbersome. Mixing types of different signedness and potentially of
    different size is just another bad habit which should be avoided.


        for (char c : letters)
        {
            int spaces = len - (position + 1);

            while (spaces > 0)
            {
                cout << " ";
                --spaces;
            }

            for (int i = 0; i < position; i++)
            {
                cout << letters.at(i);

    The only reason to use at() instead of [] is to get a well-defined
    exception if your program is buggy. Alas, exceptions are not caught nor
    handled by this program, which makes it arguably more buggy, not less ;-)

            }
            cout << c;

            for (int i = position - 1; i >= 0; i--)
            {
                cout << letters.at(i);
            }
            cout << "\n";

            position++;
        }

        return 0;
    }

    <*******************************************>



    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Michael S@21:1/5 to Paavo Helde on Wed Aug 28 15:59:11 2024
    XPost: alt.comp.lang.c++.misc

    On Wed, 28 Aug 2024 12:37:14 +0300
    Paavo Helde <[email protected]> wrote:

    On 26.08.2024 21:45, Student Project wrote:
    This is worth a try to run it in Linux, Windows and MacOS:

    <*******************************************>

    #include <iostream>
    #include <string>


    There are some minor issues with this program.

    using namespace std;

    Better not to get used to this bad habit.


    <snip>

    I'd start a little earlier:

    #include <iostream>

    Better not to get used to this bad habit.
    The correct way to write it is:

    #include <cstdio>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)