Last visit was: It is currently Thu Mar 28, 2024 10:24 am


All times are UTC-05:00




Post new topic Reply to topic  [6 posts ] 
    Author Message
     Post subject:ASCII Art gallery
    PostPosted:Sun Sep 09, 2007 8:27 pm 
    User avatar
     

    Joined:Sun Feb 12, 2006 8:56 pm
    Posts:1019
    Website:http://eddieringle.com
    Location:Detroit, MI
    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?

    _________________
    -- Eddie Ringle

    Check out Elysian Shadows and consider backing us on Kickstarter!

    ====================================

    Image


    Top
    Offline  
     Post subject:
    PostPosted:Mon Sep 10, 2007 8:11 am 
    Sagely Amphibian
    User avatar
     

    Joined:Sun Jun 18, 2006 3:06 pm
    Posts:69
    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 ;)


    Top
    Offline  
     Post subject:
    PostPosted:Mon Sep 10, 2007 3:37 pm 
    User avatar
     

    Joined:Sun Feb 12, 2006 8:56 pm
    Posts:1019
    Website:http://eddieringle.com
    Location:Detroit, MI
    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]

    _________________
    -- Eddie Ringle

    Check out Elysian Shadows and consider backing us on Kickstarter!

    ====================================

    Image


    Top
    Offline  
     Post subject:
    PostPosted:Mon Sep 10, 2007 6:35 pm 
    Sagely Amphibian
    User avatar
     

    Joined:Sun Jun 18, 2006 3:06 pm
    Posts:69
    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())?


    Top
    Offline  
     Post subject:
    PostPosted:Mon Sep 10, 2007 7:29 pm 
    User avatar
     

    Joined:Sun Feb 12, 2006 8:56 pm
    Posts:1019
    Website:http://eddieringle.com
    Location:Detroit, MI
    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.

    _________________
    -- Eddie Ringle

    Check out Elysian Shadows and consider backing us on Kickstarter!

    ====================================

    Image


    Top
    Offline  
     Post subject:
    PostPosted:Mon Sep 10, 2007 7:41 pm 
    Sagely Amphibian
    User avatar
     

    Joined:Sun Jun 18, 2006 3:06 pm
    Posts:69
    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(); }


    Top
    Offline  
    Display posts from previous: Sort by 
    Post new topic Reply to topic

      All times are UTC-05:00


      Who is online

      Users browsing this forum: No registered users and 13 guests


      You cannot post new topics in this forum
      You cannot reply to topics in this forum
      You cannot edit your posts in this forum
      You cannot delete your posts in this forum
      You cannot post attachments in this forum

      Search for:
      Jump to:  
      cron
      Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
      Theme created by Miah with assistance from hyprnova