the program must read a text file, encrypt the content and then rewrite everything to a text file. compile ok, it crashes while running and still produces an empty output. how can I fix this?

vigenere.h

#include <string>

using namespace std;

class Vigenere{
      private:
              string key;
              string text;
              static char ALPH[21];
              
              static string StringToUpper(string);
              void LoadText(string);
              static string EraseChar(string);
              void TextErase();
      public:        
              Vigenere(string);
              void encode(string, string);
              void decode(string, string);
};

vigenere.cpp

#include "vigenere.h"
#include <string>
#include <iostream>
#include <fstream>

using namespace std;

//genera la chiave
Vigenere::Vigenere(string str){
       key = str;
       key = StringToUpper(key);
       if(!key.empty()){
           key = EraseChar(key);
       }
                                                                      
       return;
}

//array con i caratteri dell'alfabeto italiano
char Vigenere::ALPH[21] = {'A','B','C','D','E','F','G','H','I','L','M','N','O','P','Q','R','S','T','U','V','Z'};

//converte tutte le lettere in maiuscolo
string Vigenere::StringToUpper(string convstr){
       for(int i = 0; i < convstr.length(); i++){
               convstr[i] = toupper(convstr[i]);
               }
       return convstr;
}

//copia il contenuto di un file di testo nella stringa text
void Vigenere::LoadText(string filename){
       ifstream f; //crea uno stream di input
              
       f.open(filename.c_str()); //apre il file specificato in filename e lo associa allo stream di input
       if(!f.is_open()){
            cout << "Errore nell'apertura del file: " << filename << " !" << endl;
            return;
       }
       
       while(!f.eof()){
            getline(f,text); //legge il contenuto del file e lo mette nella variabile text
            text = StringToUpper(text);
            if(!text.empty())
            text = EraseChar(text);
       }
      
      f.close();
}                                                        

//elimina i caratteri speciali, tenendo solo le lettere dell'alfabeto italiano (contenute in ALPH)
string Vigenere::EraseChar(string str){
       str = StringToUpper(str); 
       for(int i = 0; i < str.length(); i++){
       bool inAlph = false;
            for(int j = 0; j < 21; j++){
                    if(str[i] == ALPH[j]){
                    inAlph = true;
                    break;
                    }
            }
            if (!inAlph) {
            str.erase(i,1);
            i--;
            }
       }
       
       return str;
}   

//parametri: file da criptare, file dove salvare il testo criptato
void Vigenere::encode(string plaintext, string encoded_f){
       ofstream f; // stream di output
       string line; 
              
       TextErase();
       LoadText(plaintext);
       
       f.open(encoded_f.c_str());
       if(!f.is_open()){
           cout << "Errore nella creazione del file: " << encoded_f << " !" << endl;
           return;
       }
       
       int index_k[key.length()];  //array degli indici della chiave           
       for(int i = 0; i < key.length(); i++){
               for(int j = 0; j < 21; j++){          
                       if(key[i] == ALPH[j])
                       index_k[i] = j;
               }        
       }
       
       int index_t[text.length()];  //array degli indici del testo
       for(int i = 0; i < text.length(); i++){
               for(int j = 0; j < 21; j++){
                       if(text[i] == ALPH[j])
                       index_t[i] = j;
               }        
       }
             
       //calcola la lettera codificata
       for(int i = 0; i < text.length(); i++){
               line += ALPH[(index_k[i%key.length()] + index_t[i])%21];
       }
       f << line;
       f << "\n";
       f.close();
       return;

}

//stessi parametri della funzione encode
void Vigenere::decode(string encoded_f, string decoded_f){
       ofstream f;
       string line;
              
       TextErase();
       LoadText(encoded_f);
       
       f.open(decoded_f.c_str());
       if(!f.is_open()){
           cout << "Errore nella creazione del file: " << decoded_f << " !" << endl;
           return;
       }
       

       int index_k[key.length()];  //array degli indici della chiave
       for(int i = 0; i < key.length(); i++){
               for(int j = 0; j < 21; j++){
                       if(key[i] == ALPH[j])
                       index_k[i] = j;
               }
       }
       

       int index_c[text.length()];  //array degli indici del testo cifrato
       for(int i = 0; i < text.length(); i++){
               for(int j = 0; j < 21; j++){
                       if(text[i] == ALPH[j])
                       index_c[i] = j;
               }
       }
       
       //calcola la lettera decodificata
       for(int i = 0; i < text.length(); i++){
               line += ALPH[(index_c[i] - index_k[i%key.length()] + 21)%21];
       }
       f << line;
       f << "\n";
       f.close();
       return;
        

}

//cancella il contenuto della stringa "text"
void Vigenere::TextErase(){
       text.clear(); 
}

main.cpp

#include <iostream>
#include <stdlib.h>
#include "vigenere.h"

using namespace std;

void menu(void);

int main()
{
    string key, encoded, plaintext, decoded;
    char ch;
    
    menu();
    cin >> ch;
    
    if(ch == 1 || ch == 2){
          cout << "Chiave: ";
          cin >> key;
    }
    
    Vigenere cipher(key);
    
    switch(ch){
        case '1' : cout << "Input file: ";
                   cin >> plaintext;
                   cout << "Output file: ";
                   cin >> encoded;
                   cipher.encode(plaintext,encoded);
                   break;
                   
        case '2' : cout << "Input file: ";
                   cin >> encoded;
                   cout << "Output file: ";
                   cin >> decoded;
                   cipher.decode(encoded,decoded);
                   break;
                   
        case '3' : return 0;
                   break;
                   
    }
    return 0;                                   
}

void menu(void){
     cout << "MENU'\n";
     cout << "1.Encode\n";
     cout << "2.Decode\n";
     cout << "3.Exit\n";
     cout << "Selezionare un'opzione: ";
}

Recommended Answers

All 2 Replies

int index_k[key.length()];

You cant statically allocate a dynamic array.

if I use new to dinamically allocate an array, can I solve the problem?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.