Compiles fine, but doesn't return correctly?

My program below is supposed to output a randomized string of letters, capitol and lower case at random order. It compiles fine, but doesn't cout the pass string. Suggestions to fix this?

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

using namespace std;

int main() {
    char ar[] = {'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'};
    string pass;
    char add;
    srand(time(NULL));
    for (int i; i < 15; i++) {
        int z = rand() % 25;
        char add = ar[z];
        int a = rand() % 1;
        if (a == 1) {
            toupper(add);
        } else {
            tolower(add);
        }
        pass.append(1, add);
    }
    cout << pass;
    system("PAUSE >NUL");
}
You'll want rand()%26 (26%26 == 0, so with 25 you wouldn't get the 'z'). Same here: int a = rand() % 2;//not 1

Your problem however, is most likely just that you forgot the endl at the end. This may keep the out buffer from being flushed.

cout << pass << std::endl;
Topic archived. No new replies allowed.