Exe file crashes when closed...

I figure that I should probably figure out how to allocate a memory buffer or something so that the the exe file does not crash when it is done. I want to have more room to add more code to this code, like getting rid of white space in the string and changing uppercase to lower case; Etc. Here is the code:

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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <iostream>
#include <conio.h>

using namespace std;

      int acount;
      int bcount;
      int ccount;
      int dcount;
      int ecount;
      int fcount;
      int gcount;
      int hcount;
      int icount;
      int jcount;
      int kcount;
      int lcount;
      int mcount;
      int ncount;
      int ocount;
      int pcount;
      int qcount;
      int rcount;
      int scount;
      int tcount;
      int ucount;
      int vcount;
      int wcount;
      int xcount;
      int ycount;
      int zcount;
      
      int sphere1 = 1;   
      int sphere2 = 2;
      int sphere3 = 3;
      int sphere4 = 4;
      int sphere5 = 5;
      int sphere6 = 6;
      int sphere7 = 7;
      int sphere8 = 8;
      int sphere9 = 9;
      int sphere0 = 10;
      
      int count1;
      int count2;
      int count3;
      int count4;
      int count5;
      int count6;
      int count7;
      int count8;
      int count9;
      int count0;

      int rsphere1;      
      int rsphere2;
      int rsphere3;
      int rsphere4;
      int rsphere5;
      int rsphere6;
      int rsphere7;
      int rsphere8;
      int rsphere9;
      int rsphere0;

       int CountCharacter( char * str , char ch )
       {
       int countChar = 0;
       for ( int i = 0; str[i] != '\0'; i++)
       {
       if ( str[i] == ch )
       countChar = countChar + 1;
       }
       return countChar;
       }
       
      int main()
      {  
      char str[] = "";
      
      cout << "                         Number Tree System";
      cout << endl;
      cout << endl;
      cout << "Enter Word or a Name: ";
      cin >> str;
      
      acount = CountCharacter( str , 'a' );
      bcount = CountCharacter( str , 'b' );
      ccount = CountCharacter( str , 'c' );
      dcount = CountCharacter( str , 'd' );
      ecount = CountCharacter( str , 'e' );
      fcount = CountCharacter( str , 'f' );
      gcount = CountCharacter( str , 'g' );
      hcount = CountCharacter( str , 'h' );
      icount = CountCharacter( str , 'i' );
      jcount = CountCharacter( str , 'j' );
      kcount = CountCharacter( str , 'k' );
      lcount = CountCharacter( str , 'l' );
      mcount = CountCharacter( str , 'm' );
      ncount = CountCharacter( str , 'n' );
      ocount = CountCharacter( str , 'o' );
      pcount = CountCharacter( str , 'p' );
      qcount = CountCharacter( str , 'q' );
      rcount = CountCharacter( str , 'r' );
      scount = CountCharacter( str , 's' );
      tcount = CountCharacter( str , 't' );
      ucount = CountCharacter( str , 'u' );
      vcount = CountCharacter( str , 'v' );
      wcount = CountCharacter( str , 'w' );
      xcount = CountCharacter( str , 'x' );
      ycount = CountCharacter( str , 'y' );
      zcount = CountCharacter( str , 'z' );
      _getche();
      
      count1 = acount + icount + jcount + kcount + qcount + tcount + ycount;
      count2 = bcount + kcount + lcount + rcount + ucount;
      count3 = ccount + gcount + lcount + mcount + scount + xcount;
      count4 = dcount + mcount + ncount + tcount + ycount;
      count5 = ecount + hcount + ncount + ocount + xcount;
      count6 = fcount + jcount + pcount + ucount + vcount + wcount;
      count7 = gcount + ocount + qcount + vcount + zcount;
      count8 = fcount + hcount + pcount + rcount;
      count9 = icount + scount;
      count0 = jcount + kcount + lcount + mcount + ncount + ocount + pcount + qcount + rcount + scount + tcount + ucount + vcount + wcount + xcount + ycount;
      
       rsphere1 = sphere1 * count1;
       rsphere2 = sphere2 * count2;
       rsphere3 = sphere3 * count3;
       rsphere4 = sphere4 * count4;
       rsphere5 = sphere5 * count6;
       rsphere6 = sphere6 * count6;
       rsphere7 = sphere7 * count7;
       rsphere8 = sphere8 * count8;
       rsphere9 = sphere9 * count9;
       rsphere0 = sphere0 * count0;
      
       cout << endl;
       cout << "                                 1 x ";
       cout << count1;
       cout << " = ";
       cout << rsphere1 << endl;
       
       cout << endl;
       cout << "                                 2 x ";
       cout << count2;
       cout << " = ";
       cout << rsphere2 << endl;
       
       cout << endl;
       cout << "                                 3 x ";
       cout << count3;
       cout << " = ";
       cout << rsphere3 << endl;
       
       cout << endl;
       cout << "                                 4 x ";
       cout << count4;
       cout << " = ";
       cout << rsphere4 << endl;
       
       cout << endl;
       cout << "                                 5 x ";
       cout << count5;
       cout << " = ";
       cout << rsphere5 << endl;
       
       cout << endl;
       cout << "                                 6 x ";
       cout << count6;
       cout << " = ";
       cout << rsphere6 << endl;
       
       cout << endl;
       cout << "                                 7 x ";
       cout << count7;
       cout << " = ";
       cout << rsphere7 << endl;
       
       cout << endl;
       cout << "                                 8 x ";
       cout << count8;
       cout << " = ";
       cout << rsphere8 << endl;
       
       cout << endl;
       cout << "                                 9 x ";
       cout << count9;
       cout << " = ";
       cout << rsphere9 << endl;
       
       cout << endl;
       cout << "                                 10 x ";
       cout << count0;
       cout << " = ";
       cout << rsphere0 << endl;
       
       cout << endl;
       system("pause");
       
       return 0;
}


