vendredi 22 janvier 2016

How to convert an integer to its ascii equivalent

First up, i am very much aware that there are other posts like this one. But i, for whatever reason, haven't been able to get it to work (#imanoob).

Context: I am making myself a password generator in c++, basically it uses rand to generate numbers, and these numbers correspond directly to ASCII characters.

So, generating the numbers is easy, but i need to convert this to their ASCII equivalents to actually make a usable password.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main ()
{
    //Initializing variables.
    int type = 0;
    int character = 0;
    char print = character;

    //Generator.
    srand(time(NULL));
    for (int i=0; i<10;i++)
    {
        type = rand()%(3-1+1)+1;//Determines: 1 = Uppercase, 2 = Lowercase, 3 = Number.
        if (type == 1)//If Uppercase.
        {
            character = rand()%(90-65+1)+65;//Determines Uppercase character to be generated.
            cout<<print<<endl;
        }
        if (type == 2)//If Lowercase.
        {
            character = rand()%(122-97+1)+97;//Determine Lowercase character to be generated.
            cout<<print<<endl;
        }
        if (type == 3)//If Numerical.
        {
            character = rand()%(57-48+1)+48;//Determines Number to be generated.
            cout<<print<<endl;
        }
    }
}

In the code i posted above, you can see i last tried blatantly telling the program that the variable "character" needs to be used as an actually character. yes im stupid.

can i get a bit of insight here pls. if there is a link someon could provide to exactly this problem, i would be happy. and i really dont care if i get downvoted to oblivion.

Ok so there seems to be a bit of confusion. Rand generates a number say between 65 and 90. These numbers correspond to capital letters on the ASCII table. i want to print to the console the letter, not the generated number.




Aucun commentaire:

Enregistrer un commentaire