I think ive brained myself here, this fun ass code is supposed to output horizontal, instead its vertical, how can i rectify?!

Pages: 12
Theres a txt file too its here https://www.dropbox.com/sh/v0l2n2v1iltnezk/7YttxzW9wC

heres what that text file contains if you are a cautious type:

--AA--
AA--AA
AA--AA
AAAAAA
AA--AA
AA--AA
BBBB--
B---BB
BBBB--
B---BB
B---BB
BBBB--
--CC--
CC--CC
CC----
CC----
CC--CC
--CC--
DDDD--
D---DD
D---DD
D---DD
D---DD
DDDD--
EEEEEE
EE----
EEEEEE
EE----
EE----
EEEEEE
FFFFFF
FF----
FFFF--
FF----
FF----
FF----
--GG--
GG--GG
GG----
GG-GGG
GG--GG
--GG--


thats needed for the code to work, but if you want to use your giant brain its a load of 6 by 6 ascii art style letters that all sit on top of each other so if you were to look at it it would be 168 chars down and 6 chars long.

can you also critique my coding style? be mean its fine but if you only say mean things i will presume theres nothing good about it at all, i would try a beginners forum but i want EXPERTS :)


heres the 'blit' function on its own:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void blit ()
{
    vector <alphabet> letters;
    string sentence;
    getline (cin,sentence);
    int whole_length =6*sentence.length();
    int letter_postion=0;
    char output [whole_length][6];
    for (int b = 0; b<sentence.length(); b++)
    {
        letters.push_back(alphabet()); //each vector holds an array of a letter or space for the length of the line
        letters[b].set_arr(sentence[b]);
    }
    for (int a = 0; a<sentence.length(); a++)
    {
        for (int i = 1; i < 7; i++)
        {
            for (int j = 0; j < 6; j++)
            {
                output[i][j+letter_postion] = letters[a].ret_arr (i,j);
                cout<<output[i][j+letter_postion];
            }
            cout<<endl; //this whole block is where i have been re-organising things only to get the same result!!
        }

        letter_postion=letter_postion+6;
    }


but the rest of it is here if you want it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <string>

using namespace std;

class alphabet
{
public:

    alphabet()
    {

        ifstream letterfile ("letters.txt.txt");
        for (int q = 0; q<168; q++)
        {
            for (int z = 0; z<6; z++)
            {
                letterfile >> box[q][z];//initialize box from the txt
            }

        }

        char zeta[27]= {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' '};
        for(int a = 0; a<27; a++)//initialize alpha to the alphabet
            alpha[a]=zeta[a];
    }

    void set_arr(char omega)//omega will be used to find what ascii art letter should be stored on the objects arr
    {
        int num;
        for (int a = 0; a<27; a++)
        {
            if (alpha[a]==omega)
                num=a;//intialize num to the index of the alphabet
        }
        num=num*6;
        int temp=num+6;
        int c=0;
        for (num; num<temp; num++)
        {
            c++;
            for (int b=0; b<6; b++)
            {
                arr[c][b]=box[num][b];
            }
        }
    }

    char ret_arr (int num1, int num2)
    {
        return arr[num1][num2];
    }

private:
    char arr [6][6];
    char box [168][6];
    char alpha [27] ;

};

void blit ()
{
    vector <alphabet> letters;
    string sentence;
    getline (cin,sentence);
    int whole_length =6*sentence.length();
    int letter_postion=0;
    char output [whole_length][6];
    for (int b = 0; b<sentence.length(); b++)
    {
        letters.push_back(alphabet()); //each vector holds an array of a letter or space for the length of the line
        letters[b].set_arr(sentence[b]);
    }
    for (int a = 0; a<sentence.length(); a++)
    {
        for (int i = 1; i < 7; i++)
        {
            for (int j = 0; j < 6; j++)
            {
                output[i][j+letter_postion] = letters[a].ret_arr (i,j);
                cout<<output[i][j+letter_postion];
            }
            cout<<endl; //this whole block is where i have been re-organising things only to get the same result!!
        }

        letter_postion=letter_postion+6;
    }

}

int main ()
{
    blit();

}


And when i modified it like the following it started beeping!!HOW DO I MAKE THINGS BEEP!? THIS IS TRULY AN AMAZING DISCOVERY :O

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void blit ()
{
    vector <alphabet> letters;
    string sentence;
    getline (cin,sentence);
    int whole_length =6*sentence.length();
    int letter_postion=0;
    char output [whole_length][6];
    for (int b = 0; b<sentence.length(); b++)
    {
        letters.push_back(alphabet()); //each vector holds an array of a letter or space for the length of the line
        letters[b].set_arr(sentence[b]);
    }
    for (int a = 0; a<sentence.length(); a++)
    {
        for (int i = 1; i < 7; i++)
        {
            for (int j = 0; j < 6; j++)
            {
                output[i][j+letter_postion] = letters[a].ret_arr (i,j);
            }
        }

        letter_postion=letter_postion+6;
    }
    for (int a = 0;a<whole_length;a++)
    {
        for (int b = 6; b<whole_length;b++){
        cout<<output[b][a];
        }
        cout<<endl;
    }

}
Last edited on
printf("\a"); will make a beep, so if your reading in that ASCII symbol, and then output it, guess what?

I would test your code but only if you post a couple lines from your input file. external links = bad, never know what you will happen to you. The FBI might come to visit or something worse.
(I apologize for any harshness I may present, sorry)
Well, your code looks fine for the most part, but it's not very verbose. Meaning that I can't look at your code and distinctly tell what variable is what or why/where it is used. For instance, the lines:
1
2
3
4
5
6
7
8
        for (int q = 0; q<168; q++)
        {
            for (int z = 0; z<6; z++)
            {
                letterfile >> box[q][z];//initialize box from the txt
            }

        }


What is "q"? I know you'll say "just look at the code" but the point is I don't want/have time to read your code, so at a glance, what is "q"? Also, do NOT EVER use magic numbers. in the previous example, the line
 
for (int q = 0; q<168; q++)


has a magic number in it, the number '168' is this magic number. We call it that because it isn't a variable, or a const variable, it is simply 168. Which means, if you change this '168' to say, '72' somewhere else, you now need to change ALL '168's to '72's. If you can, ALWAYS prevent the use of magic numbers. Also, you comments aren't documentation as comments are meant to be, they are simply stating the obvious. In the line letterfile >> box[q][z];//initialize box from the txt the comment should explain not what is happening, but instead what your intentions are. In this case, I can clearly see that you are initializing the 'box' from the 'txt'. I don't need you to tell me that, I need to instead know you intentions as the coder and why this line is important.

Also, I am seeing a comparison between int and size_t // typedef'd as unsigned int usually. This will give you a warning at compile time and you should fix ALL warnings no matter how benign you may think they are.

You also are in need of a coding pattern. I use a lot of camelCase and have reserved letters for type specification. For instance, "i" is used to mean int, "n" is used to mean float/double, "v" is used to mean typename [] // Array , "p" is used to mean * // pointer , "C" is used to mean class, "S" is used to mean struct, "E is used to mean enum, "l_" is used to mean static, "m" is used to mean "class member", and lastly "g_" is use for global variables (all functions start with capitals). I use these prefixes like so:
1
2
3
4
for(size_t iNextListElem = 0; iNextListElem < miNumItemsInList; ++iNextListElem)
{
    g_vBuff[iNextListElem] = pCurrCharVal;
}


In looking at that code, you can tell just about what everything is used for (I do slip myself, so it's ok if you do too :) )

Another thing, you class doesn't follow the Rule of Three, I don't see a destructor, copy constructor, or the overloaded assignment operator (operator=()). These are VITAL when creating classes. Your class can still function without them, but it will behave in different way than you might expect. For more info on the Rule of Three: http://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29

This may seem like a lot....well...let's be honest, it kinda is :D. But if you adopt something similar but in your own way, finding bugs becomes a breeze and your code will look neater. Along side the fact that it will be easy to maintain. So if, say, in 10 years you fire up this program and notice a missing feature (or something of the like) you will be able to easily implement and maintain it!

I hope this long rant of mine has helped you!

EDIT: It also looks like you are in need of some function/MACRO that will give you the size of an array. You can use one of these (or your own if you want! This will help eliminate some of those magic numbers):
MACRO (yes, I know it's a macro, yes I know not to encourage their use):
#define DIM(x) (sizeof(x)/sizeof(*(x)))
Last edited on
Needed to hear that...so lastly what about outputting with letters side by side rather than on top of each other? any ideas?? maybe i can suss it out, i just need to make my brain stop buzzing.

hurr, yes i should learn to implement all this before im too set in my ways, good thing about beginners is they're malleable, so kay gonna have to use ur comments for reference.
You don't need the local array char output [whole_length][6]; in this function at all, which is good because declaring the dimensions dynamically in that way is bogus anyway.

Your "alphabet" object contains a 6x6 array of characters doesn't it?
You should be able to print the letters side by side (what I think you are trying to do) by writing all letters row 0 (with a space between) on one line, all letters row 1 on the next line, and so on...
@devonrevenge
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
	std::ifstream f("letters.txt.txt");
	std::ofstream of("text2.txt");
	std::vector<std::vector<std::string> > letters(26);
	std::string s;
	for(int i = 0; i < 26; ++i)
	{
		for(int j = 0; j < 6; ++j)
		{
			std::getline(f, s);
			letters[i].push_back(s);
		}
	}
	//output
	for(int i = 0; i < 6; ++i)
	{
		for(int j = 0; j < 26; ++j)
		{
			of<<letters[j][i]<<"  ";
		}
		of<<std::endl;
	}
oooh yeah...crap :/

maybe i shud jus move on to python.
Last edited on
Try adding a constructor to your alphabet class which takes a char as argument.
Then, instead of this:
1
2
letters.push_back(alphabet());
letters[b].set_arr(sentence[b]);

you could do it in one line:
letters.push_back(alphabet(sentence[b]));
ohh kaythats tidy... i was planning on submitting it and the asking for more efficient ideas afterwards so i could see where i need improving but know i know i have to put it in :D

i could just merge the member function in actually
Last edited on
@snow 56767: if you need one, you need the three.
¿what would you put it the destructor/copy-constructor/assignment operator in this case?


@devonrevenge:
All your object have the same content in the `box' member.
You may want to make it a class attribute instead.

¿What's `alpha' for? I see a find, that could be done in constant time instead value-'a'
I must admit, devonrevenge, that you do come up with interesting mini-project ideas!

I hope you welcome my frequent input.
ne555 points out some issues, and I wonder about some others too.

It seems to me that there could be a class "letter", which contains just the 6x6 char array.

A class called "alphabet" would contain an array of 26 letters.
This alphabet object should be a "singleton" type object. There only needs to be one instance of an alphabet to serve as a resource.

I have built a simple version based on this, and added word and sentence objects to it.
The idea is to produce this output:

HH  HH  EEEEEE  LL      LL        OO
HH  HH  EE      LL      LL      OO  OO
HHHHHH  EEEEEE  LL      LL      OO  OO
HH  HH  EE      LL      LL      OO  OO
HH  HH  EE      LL      LL      OO  OO
HH  HH  EEEEEE  LLLLLL  LLLLLL    OO

MM  MM  YY  YY
M MM M  YY__YY
M    M   YYYY
M    M    YY
M    M    YY
MM  MM    YY

NN  NN    AA    MM  MM  EEEEEE
N N  N  AA  AA  M MM M  EE
N  N N  AA  AA  M    M  EEEEEE
N   NN  AAAAAA  M    M  EE
N    N  AA  AA  M    M  EE
NN  NN  AA  AA  MM  MM  EEEEEE

 IIII     SS
  II    SS  SS
  II     SS
  II       SS
  II    SS  SS
 IIII     SS

DDDD    EEEEEE  V    V    OO    NN  NN
D   DD  EE      V    V  OO  OO  N N  N
D   DD  EEEEEE  VV  VV  OO  OO  N  N N
D   DD  EE      VV  VV  OO  OO  N   NN
D   DD  EE       VVVV   OO  OO  N    N
DDDD    EEEEEE    VV      OO    NN  NN

from this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
alphabet A("letters.txt");
alphabet* word::pA = &A;// give words access to the alphabet resource

int main ()
{
    word w_a("HELLO");
    sentence s_a("MY NAME IS DEVON");

    cout << w_a << endl;
    cout << s_a;

    cout << endl;
    return 0;
}

Is this sort of what you're aiming for here?
what you mean fun2code how side by side?
Could you please phrase that question more clearly?
THATS EXACTLY WHAT I WAS TRYING TO DO

but my code i keep re-writing keeps saying things top to bottom not left to right for some reason my brain thinks top to bottom not left to right cos i cant come up with the left to right solution that doesn't turn out top to bottom.

sokay fun2code, i didnt see ALL THIS you guys gave me, no need to rephrase anything...i really appreciate it fun2code, im not picking things up so fast when your not helping :P

that output... XD

im gonna make a mad face letter.

But wait...you make the same thing look soooo easy, in so few lines :/

this is where im at at the moment, some parts are there because i havnt moved on to the next part hence the class stuff,its a tad more efficient but still top to bottom (blit will take a argument and if i can find a timer delay function i can then animate words too)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <string>

using namespace std;

class alphabet
{
public:

    alphabet(char omega)//omega initializes an arr with appropriate array data
    {
        ifstream letterfile ("letterz.txt");
        for (int q = 0; q<27; q++)
        {
            for (int z = 0; z<36; z++)
            {
                letterfile >> box[q][z];
            }
        }

        char zeta[27]= {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' '};
        for(int a = 0; a<27; a++)
            alpha[a]=zeta[a];
        int num;
        for (int a = 0; a<27; a++)
        {
            if (alpha[a]==omega)
                num=a;
        }
        int boxcounter=0;
        for (int b = 0; b<6; b++)
        {
            for(int c = 0; c<6; c++)
            {
                arr[b][c]=box[num][boxcounter];
                boxcounter++;
            }
        }
    }


    char ret_arr (int num1, int num2)
    {
        return arr[num1][num2];
    }
private:
    char arr [6][6];
    char box [27][36];
    char alpha [27] ;

};

char blit ()//loads a big array with a load of little arrays using vectors
{
    vector <alphabet> letters;
    string sentence;
    getline (cin,sentence);

    for (int b = 0; b<sentence.length(); b++)
    {
      letters.push_back(alphabet(sentence[b]));
    }
    for(int a = 0; a<sentence.length();a++)//create a big array accrding to sent.length and init it with letterses vector arr
    {
        int bignum = 6*sentence.length();
        char bigarr [bignum][6];
        int letterposition = 0;
        for (int c= 0;c<6;c++)
        {
            for (int d=0;d<6;d++)
            {
                bigarr[c][d+letterposition]=letters[a].ret_arr(c,d);
                letterposition+6;
                cout<<bigarr[c][d+letterposition];
            }cout<<endl;
        }
    }
}




int main ()
{
blit();
}




this is an important mission
Last edited on
OK. I thought that's what you're after.

you make the same thing look soooo easy, in so few lines :/

I only showed what the code in main() looks like.
What you don't see is the 136 lines of code above main() where the various objects are defined, operators setup, etc... all so the code in main() can be so simple.

KABOOM:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
//#include <list>
//#include <algorithm>
//#include <queue>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::ifstream;

class letter
{
    public:

    letter(){};
    letter( const letter& r_ltr );// copy
    letter operator=( const letter& r_ltr );
    char arr[6][6];
};

letter::letter( const letter& r_ltr )// copy
{
    for( unsigned int i=0; i<36; ++i)
        arr[i/6][i%6] = r_ltr.arr[i/6][i%6];
}
letter letter::operator=( const letter& r_ltr )
{
    for( unsigned int i=0; i<36; ++i)
        arr[i/6][i%6] = r_ltr.arr[i/6][i%6];
    return *this;
}

class alphabet
{
    public:
    letter ltr[26];
    const letter& operator[](char idx )const;
    alphabet( string fname );
};

alphabet::alphabet( string fname )
{
    ifstream fin( fname.c_str() );

    if( fin )
    {
        for( unsigned int i=0; i< 26; ++i )
            for( unsigned int j=0; j< 6; ++j )
                for( unsigned int k=0; k< 6; ++k )
                {
                    char chIn;
                    fin >> chIn;

                    if( chIn == '-' )
                        ltr[i].arr[j][k] = ' ';
                    else
                        ltr[i].arr[j][k] = chIn;
                }

        fin.close();
    }
    else
        cout << "no file opened." << endl;
}

const letter& alphabet::operator[](char idx )const
{
    return ltr[ (int)(idx-'A') ];
}

class word
{
    public:
    static alphabet* pA;
    std::vector<char> ltrVec;
    word(){}
    word( string wordStr )
    {
        for(unsigned int i = 0; i < wordStr.size(); ++i )
            ltrVec.push_back( wordStr[i] );
    }
};

// output a word
std::ostream& operator<<( std::ostream& os, const word& r_word )
{
    for( unsigned int j=0; j< 6; ++j )
    {
        for( unsigned int c = 0; c <= r_word.ltrVec.size(); ++c )
        {
            for( unsigned int i=0; i< 6; ++i )
            {
                os << (*r_word.pA)[ r_word.ltrVec[c] ].arr[j][i];
            }
            os << "  ";
        }
        os << '\n';
    }
    return os;
}

class sentence
{
    public:
    std::vector<word> wordVec;
    sentence( string Sentence );
};

sentence::sentence( string Sentence )
{
    word temp;
    for( unsigned int i = 0; i < Sentence.size(); ++i )
    {
        if( Sentence[i] == ' ' )// new word
        {
            wordVec.push_back( temp );
            temp.ltrVec.clear();
        }
        else
            temp.ltrVec.push_back( Sentence[i] );
    }
    if( temp.ltrVec.size() > 0 )
        wordVec.push_back( temp );
}

// output a sentence
std::ostream& operator<<( std::ostream& os, const sentence& r_sent )
{
    for( unsigned int i = 0; i < r_sent.wordVec.size(); ++i )
    {
        os << r_sent.wordVec[i] << '\n';
    }
    return os;
}

See the definition of operator << for the word class to see how the side-by-side letter output works.
Last edited on
theres a lot of new stuff here so give me 20 hours or so :P

ooh theres a copy constructor, i know what that is :D maybe not so long

its very interesting seeing how someone else would do the same ting you would
Last edited on
OK. The basic thing about printing the letters side-by-side is that you can't just output one letter after another (as you can if displaying the letters vertically),

Instead, because each letter takes up 6 lines you print part (one row) of each letter on each line.
My quick and dirty version.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <sstream>

class alphabet
{
	std::vector<std::vector<std::string> > letters;
public:
	alphabet(std::string filename)
	{
		std::ifstream f(filename.c_str());
		letters.resize(26);
		std::string s;
		for(int i = 0; i < 26; ++i)
		{
			for(int j = 0; j < 6; ++j)
			{
				std::getline(f, s);
				letters[i].push_back(s);
			}
		}
                f.close();
	}

	std::string operator()(std::string s)
	{
		std::stringstream ss;
		
		for(int j = 0; j < 6; ++j)
		{
			for(int i = 0; i < s.size(); ++i)
			{
				ss << (((s[i] > 64) && (s[i] < 91)) ? letters[s[i] - 'A'][j] : "      ") << " ";
				//std::cout<<ss.str()<<std::endl;
			}
			ss<<"\n";
		}
		return ss.str();
	}
};

int main()
{	
	std::string s = "HELLO WORLD";
	alphabet a("text.txt");
	
	std::cout<<a(s)<<std::endl;
}


Edit: I didn't parse the dashes out, but that can be added easily.
Last edited on
@naraku9333
Your solution works nicely and is much simpler than mine.
It focuses on the key issue of printing the letters side by side quite directly.
Nice job.

I often over complicate my solutions.
how does operator() work again, its pretty hard to google cos you get lists of all the operators, i jsut find that linked list post again something on it there :/

does it just create a bespoke function for a class object?

I guess fun2code and I should study how to come up with said solutions, break problems down to basics in order to create simpler code

ah okay i see it now i remember all clearly, better had start playing with that
Last edited on
Pages: 12