Just let me know what I should do to keep this from crashing so I can add more code.
Edit : Please give the log program output

First, you did very good about variables managing.

Second:
char str[] = "";
I pretty sure this is the problem which causes crashes that you are hunting.
You defined a non-size char array.
This should have a specific size . Other method (you will figure soon) is using pointer to allocating an array dynamic size.
Example :
char str[4095];
It can hold up 4095 characters. Any input string that contains more than 4095 characters will cause crash the program at all.
Last edited on
The program desperately cries out for the use of arrays.
For example this part:
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
      int acount;
      int bcount;
      int ccount;
      int dcount;
      int ecount;
      int fcount;
      int gcount;
      int hcount;
      int icount;
      int jcount;
      int kcount;
      int lcount;
      int mcount;
      int ncount;
      int ocount;
      int pcount;
      int qcount;
      int rcount;
      int scount;
      int tcount;
      int ucount;
      int vcount;
      int wcount;
      int xcount;
      int ycount;
      int zcount;
      acount = CountCharacter( str , 'a' );
      bcount = CountCharacter( str , 'b' );
      ccount = CountCharacter( str , 'c' );
      dcount = CountCharacter( str , 'd' );
      ecount = CountCharacter( str , 'e' );
      fcount = CountCharacter( str , 'f' );
      gcount = CountCharacter( str , 'g' );
      hcount = CountCharacter( str , 'h' );
      icount = CountCharacter( str , 'i' );
      jcount = CountCharacter( str , 'j' );
      kcount = CountCharacter( str , 'k' );
      lcount = CountCharacter( str , 'l' );
      mcount = CountCharacter( str , 'm' );
      ncount = CountCharacter( str , 'n' );
      ocount = CountCharacter( str , 'o' );
      pcount = CountCharacter( str , 'p' );
      qcount = CountCharacter( str , 'q' );
      rcount = CountCharacter( str , 'r' );
      scount = CountCharacter( str , 's' );
      tcount = CountCharacter( str , 't' );
      ucount = CountCharacter( str , 'u' );
      vcount = CountCharacter( str , 'v' );
      wcount = CountCharacter( str , 'w' );
      xcount = CountCharacter( str , 'x' );
      ycount = CountCharacter( str , 'y' );
      zcount = CountCharacter( str , 'z' );

could be replaced with this:
1
2
3
4
5
6
7
    int count[26];

    char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
    for (int i=0; i<26; i++)
    {
        count[i] = CountCharacter(str, alphabet[i]);
    }

I suggest you take some time out from writing code to absorb a bit of information on the topic: http://www.cplusplus.com/doc/tutorial/arrays/
and http://www.cplusplus.com/doc/tutorial/ntcs/
How am I supposed to do the math with it then? Each letter count is its own variable for a reason. I will allocate some memory though.
An array is a group of variables that share the same name. For example:

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
int foo[5];  // this creates 5 different ints

// each can be accessed with an 'index', between 0 and 4:
foo[0] = 3;
foo[1] = 6;
foo[2] = 25;
foo[3] = 10;
foo[4] = 1;


// you can also use another variable as an index:
int index = 2;

cout << foo[index] << endl;  // prints 25

// this allows you to use a for loop, and use the loop counter to step through each
//  element in an array:
for( int i = 0; i < 5; ++i )  // this for loop will print each element of the array
{
  cout << foo[i] << endl;
}
/* above loop prints:
3
6
25
10
1
*/



