tropix
tropix

Reputation: 13

How do I randomize my program's Window Title?

I am trying to make my program have a different window title each time it get's compiled.

void rndmTitle() {
    int num;
    int length = 15;

    std::string characters = "abcdefghi9182345jklmnopqrstuv211935960473wxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    for (int j = 0; j < length; j++)
    {
        num = rand() % 80 + 1;
    }
    SetConsoleTitle(characters[val.num]);
}

I got that code from a password generator I had made a while ago. However it doesn't work like I thought it would.

It's supposed to take random letters/numbers from those 81 characters in "string characters" and then set it as window title.

But if I try to set the console title, it'll just tell me that the argument of type "char" is incompatible with parameter of type "LPCSTR".

Upvotes: 0

Views: 2312

Answers (2)

Clapps
Clapps

Reputation: 1

This also works

#include <iostream>
#include <random>
#include <Windows.h>

using namespace std;

std::string gen_string(const int length)
{
    std::string GeneratedString;
    static const char Alphabet[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 
    srand((unsigned)time(NULL) * 5);
    for (int i = 0; i < length; i++) 
        GeneratedString += Alphabet[rand() % (sizeof(Alphabet) - 1)];
    return GeneratedString;
}

int main()
{
    SetConsoleTitleA(gen_string(25).c_str());
}

credits to this dude

Upvotes: 0

Jarod42
Jarod42

Reputation: 217275

Your title creation should be something like:

void rndmTitle(){
    constexpr int length = 15;
    const auto characters = TEXT("abcdefghi9182345jklmnopqrstuv211935960473wxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
    TCHAR title[length + 1]{};

    for (int j = 0; j != length; j++)
    {
        title[j] += characters[rand() % 80];
    }

    SetConsoleTitle(title);
}

Upvotes: 2

Related Questions