Nikola
Nikola

Reputation: 53

Printing triangle with given letter in C++

I would like to to print a triangle with a given letter. For example, if I input D, the program should return:

     A
     AB
     ABC
     ABCD

So far, I have managed to print all letters until the given one in my example, but as you see this method is not quite effective since I need to do this for all 26 cases since the English alphabet is 26 chars. Is there some way to optimize my code?

#include <iostream>

using namespace std;

int main() {
    char i;
    cout << "Enter char ";
    cin >> i;
    int c = static_cast<int>(i);
    if (65 < c) {
        cout << "A";
        cout << endl;
    }
    if (66 < c) {
        cout << "AB";
        cout << endl;
    }
    if (67 < c) {
        cout << "ABC";
        cout << endl;
    }

    for (int i = 64; i < c; i++) {
        cout << static_cast<char>(i + 1);
    }

    return 0;
}

Upvotes: 3

Views: 1579

Answers (4)

Mykola
Mykola

Reputation: 3363

We must run the loop from position is above 'A' character until we reached the charanter you enter

    // procead until reached input letter
    while (chNew != c)
    {
             // go to next letter
        chNew++;

             // start with 'A' until current char + 1
        for (int j = 'A'; j < chNew + 1; j++)
            cout << (char)j;
             // go to next line
        cout << endl;
    }

in each loop we increment character value by 1 to go to the next value

// go to next letter
chNew++;

inner loop simply print the character from A to next value relative to current chNew + 1, it is because we also want to include current character to our printed line.

Here is your working code.

#include <iostream>

using namespace std;

int main() 
{
    char i;
    cout << "Enter char ";
    cin >> i;
    int c = static_cast<int>(i);

    // start with 'A' - 1 character
    char chNew = 'A' - 1;

    // procead until reached input letter
    while (chNew != c)
    {
             // go to next letter
        chNew++;

             // start with 'A' until current char + 1
        for (int j = 'A'; j < chNew + 1; j++)
            cout << (char)j;
             // go to next line
        cout << endl;
    }

    // we have done 
    return 0;
}

Upvotes: 0

Zehryo Karham
Zehryo Karham

Reputation: 95

You definitely need to work on your comprehension of loops. This one works just fine and it even has some checks on what is typed in and it eventually converts lower case letters into upper casse.

char first = 'A';
char last = 0;

cout << "Enter a char: ";
cin >> last;
fflush(stdin);
cout << "\n\n";

if ((last > 96) && (last < 123)) //97 to 122 are lower case letters
{
    last -= 32; //32 is the delta between each lower case letter and its upper case "twin"
}

if ((last > 64) && (last < 91))
{
    for (char i = 65; i <= last; i++)
    {
        for (char j = 65; j <= i; j++)
        {
            cout << j;
        }
        cout << "\n";
    }
}
else
{
    cout << "\nWrong character!!\n\n";
    return 0;
}

Upvotes: 3

selbie
selbie

Reputation: 104559

Don't harcode ascii integer values into code. Explicitly use the character or string literals (e.g. 'A' instead of 65)

Start with a helper function to print exactly one line

// prints all the characters of the alphabetic sequence from "A" to the final char designated by <c>
void printTriangleLine(char c)
{
    if ((c < 'A') || (c > 'Z'))
    {
        return;
    }

    for (char x = 'A'; x <= c; x++)
    {
        cout << x;
    }

    cout << endl;
}

Then put it all together in your main:

int main()
{
    char i;
    cout << "Enter char ";
    cin >> i;

    if ((i < 'A') || (i > 'Z'))
    {
        return 0;
    }

    for (char x = 'A'; x <= i; x++)
    {
        printTriangleLine(x);
    }
    return 0;
}

Upvotes: 3

slightlynybbled
slightlynybbled

Reputation: 2645

Use a nested loop structure. Use the outer loop to 'walk' down your triangle,

lineLength = 1;
while(lineLength <= (c - 64)){
    ...stuff...

    lineLength++;
    cout << endl;
}

Use the inner loop to 'walk' down the alphabet (you've already done most of this):

for (int i = 0; i < lineLength; i++) {
    cout << static_cast<char>(i + 65);
}

Putting it together:

lineLength = 1;
while(lineLength <= (c - 64)){
    for (int i = 0; i < lineLength; i++) {
        cout << static_cast<char>(i + 65);
    }

    lineLength++;
    cout << endl;
}

I see that someone else has posted a similar answer. Between these two answers, you should be able to find your way. I haven't compiled and run this code, but I believe that it should work or be very close.

Upvotes: 3

Related Questions