mercredi 26 août 2020

Random and replacement characters in c++ output

I am writing a program to reverse a string and insert random characters in between. Here is my code: main.cpp

#include <iostream>
#include <chrono>
#include <thread>
#include "encrypter.cpp"
using namespace std;
int main(int argc, char *argv[])
{
    char message[256];
 this_thread::sleep_for(chrono::milliseconds(1000));
 cout <<    "Make sure theres no one around you" << endl;
 this_thread::sleep_for(chrono::milliseconds(1000));
 cout <<    "Enter secret message ";
 cin.get(message, 256);
 cout <<    "message encrypted" <<  endl;
 enc(message);
 return 0;
}

encrypter.cpp

#include <iostream>
#include <string>
int getRandom(int a, int b) {
    return a + (rand() % static_cast<int>(b - a + 1));
}
using namespace std;
void enc(char message[256]) {
    int i = 0;
    int len =   strlen(message);
    int revlen =    len - 1;
    int wtpselector;
    int charselector;
    int encsim;
    char randchar[6] =  "@#$%&";
    char strreved[256];
    char strenc[1024];
    while (i <  len) {
        strreved[revlen]    =   message[i];
        i++;
        revlen--;
    }
    revlen =    strlen(strreved);
    len =   revlen - 1;
    i = 0;
    encsim =    0;
    while (i <  revlen) {
        wtpselector =   getRandom(0, 4);
        charselector =  getRandom(0, 4);
        if (wtpselector ==  0) {
            strenc[encsim]  =   strreved[i];
            i++;
            encsim++;
        } else {
            strenc[encsim]  =   randchar[charselector];
            encsim++;
        }
    }
    cout << strenc << endl;
}

But the output has many random characters that are not supposed to be there and are not in the program. Like:

Input: hello world
Output: $%@$#&&d&&@%$%1%row &#$$@%&ol@%#@%&1%&#$&#ehe$%%€@@8@&%@$#& #%@@%&¢&@%#&#$#@$#%%##&%#@&#&$8%#$#@#$@$@#%&&@&#@Q#$$#&¢%@% Q#\#&$@#{&&&&$@¢ \ $$$$@@@@@#&&&%%&&%{ $@¢v#&@&~u@@@@%&%¢

Please help!!!




Aucun commentaire:

Enregistrer un commentaire