Discussion:
C++ Patterns Program
(too old to reply)
Student Project
2024-08-26 18:45:28 UTC
Permalink
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;
}

<*******************************************>
Paavo Helde
2024-08-28 09:37:14 UTC
Permalink
Post by Student Project
<*******************************************>
#include <iostream>
#include <string>
There are some minor issues with this program.
Post by Student Project
using namespace std;
Better not to get used to this bad habit.
Post by Student Project
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.
Post by Student Project
    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.
Post by Student Project
    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 ;-)
Post by Student Project
        }
        cout << c;
        for (int i = position - 1; i >= 0; i--)
        {
            cout << letters.at(i);
        }
        cout << "\n";
        position++;
    }
    return 0;
}
<*******************************************>
Michael S
2024-08-28 12:59:11 UTC
Permalink
On Wed, 28 Aug 2024 12:37:14 +0300
Post by Paavo Helde
Post by Student Project
<*******************************************>
#include <iostream>
#include <string>
There are some minor issues with this program.
Post by Student Project
using namespace std;
Better not to get used to this bad habit.
<snip>
Post by Paavo Helde
#include <iostream>
Better not to get used to this bad habit.
The correct way to write it is:

#include <cstdio>

Loading...