0

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;

}

  • Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you probably want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) Using a debugger will also allow you to find out where exactly your program is crashing, for example in which line. – Andreas Wenzel Jul 27 '21 at 22:04
  • 3
    `for(int p = 0; p<= cifrado.size()-1; ++p){` should just be `for(int p = 0; p< cifrado.size(); ++p){` The problem is `>>` doesn't remove the end of line character so `getline` reads only that and you have an empty string. Then, because of the odd loop condition above, the loop executes and `at` fails because it is out of range. – Retired Ninja Jul 27 '21 at 22:06
  • `cin >> i; ... getline(cin, mensagem);` doesn't work [because of this](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction), and then `for(int p = 0; p <= mensagem.size()-1; ++p )` doesn't work and goes out of bounds when `mensagem` is an empty string. – Remy Lebeau Jul 27 '21 at 22:06
  • he should remove `=` in for. – Parisa.H.R Jul 27 '21 at 22:07
  • @Parisa.H.R `cifrado.size()-1` would still be a problem on a blank line. – drescherjm Jul 27 '21 at 22:08
  • but crash fixed :) – Parisa.H.R Jul 27 '21 at 22:09
  • Unfortunately Undefined behavior may allow broken code to appear to work in that case for some inputs. – drescherjm Jul 27 '21 at 22:10
  • ok, I remove my answer :) – Parisa.H.R Jul 27 '21 at 22:11
  • Regarding your latest edit, `<=` was the bug, the `-1` just made it worse. – Retired Ninja Jul 27 '21 at 22:26

0 Answers0