As a general rule of thumb, if you find yourself naming things like "foo1", "foo2", "foo3", etc... then you are doing it wrong. An array is probably a better option in that case.
Quantum7 wrote:
How am I supposed to do the math with it then?


Instead of this:
 
    count1 = acount + icount + jcount + kcount + qcount + tcount + ycount; 

you will have:
1
2
    count1 = count[0] + count[8] + count[9] + count[10] + count[16] 
               + count[19] + count[24]; 


If you use the editor to make a global change of each variable to its new equivalent, the change can be carried out methodically, before doing some tidying up of code which will no longer be needed.

Similarly, this:
1
2
3
    count0 = jcount + kcount + lcount + mcount + ncount + ocount 
              + pcount + qcount + rcount + scount + tcount + ucount 
              + vcount + wcount + xcount + ycount;

could become:
1
2
3
    count0 = 0;
    for (int i=8; i<25; i++)
        count0 += count[i];


One the whole, there is far more to be gained from the use of arrays than there is from sticking with lots of individual variables which all have to be named individually whenever they are used.
Last edited on
Here's the converted 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
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
#include <iostream>
#include <conio.h>

using namespace std;

    int ltr_cnt[26];
    int sphere[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int count[10];
    int rsphere[10];

int CountCharacter( char * str , char ch )
{
    int countChar = 0;
    for ( int i = 0; str[i] != '\0'; i++)
    {
        if ( str[i] == ch )
            countChar = countChar + 1;
    }
    return countChar;
}

int main()
{
    char str[1000] = "";

    cout << "                         Number Tree System";
    cout << endl;
    cout << endl;
    cout << "Enter Word or a Name: ";
    cin >> str;


    char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
    for (int i=0; i<26; i++)
    {
        ltr_cnt[i] = CountCharacter(str, alphabet[i]);
    }

    _getche();

    count[0] = ltr_cnt[0] + ltr_cnt[8]  + ltr_cnt[9]  + ltr_cnt[10] + ltr_cnt[16]
             + ltr_cnt[19] + ltr_cnt[24];
    count[1] = ltr_cnt[1] + ltr_cnt[10] + ltr_cnt[11] + ltr_cnt[17] + ltr_cnt[20];
    count[2] = ltr_cnt[2] + ltr_cnt[6]  + ltr_cnt[11] + ltr_cnt[12] + ltr_cnt[18] + ltr_cnt[23];
    count[3] = ltr_cnt[3] + ltr_cnt[12] + ltr_cnt[13] + ltr_cnt[19] + ltr_cnt[24];
    count[4] = ltr_cnt[4] + ltr_cnt[7]  + ltr_cnt[13] + ltr_cnt[14] + ltr_cnt[23];
    count[5] = ltr_cnt[5] + ltr_cnt[9]  + ltr_cnt[15] + ltr_cnt[20] + ltr_cnt[21] + ltr_cnt[22];
    count[6] = ltr_cnt[6] + ltr_cnt[14] + ltr_cnt[16] + ltr_cnt[21] + ltr_cnt[25];
    count[7] = ltr_cnt[5] + ltr_cnt[7]  + ltr_cnt[15] + ltr_cnt[17];
    count[8] = ltr_cnt[8] + ltr_cnt[18];
    count[9] = ltr_cnt[9]  + ltr_cnt[10] + ltr_cnt[11] + ltr_cnt[12] + ltr_cnt[13] + ltr_cnt[14]
             + ltr_cnt[15] + ltr_cnt[16] + ltr_cnt[17] + ltr_cnt[18] + ltr_cnt[19] + ltr_cnt[20]
             + ltr_cnt[21] + ltr_cnt[22] + ltr_cnt[23] + ltr_cnt[24];


    for (int i=0; i<10; ++i)
    {
       rsphere[i] = sphere[i] * count[i];
    }


    for (int i=0; i<10; i++)
    {
        cout << endl;
        cout << "                                 ";
        cout << i+1 << " x ";
        cout << count[i];
        cout << " = ";
        cout << rsphere[i] << endl;
    }

    cout << endl;
    system("pause");

    return 0;
}


76 lines versus 201 lines.

And in so doing I automatically fixed a bug in the original program at line 130, which gave incorrect results.
rsphere5 = sphere5 * count6;
Last edited on
Wow, this program seems needs lots of improvements. Anything else? Simpler? Shorter? Smarter? Faster?
I think it can still be better.
Jackson Marie wrote:
Simpler? Shorter? Smarter? Faster?

At this stage I risk the code being more clever than useful.
But taking that risk, original code:
1
2
3
4
5
6
7
8
9
10
11
12
    count1 = acount + icount + jcount + kcount + qcount + tcount + ycount;
    count2 = bcount + kcount + lcount + rcount + ucount;
    count3 = ccount + gcount + lcount + mcount + scount + xcount;
    count4 = dcount + mcount + ncount + tcount + ycount;
    count5 = ecount + hcount + ncount + ocount + xcount;
    count6 = fcount + jcount + pcount + ucount + vcount + wcount;
    count7 = gcount + ocount + qcount + vcount + zcount;
    count8 = fcount + hcount + pcount + rcount;
    count9 = icount + scount;
    count0 = jcount + kcount + lcount + mcount + ncount + ocount 
           + pcount + qcount + rcount + scount + tcount + ucount
           + vcount + wcount + xcount + ycount;


New code:
1
2
3
4
5
6
7
8
9
10
    count[0] = sum(ltr_cnt, "aijkqty" );
    count[1] = sum(ltr_cnt, "bklru" );
    count[2] = sum(ltr_cnt, "cglmsx" );
    count[3] = sum(ltr_cnt, "dmnty" );
    count[4] = sum(ltr_cnt, "ehnox" );
    count[5] = sum(ltr_cnt, "fjpuvw" );
    count[6] = sum(ltr_cnt, "goqvz" );
    count[7] = sum(ltr_cnt, "fhpr" );
    count[8] = sum(ltr_cnt, "is" );
    count[9] = sum(ltr_cnt, "jklmnopqrstuvwxy" );

together with this function definition:
1
2
3
4
5
6
7
8
9
10
11
int sum(int array[], char * letters)
{
    int total = 0;
    int i = 0;
    while (int index = letters[i++]) // loop until null terminator
    {
        index -= 'a';                // 'a' converts to [0] etc.
        total += array[index];
    }
    return total;
}

I don't know whether this is an improvement or not. It does rely on the fact that letters a-z are consecutive in ASCII, but could cause problems with different encodings.

To be honest, I was just playing around with ideas. Next of course the strings "aijkqty", "bklru" etc. could be placed in an array and the above carried out using yet another loop. I do think that might be going a little too far. :-)
It does rely on the fact that letters a-z are consecutive in ASCII, but could cause problems with different encodings.


