I was making a cipher program, and when I changed from cin << mensagem to getline(cin, mensagem) the program started crashing after I executed it.
Any suggestions?
Here follows the program. I am using the C-free 5.0 as IDE and compiler, don't know if it helps wit the answer.
Just as a warn, i tried for(int p = 0; p <= mensagem.size(; ++p ), the program crashed as well without warning. Then after some tests in other compilers and getting the same result one of them said that was out of range, so when i putted the -1 it corrected the bug.
Update on the situation: Ok.. so now it reads space, but cuts the first letter. As for the fors loops, i removed the equal and the -1 and know it's working better, thanks. Any suggestion for it to stop cutting the first letter of the string?
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int codigo =0; //codigo AVCII da letra
string mensagem ;
char convertido = 'b';
int i = 0; // codigo de conversão AVCII
string cifrado;
string decifrado;
int main()
{
cout << "fale a cifra inicial"<<endl;
cin >> i;
i= i +30;
cout << " fale a mensagem:"<<endl;
cin >> mensagem;
getline(cin, mensagem);
for(int p = 0; p<mensagem.size(); ++p ){
codigo = mensagem.at(p);
convertido = codigo+i ;
cifrado+= convertido;
}
cout<< "concluido a cifragem"<<endl;
cout<< "--------------------------------------------------------"<<endl<<endl;
cout << cifrado<<endl;
cout<< endl<<"-----------------------------------"<<endl;
cout << "seu computador foi hackeado, estou desfazendo a cifra"<<endl;
for(int p = 0; p< cifrado.size(); ++p){
codigo = cifrado.at(p);
convertido = codigo - i;
decifrado += convertido;
}
cout << "decodificacao feita:"<<endl;
cout<< decifrado<< endl ;
return 0;
}