Quote:
7) What could also be done is replace all the _hard coded_ output of H in int arthi() and instead generalize it with an array char [][] and use a for to output it. Once this first step is done you could generalize it again for all characters.
Just for fun, here is my suggestion 7) implemented.
EDIT: replaced
char* by
string.
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
bool artDraw(string word);
void arthinew();
void pressKey();
static const int rowsPerLetter = 5;
static bool implementedUpperLetters[26] = {0,0,0,0,0,0,0,1}; // 'H' is implemented
static bool implementedLowerLetters[26] = {0,0,0,0,0,0,0,0,1}; // 'i' is implemented
// 26 letters, 5 rows per letter.
// For a given letter each rows must be of the same length
static string upperLetters[26][rowsPerLetter] = {
// A, B, C, D, E, F, G
{}, {}, {}, {}, {}, {}, {},
// H
{"|| ||",
"|| ||",
"||=====||",
"|| ||",
"|| ||"}
};
static string lowerLetters[26][rowsPerLetter] = {
// a, b, c, d, e, f, g, h
{}, {}, {}, {}, {}, {}, {}, {},
// i
{"<>",
" ",
"||",
"||",
"||"}
};
int main ()
{
int nMenuSel=0;
while (nMenuSel !=3)
{
cout << "Welcome to ASCII Art v1.1\n";
cout << " 1. Art: Hi (no file io)\n 2. Art: Hi (w/ file io)\n 3. Exit\nPlease input the number of your choice: ";
cin.clear();
cin >> nMenuSel;
cout << "\n";
switch(nMenuSel)
{
case 1:
artDraw("Hi");
pressKey();
break;
case 2:
arthinew();
case 3:
break;
default:
cout << "\nYou did not enter a menu option.\n\n";
break;
}
}
return 0;
}
void pressKey()
{
char n;
cout << "Press any key to continue.\n" << endl;
cin.clear();
cin.ignore(1000, '\n');
cin.get(n);
}
bool artDraw(string word)
{
bool allLettersDrawn = true;
unsigned int lenWord = static_cast<unsigned int>(word.length());
for(unsigned int row = 0; row < rowsPerLetter; row++)
{
int currentGoodLetter = 0;
for(unsigned int i = 0; i < lenWord; i++)
{
char c = word[i];
if('A' <= c && c <= 'Z' && implementedUpperLetters[c - 'A'])
{
if(currentGoodLetter > 0)
cout << " ";
currentGoodLetter++;
cout << upperLetters[c - 'A'][row];
}
else if('a' <= c && c <= 'z' && implementedLowerLetters[c - 'a'])
{
if(currentGoodLetter > 0)
cout << " ";
currentGoodLetter++;
cout << lowerLetters[c - 'a'][row];
}
else if(c == ' ')
{
if(currentGoodLetter > 0)
cout << " ";
currentGoodLetter++;
cout << " ";
}
else
{
allLettersDrawn = false;
}
}
cout << endl;
}
return allLettersDrawn;
}
void arthinew()
{
char str[2000];
fstream piece("arthi.txt",ios::in);
while(!piece.fail() && !piece.eof())
{
piece.getline(str,2000);
cout << str << endl;
}
piece.close();
}