| Ferrous Moon http://www.ferrousmoon.com:80/forums/ |
|
| ASCII Art gallery http://www.ferrousmoon.com:80/forums/viewtopic.php?f=45&t=978 |
Page 1 of 1 |
| Author: | eddieringle [Sun Sep 09, 2007 8:27 pm ] |
| Post subject: | ASCII Art gallery |
Okay, a small project I am working on, basically a console ascii art gallery written in C++. Here's the code: asciihello.cpp: Code: #include <iostream>
#include <conio>
#include "Globals.h"
using namespace std;
int main ()
{
int nMenuSel;
menu:
cout << "Welcome to ASCII Art v1\n";
cout << " 1. Art: Hi\n 2. Exit\nPlease input the number of your choice: ";
cin >> nMenuSel;
cout << "\n";
switch(nMenuSel)
{
case 1:
arthi();
break;
case 2:
break;
default:
cout << "\nYou did not enter a menu option.\n";
goto menu;
break;
}
return 0;
}
int arthi()
{
cout << " ## ## <>" << endl;
cout << " ## ##" << endl;
cout << " ######### ||" << endl;
cout << " ## ## ||" << endl;
cout << " ## ## ||" << endl;
cout << "\n Press any key to continue.\n\n" << endl;
getch();
main();
return 0;
}
Globals.h:
Code: #ifndef GLOBALS_H
#define GLOBALS_H
int arthi();
#endif
I wrote this with DevCpp Portable. As I was reading other sites I heard that conio.h may not be portable. I have linux so I can test it myself, but if I could get other people's opinions it would help. By the way as you can see their is only one ACSII art piece, if anyone wants to post another piece so I can add it I would appreciate it. Credit will be given to the poster as well. EDIT: conio.h wouldn't work under linux, so no pausing after the art piece is displayed. Any ideas how to implement this feature for linux? |
|
| Author: | frenchfrog [Mon Sep 10, 2007 8:11 am ] |
| Post subject: | |
1) Don't use goto menu;, put your _whole_ main in a while. 2) Use getchar() (part of stdio.h) instead of getch(), or if you want to do it in C++ try to something like char n; cin.get(n); (you may need to flush cin before trying to read from it, cin.clear();cin.ignore(1000, '\n');)? The output result won't be as nice a getch() but it will be portable (pressing enter won't work?). 3) Globals.h could be removed completely just put the function prototype int arthi(); at the beginning of your main file. Also the convention is to name a file all in lower cap (should be globals.h). 4) int arthi() function shouldn't recall the main();. This is bad logic and will finish by crashing your program with a stack overflow. 5) int arthi() should probably be void arthi() since it doesn't return anything useful. 6) In int arthi(), the whole cout << "\n Press any key to continue.\n\n" << endl; and _getch_ stuff should be made in a separate function. This way it makes the code cleaner and you only have one place to modify the _getch_ code. 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. EDIT: Once all of the above are done, you could _easily_ make a program which output a word in ASCII Art |
|
| Author: | eddieringle [Mon Sep 10, 2007 3:37 pm ] |
| Post subject: | |
I implemented all of frenchfrog's improvements except 7. I want to use file i/o. Revised code: asciihello.cpp: Code: #include <iostream>
#include <fstream>
#include <stdio>
using namespace std;
void arthi();
void arthinew();
void pressKey();
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:
arthi();
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);
}
void arthi()
{
cout << " || || <>" << endl;
cout << " || ||" << endl;
cout << " ||=====|| ||" << endl;
cout << " || || ||" << endl;
cout << " || || ||" << endl;
pressKey();
}
void arthinew()
{
char str[2000];
fstream piece("arthi.txt",ios::in);
while(!piece.eof())
{
piece.getline(str,2000);
cout << str;
} piece.close();
}
arthi.txt:
Code: || || <>
|| ||
||=====|| ||
|| || ||
|| || ||
(arthi.txt looks much better in the text editor)piece.getline(str,2000); outputs the txt file fine, just its all on one line... anyway to fix this?[/i] |
|
| Author: | frenchfrog [Mon Sep 10, 2007 6:35 pm ] |
| Post subject: | |
Quote: piece.getline(str,2000); outputs the txt file fine, just its all on one line... anyway to fix this?
Do cout << str << endl;?1) You should probably call pressKey(); in the main instead of in void arthi(). It's better to split _user interface_ logic and program functionality logic. Code: case 1:
arthi();
pressKey();
break;
2) It seems you can remove #include <stdio>3) Verify for file IO errors while(!piece.fail() && !piece.eof())? |
|
| Author: | eddieringle [Mon Sep 10, 2007 7:29 pm ] |
| Post subject: | |
Okay, thanks frenchfrog, everything works fine now. If anyone would like to post some ASCII Art I would appreciate it. Like I said before, all credit will go to the author and poster. |
|
| Author: | frenchfrog [Mon Sep 10, 2007 7:41 pm ] |
| Post subject: | |
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();
}
|
|
| Page 1 of 1 | All times are UTC-05:00 |
| Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |
|