I have to write an application in which a text file is read and stored in a vector of strings

vector<string> text;

After that I have to compare each character of text loaded from a file with an array of char with default content

char ARRAY[21] = {... ... ...}

so I thought something like that

for(int i = 0; i < text.size(); i++){
               for(int j = 0; j < 21; j++){
                       if(text[i] == ARRAY[j])
                       index_t[i] = j;
               }        
       }

at compile time I get the following error:
no match for 'operator==' in '(((std::vector<std::string, std::allocator<std::string> >*)
how can I fix that?

>> if(text == ARRAY[j])

That is trying to compare an entire string with a single character. For example: if( "Hello" == 'A') ... it just doesn't make any sense.

I think what you have to do is loop through each character in text to see which ones are contained in ARRAY.

for(int j = 0; j < text[i].length(); j++)
{
  if( strchr(ARRAY, text[i][j]) )
  {

     // do something
  }
}

I will be more clear: I have to compare each character of each string in vector. If the comparison is positive, I associate the array index corresponding to the letter to a variable. For example, if the character of a string in vector is 'R', the index variable assumes a value of 15, as ARRAY [15] = R.

That's exactly what strchr() will give you. It returns a pointer to the fist instance in ARRAY that contains the character in text[j]. Now to get the integer index just simple subtraction -- the pointer returned by strchr() - ARRAY.

char* ptr;

if( (ptr = strchr(ARRAY, text[i][j]) )
{
    index_t[i] = ptr - ARRAY;
}

I've tried to use strchr as you suggested, but it doesn't work. here's the complete code, maybe it could be helpful

//vigenere algorithm with Italian alphabet

#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <string.h>

using namespace std;

class Vigenere
{
public:
    string key;
    vector<string> text;
           
    Vigenere(string str){
       key = str;
       key = StringToUpper(key);
       if(!key.empty()){
           key = EraseChar(key);
       }
                                                                      
       return;
    }
    
    //converts every letter to uppercase
    string StringToUpper(string convstr){
       for(int i = 0; i < convstr.length(); i++){
               convstr[i] = toupper(convstr[i]);
               }
       return convstr;
    }
    
    //load the content of a file into text
    void Vigenere::LoadText(string filename){
           ifstream f; 
           string line;
                  
           f.open(filename.c_str()); 
           if(!f.is_open()){
                cout << "Error opening file: " << filename << " !" << endl;
                return;
           }
           
           while(getline(f,line)){
                line = StringToUpper(line);
                if(!line.empty()){
                   line = EraseChar(line);
                   text.push_back(line);
                }               
           }
          
          f.close();
    }
    
    //deletes all characters not in ALPH
    string EraseChar(string str){
       char ALPH[21] = {'A','B','C','D','E','F','G','H','I','L','M','N','O','P','Q','R','S','T','U','V','Z'};
       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; // don't need to keep checking against the rest of ALPH
                    }
            }
            if (!inAlph) {
            str.erase(i,1);
            i--;
            }
       }
       return str;
    }
    
    void encode(string plaintext, string encoded_f){
       ofstream f; 
       char ALPH[21] = {'A','B','C','D','E','F','G','H','I','L','M','N','O','P','Q','R','S','T','U','V','Z'};
       
       TextErase();
       LoadText(plaintext); 
              
       f.open(encoded_f.c_str());
       if(!f.is_open()){
           cout << "Error creating file: " << encoded_f << " !" << endl;
           return;
       }
    
    /****************************************************************************
    * each letter in ALPH is associated with an index, so A-->0 B-->1 and so on.
    * now it checks every characters of every string of text in order to find
    * a correspondance. the same for the key.
    * for example:
    * key: ROBERT
    * text: PRINT
    * the first letter in text is P, which corrispond to ALPH[13], so index_t[0]=13
    * the first letter in key is R, which corrispond to ALPH[15], so index_k[0]=15
    * the first encoded letter is:
    * ALPH[(index_t[0]+index_k[0])%21] = ALPH[7] = H
    ****************************************************************************/      
       
       int passwdcnt = 0;
       int* index_t = new int[text.size()];
       int* index_k = new int[key.length()];
       
       for(int i = 0; i < text.size(); i++){
           for(int j = 0; j < text[i].size(); j++){
               char* ptr_k;
               char* ptr_t;
               if(ptr_t = strchr(ALPH,text[i][j])){
                      index_t[j] = ptr_t - ALPH;
               }
               if(ptr_k = strchr(ALPH,key[passwdcnt])){
                      index_k[j] = ptr_k - ALPH;
               }
               
               char ch = ALPH[(index_t[j] + index_k[j])%21];
               f << ch;
               passwdcnt++;
               if(passwdcnt > (key.length()-1))
               passwdcnt = 0;
           }
           f << "\n";
       }
       f.close();
       delete [] index_t;
       delete [] index_k;
       return;
    }
                        
    void TextErase(){
       text.clear(); 
    }
};

int main()
{
    string key, encoded, plaintext, decoded;
    cout << "Key: ";
    cin >> key;
    
    Vigenere cipher(key);
    
    cout << "Input file: ";
    cin >> plaintext;
    cout << "Output file: ";
    cin >> encoded;
    cipher.encode(plaintext,encoded);
                                         
}
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.