+ 1

How to perform this type conversation properly? Without affecting the speed of the code

This program takes a string as input , and covert it to some random string. But when the value is more that 127 , c gets some negative value assigned to it . And when I convert the c to int the int value becomes much bigger negative value. cout << "\nEnter any string : "; cin >> s1; for (char& c : s1) { num = c; num= (num+rand())%256; // const char char_max = (char)(((unsigned char) char(-1)) / 2); //c = (num & char_max) c = num; } cout <<"\n"<< s1;

22nd Jan 2019, 2:30 AM
BALA KUMARAN J
BALA KUMARAN J - avatar
2 Respuestas
+ 2
Can't you just do, cin >> s1; for (char& c : s1) c = rand()%256; cout << "\n" << s1; but ofc, you may want to consider to not land on invisible characters (0-32). cin >> s1; for (char& c : s1) c = 32+(rand()%224); cout << "\n" << s1;
22nd Jan 2019, 2:56 AM
Hatsy Rei
Hatsy Rei - avatar
0
That's correct but That's not what I asked for. I just wanted a solution for my problem , that was just a sample program
22nd Jan 2019, 11:29 AM
BALA KUMARAN J
BALA KUMARAN J - avatar