a-z are consecutive in virtually all encodings, so this is not something I would worry about.

Besides, they are guaranteed to be consecutive in the only encoding that matters: Unicode. ;P
Looks like some really good ideas for me to think on.
@Quantum7 Just think of these as ideas, there's always more than one way to approach a problem.

I like to get code down to a more compact size, so that the overall structure can be seen. The fine detail may be separated out into separate functions - or classes where appropriate. That's why in general I'd prefer to keep the size of the code in main() fairly modest. If main() can be printed on a single sheet of paper, that's reasonable to me.

Here's one more version. Again, I don't claim it is an improvement, but it follows up a previous idea. The array of char strings at line 29 is used to then allow everything to be processed in a single for loop at lines 32 to 42.

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
#include <iostream>
#include <conio.h>

    using namespace std;

int sum(int array[], char * letters);
int CountCharacter( char * str , char ch );

int main()
{
    char str[1000] = "";
    int ltr_cnt[26];
    int sphere[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int count[10];
    int rsphere[10];

    cout << "                         Number Tree System" << endl << endl;
    cout << "Enter Word or a Name: ";
    cin >> str;

    char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
    for (int i=0; i<26; i++)
    {
        ltr_cnt[i] = CountCharacter(str, alphabet[i]);
    }

    _getche();

    char * alpha[10] = { "aijkqty", "bklru", "cglmsx", "dmnty", "ehnox",
        "fjpuvw", "goqvz" , "fhpr", "is", "jklmnopqrstuvwxy" };

    for (int i=0; i<10; ++i)
    {
        count[i] = sum(ltr_cnt, alpha[i] );
        rsphere[i] = sphere[i] * count[i];
        cout << endl;
        cout << "                                 ";
        cout << i+1 << " x ";
        cout << count[i];
        cout << " = ";
        cout << rsphere[i] << endl;
    }

    cout << endl;
    system("pause");

    return 0;
}
//--------------------------------------------
int sum(int array[], char * letters)
{
    int total = 0;
    int i = 0;
    while (int index = letters[i++]) // loop until null terminator
    {
        index -= 'a';                // 'a' converts to [0] etc.
        total += array[index];
    }
    return total;
}
//----------------------------------------
int CountCharacter( char * str , char ch )
{
    int countChar = 0;
    for ( int i = 0; str[i] != '\0'; i++)
    {
        if ( str[i] == ch )
            countChar = countChar + 1;
    }
    return countChar;
}
Last edited on
Topic archived. No new replies allowed.