0

I am trying to get my program to print letters instead of numbers. I used char c = static_cast<char>(N); to attempt to do this but it wont work, instead it prints character images that are not (a-z) How can I get the numbers to be printed as letters?

#include  <cstdlib>
#include  <iostream>
using namespace std;

// Function getUserInput obtains an integer input value from the user.
//  This function performs no error checking of user input.
int getUserInput()
{
    int N(0);

    cout << endl << "Please enter a positive, odd integer value, between (1-51): ";
    cin >> N;
    if (N < 1 || N > 51 || N % 2 == 0)
    {
        cout << "Error value is invalid!" << "\n";
        cout << endl << "Please enter a positive, odd integer value, between (1-51): ";
        cin >> N;
        system("cls");
    }

    cout << endl;
    return N;
} // end getUserInput function

//  Function printDiamond prints a diamond comprised of N rows of asterisks.
//  This function assumes that N is a positive, odd integer.
void printHourglass(int N)
{
    char c = static_cast<char>(N);
    for (int row = (N / 2); row >= 1; row--)
    {
        for (int spaceCount = 1; spaceCount <= (N / 2 + 1 - row); spaceCount++)
            cout << ' ';
        for (int column = 1; column <= (2 * row - 1); column++)
            cout << c;
        cout << endl;
    } // end for loop
    // print top ~half of the diamond ...
    for (int row = 1; row <= (N / 2 + 1); row++)
    {
        for (int spaceCount = 1; spaceCount <= (N / 2 + 1 - row); spaceCount++)
            cout << ' ';
        for (int column = 1; column <= (2 * row - 1); column++)
            cout << c;
        cout << endl;
    } // end for loop

    // print bottom ~half of the diamond ...


    return;
} // end printDiamond function

int main()
{
    int N = 1;

    while (N == 1)
    {
        printHourglass(getUserInput());
        cout << endl;
        cout << "Would you like to print another hourglass? ( 1 = Yes, 0 = No ):";
        cin >> N;
    }
} // end main function
David G
  • 94,763
  • 41
  • 167
  • 253

2 Answers2

0
  1. C functions, itoa
  2. C++, using stringstream
  3. boost::lexical_cast

Actually for your case, you can directly print it out. cout << N

Zac Wrangler
  • 1,445
  • 9
  • 8
0

The letters are not numbered with A starting at 1 or anything like that. You're likely on an ASCII/UTF-8 system. So, in printHourglass, replace cout << N with

cout << static_cast<char>('A' + count - 1);
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313