Random String Sequence Crash

Hey, I was trying to create a string that had a random sequence of letters, but when I try it, it completes the string, and then displays an error report message.
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
#include <iostream>
#include <fstream>
#include <windows.h>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;


string fileTitle="aaaaaa";
char letter;


char generateLetter () 
{
int random;
random = rand () % 26 + 1;    
       if (random == 1) {
       letter= 'a';
       } 
       if (random == 2) {
       letter= 'b';
       }  
       if (random == 3) {
       letter= 'c';
       }  
       if (random == 4) {
       letter= 'd';
       }  
       if (random == 5) {
       letter= 'e';
       }  
       if (random == 6) {
       letter= 'f';
       }  
       if (random == 7) {
       letter= 'g';
       }  
       if (random == 8) {
       letter= 'h';
       }  
       if (random == 9) {
       letter= 'i';
       }  
       if (random == 10) {
       letter= 'j';
       }  
       if (random == 11) {
       letter= 'k';
       }  
       if (random == 12) {
       letter= 'l';
       }  
       if (random == 13) {
       letter= 'm';
       }  
       if (random == 14) {
       letter= 'n';
       }  
       if (random == 15) {
       letter= 'o';
       }  
       if (random == 16) {
       letter= 'p';
       }  
       if (random == 17) {
       letter= 'q';
       }  
       if (random == 18) {
       letter= 'r';
       }  
       if (random == 19) {
       letter= 's';
       }  
       if (random == 20) {
       letter= 't';
       }  
       if (random == 21) {
       letter= 'u';
       }  
       if (random == 22) {
       letter= 'v';
       }  
       if (random == 23) {
       letter= 'w';
       }  
       if (random == 24) {
       letter= 'x';
       }  
       if (random == 25) {
       letter= 'y';
       }  
       if (random == 26) {
       letter= 'z';
       }
       //cout << letter;
       return letter;
}                   

string generateFileTitle () 
{
       for (int n = 0; n < fileTitle.size(); n++) {
           fileTitle[n]=generateLetter();
       }
cout << fileTitle;       
}       
      
int main () {
srand (time(0));
    
generateFileTitle();
Sleep(2000);

return 0;

}  


Anyone know what might have happened?
Last edited on
Here is a random string generator. To have longer strings manipulate the number bolded

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
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;

int main(int nNumberofArgs,char* pszArgs[])
{
    char code[32];
    char input;
    int s;
    int n;
    for(;input != 'n';)
    {
        srand((unsigned)time(0));
        for(s = 0;s < 32;s++)
        {
           code[s] = 1 + rand() % (52);
        }

        for(n = 0;n < 32;n++)
        {
            switch(code[n])
            {
                case 1:
                    code[n] = 'a';
                    break;
                case 2:
                    code[n] = 'b';
                    break;
                case 3:
                    code[n] = 'c';
                    break;
                case 4:
                    code[n] = 'd';
                    break;
                case 5:
                    code[n] = 'e';
                    break;
                case 6:
                    code[n] = 'f';
                    break;
                case 7:
                    code[n] = 'g';
                    break;
                case 8:
                    code[n] = 'h';
                    break;
                case 9:
                    code[n] = 'i';
                case 10:
                    code[n] = 'j';
                    break;
                case 11:
                    code[n] = 'k';
                    break;
                case 12:
                    code[n] = 'l';
                    break;
                case 13:
                    code[n] = 'm';
                    break;
                case 14:
                    code[n] = 'n';
                    break;
                case 15:
                    code[n] = 'o';
                    break;
                case 16:
                    code[n] = 'p';
                    break;
                case 17:
                    code[n] = 'q';
                    break;
                case 18:
                    code[n] = 'r';
                    break;
                case 19:
                    code[n] = 's';
                    break;
                case 20:
                    code[n] = 't';
                    break;
                case 21:
                    code[n] = 'u';
                    break;
                case 22:
                    code[n] = 'v';
                    break;
                case 23:
                    code[n] = 'w';
                    break;
                case 24:
                    code[n] = 'x';
                    break;
                case 25:
                    code[n] = 'y';
                    break;
                case 26:
                    code[n] = 'z';
                    break;
                case 27:
                    code[n] = 'A';
                    break;
                case 28:
                    code[n] = 'B';
                    break;
                case 29:
                    code[n] = 'C';
                    break;
                case 30:
                    code[n] = 'D';
                    break;
                case 31:
                    code[n] = 'E';
                    break;
                case 32:
                    code[n] = 'F';
                    break;
                case 33:
                    code[n] = 'G';
                    break;
                case 34:
                    code[n] = 'H';
                    break;
                case 35:
                    code[n] = 'I';
                case 36:
                    code[n] = 'J';
                    break;
                case 37:
                    code[n] = 'K';
                    break;
                case 38:
                    code[n] = 'L';
                    break;
                case 39:
                    code[n] = 'M';
                    break;
                case 40:
                    code[n] = 'N';
                    break;
                case 41:
                    code[n] = 'O';
                    break;
                case 42:
                    code[n] = 'P';
                    break;
                case 43:
                    code[n] = 'Q';
                    break;
                case 44:
                    code[n] = 'R';
                    break;
                case 45:
                    code[n] = 'S';
                    break;
                case 46:
                    code[n] = 'T';
                    break;
                case 47:
                    code[n] = 'U';
                    break;
                case 48:
                    code[n] = 'V';
                    break;
                case 49:
                    code[n] = 'W';
                    break;
                case 50:
                    code[n] = 'X';
                    break;
                case 51:
                    code[n] = 'Y';
                    break;
                case 52:
                    code[n] = 'Z';
                    break;
                default:
                    break;
            }
        }
        cout << "Here is your random string: " << code << endl;
        cout << "Would you like another? (Y/N): ";
        cin >> input;
        cout << endl;
    }
        system("PAUSE");
        return 0;
}
You didnt need all those if/case's.
What is the error message?
I removed the windows.h header and the call to Sleep() and it ran fine for me.

generatrFileTitle doesn't return anything and should be a void function.

generateLetter can be simplified considerably.
1
2
3
char generateLetter () 
{   return rand () % 26 + 'a';
}                   

Ah! Changing the generateFileTitle to void fixed it. strange. Thanks for the assistance to all. And that does simplify the random generation a lot. Didn't know it worked with characters. Thanks again.
I feel like a total idiot now...

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
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;

int main(int nNumberofArgs,char* pszArgs[])
{
    char code[32];
    char input;
    int s;
    for(;input != 'n';)
    {
        srand((unsigned)time(0));
        for(s = 0;s < 32;s++)
        {
           code[s] = rand() % (127) + 'a';
        }

        cout << "Here is your random string: " << code << endl;
        cout << "Would you like another? (Y/N): ";
        cin >> input;
        cout << endl;
    }
        system("PAUSE");
        return 0;
}
closed account (S6k9GNh0)
Here's a secret: ASCII values (and unicode values I guess...) have numerical values associated with them. 'a' is synonomous to an 8-bit variable holding the value 97. So, random = rand () % 26 + 'a'; has the same functionality as your entire generateLetter() function.

Aside from that, it seems to work: http://liveworkspace.org/code/vIPQm$0
Example of improved version: http://liveworkspace.org/code/vIPQm$6

EDIT: Well, I left this post while there were no replies and when I submit, there's five saying the exact same thing. Oh well, left for educational purpose...
Last edited on
@greenleaf800073: you forget the null terminating char.
Topic archived. No new replies allowed.