Oppure

Loading
01/10/09 14:16
oretovalley
Ciao a tutti stavo programmando in C++ e devo effettuare questo confronto:

if(chiave[indice_chiave] != "-" && chiave[indice_chiave + 1] == '-') {		 
}


ma il dev-c mi da un errore:

ISO C++ forbids comparison between pointer and integer

non riesco a capire il motivo. se invece metto invece delle doppie virgolette, l'apice il compilatore compila perfettamente ma la condizione non si verifica mai :(

inoltre non riesco ad unire sotto una string queste due stringhe:

temp2=chiave[indice_chiave] + chiave[indice_chiave + 1];


stampando temp2 ottengo f...

come posso fare?

il codice per esteso è questo:

#include<iostream>
#include<fstream>
#include<string>
#include <sstream>
#define nomefile "file1.txt"
#define nomefile2 "file2.txt"
#define file_chiave "chiave.txt"
using namespace std;

void crea_file() {
ofstream write(nomefile);
int numero = 0;
string parola = "";
cout << "Inserisci il numero delle parole da inserire: ";
cin >> numero;
for(int i=0;i < numero;++i) {
cout << "Inserisci la " << i + 1 << " parola: ";
cin >> parola;
transform( parola.begin(),parola.end(), parola.begin(),(int(*)(int)) toupper);
write << parola << endl;
}
}

void visualizza_file() {
ifstream read(nomefile);
string parola = "";
while(read>>parola) { cout << parola << endl; }
}

void cripta_file() {
    string alfabeto = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    string parola_criptata = "";
    string parola = "";
    int num_random = 0;

    int go = 0;
    ifstream read(nomefile);

    if(!read) {

        cout << "Il file non esiste. Crearlo e riprovare" << endl;

    } else {

        ofstream write(nomefile2);
        ofstream write_key(file_chiave);

        while(read >> parola) {

            for(int indice_testo = 0; indice_testo < parola.length() + 1; indice_testo++) {
                for(int indice_parola = 0; indice_parola < 26; indice_parola++) {
                    if(alfabeto[indice_parola] == parola[indice_testo]) {
                        num_random = rand() % 26;
                        if(indice_testo > 0) {
                            write_key << "-" << num_random;
                        } else {
                            write_key << num_random;
                        }
                        for(int indice_sottrazione = 0; indice_sottrazione < indice_parola + 1; indice_sottrazione++) {
                            num_random -= 1;
                            if(num_random < 0) {
                                num_random = 25;
                            }
                        }
                        parola_criptata += alfabeto[num_random];
                        break;
                    }
                }
            }

            write << parola_criptata << endl;
            write_key << endl;
            parola_criptata = "";
            num_random = 0;
        }
        read.close();
        write.close();
        remove(nomefile);
        rename(nomefile2,nomefile);
    }

}

void decripta_file() {
    
    string alfabeto = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    string parola_decriptata = "";
    string parola = "";
    string parola_chiave = "";
    string chiave = "";
    int indice_chiave = 0;
    int num_random = 0;
    string temp = "", temp2 = "";

    ifstream read(nomefile);
    ifstream read_key(file_chiave);

    if(!read || !read_key) {

        cout << "Il file non esiste. Crearlo e riprovare" << endl;
                    } else {

        ofstream write(nomefile2);

        while(read >> parola && read_key >> chiave) {

            for(int indice_testo = 0; indice_testo < parola.length(); indice_testo++) {
for(int indice_parola = 0; indice_parola < 26; indice_parola++) {
if(alfabeto[indice_parola] == parola[indice_testo]) {
if(indice_chiave + 1 < chiave.length()) {
if(chiave[indice_chiave] != '-' && chiave[indice_chiave + 1] != '-') {
for(num_random = 10;num_random < 26;num_random++) {
                                 stringstream out;
                                 out << num_random;
                                 temp = out.str();                                 
                                 temp2=chiave[indice_chiave] + chiave[indice_chiave + 1];
                                    system("pause";);
cout << temp2;
                                    if(temp == temp2) {
indice_chiave += 3;
break;
}
}
} else {
if(chiave[indice_chiave] != '-' && chiave[indice_chiave + 1] == '-') {        
for(num_random = 0;num_random < 10;num_random++) {
temp = num_random;
                                      temp2 = chiave[indice_chiave];
                                        if(temp == temp2) {
indice_chiave += 2;
break;
}
}
}
}
} else {
if(chiave[indice_chiave] != '-') {
for(num_random = 0;num_random < 10;num_random++) {
temp = num_random;
                                 temp2 = chiave[indice_chiave];
                                    if(temp==temp2) {
indice_chiave += 2;
break;
}
}
}
}
for(int indice_sottrazione = 0; indice_sottrazione < indice_parola + 1; indice_sottrazione++) {
num_random -= 1;
if(num_random < 0) {
num_random = 25;
}
}
parola_decriptata += alfabeto[num_random];
break;
}
}
}

            write << parola_decriptata << endl;
            temp = "";
     temp2 = "";
         parola_decriptata = "";
        }
        read.close();
        write.close();
        read_key.close();
        remove(file_chiave);
        remove(nomefile);
        rename(nomefile2,nomefile);
    }
}

int menu() {
    int scelta = 0;
    do {
     system("cls";);
        cout << "Digita 1 per creare le parole in un file" << endl;
        cout << "Digita 2 per visualizzare le parola" << endl;
        cout << "Digita 3 per criptare le parole" << endl;
        cout << "Digita 4 per decriptare le parola" << endl;
        cout << "Digita 5 per uscire" << endl;
        cout << endl << "Inserisci la tua scelta: ";
        cin >> scelta;
    }while(scelta < 0 || scelta > 5);
    system("cls";);
    return scelta;
}

int main() {
    int scelta = 0;
    do {
        scelta = menu();
        switch(scelta) {
            case 1:
                crea_file();
                break;
            case 2:
                visualizza_file();
                break;
            case 3:
                cripta_file();
                break;
            case 4:
                decripta_file();
                break;
        }
        system("pause";);
    }while(scelta != 5);
}

Ultima modifica effettuata da oretovalley 01/10/09 14:39
aaa
01/10/09 15:26
theprogrammer
Postato originariamente da oretovalley:

Ciao a tutti stavo programmando in C++ e devo effettuare questo confronto:

if(chiave[indice_chiave] != "-" && chiave[indice_chiave + 1] == '-')



Quelli che testi sono caratteri ... devi sempre usare i singoli apici

if(chiave[indice_chiave] != '-' && chiave[indice_chiave + 1] == '-')


Questo dal punto di vista sintattico. Se poi la condizione non si verifica mai, e' un altro discorso e devi capire il perche' controllando l'algoritmo che hai usato.

inoltre non riesco ad unire sotto una string queste due stringhe:

temp2=chiave[indice_chiave] + chiave[indice_chiave + 1];



Quelle due NON sono stringhe ma caratteri.

Scrivi

temp2 += chiave[indice_chiave];
temp2 += chiave[indice_chiave + 1];

Ultima modifica effettuata da theprogrammer 01/10/09 15:33
aaa