Code Trouble

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
include <cstring>
#include <iostream>
#include <ctime>
#include <cstdlib>

int main(void)
{
using namespace std;
srand(unsigned int(time(0)));
unsigned n = 1234567890;
char buf[rand() % 255 + 1]; 
_itoa(n, buf, 62);
cout << "Your Password is: " << buf << "\n";
system("pause");
return 0;
}

(11) : error C2057: expected constant expression
(11) : error C2466: cannot allocate an array of constant size 0
(11) : error C2133: 'buf' : unknown sizeI have the errors:
I know what the problem is but I don't how to fix it.
Last edited on
I'm not clear on what your asking. Are you looking to populate the char array with random numbers?
Exactly, and make the program print out random characters.
I did the array part already, but I can't get it to print random characters.
On line 11 you are allocating a char array with a non-constant size. If you want a dynamic size, you need to use dynamic allocation.

char *buf = new char[rand % 255 + 1];
Topic archived. No new replies allowed.