I've created a list of strings and load a text file in it. once list is created, the program have to read it, do something and write results in a new text file.

ofstream f;
   f.open("filename.txt");
   
   string key;
   list<string> text;
   static char ALPH[21] = {'A','B','C','D','E','F','G','H','I','L','M','N','O','P','Q','R','S','T','U','V','Z'};

   int* index_t = new int[text.size()];
   int* index_k = new int[key.length()];

   for(int i = 0; i < key.length(); i++){
               for(int j = 0; j < 21; j++){          
                       if(key[i] == ALPH[j]) 
                       index_k[i] = j;  
               }        
       }
       
   for(list<string>::iterator it = text.begin(); it != text.end(); it++){
           for(int i = 0; i < it->length(); i++){
               for(int j = 0; j < 21; j++){
                       if((*it)[i] == ALPH[j])
                       index_t[i] = j;
               }
               char ch = ALPH[(index_k[i%key.length()] + index_t[i])%21]; 
               f << ch;
           }
           f << "\n";
        }
        
        f.close();

I have not even tried to implement it because I already believe is wrong. someone tell me how to change it to be able to make it work?
thank you :confused:

Your string and list are empty when you test for their size. Also, you've only opened the file. Where did you read from it?

of course this is only a piece a code, the part which doesn't work. do you want me to post the entire code? actually, I'd have written: let's suppose you have already opened a text file and stored it in the list.
the main problem is the for loop: what I'm trying to do is compare the key with ALPH to find matches, and if so update the index variable, same thing for the list. So I have to read every character of every string of the list, do something and then write results on a text file.
Can you tell me how to adjust the for loop? I do not necessarily use iterators, it was just an idea, if there is more to solve the problem should be okay.

robdb,

First, am I correct in assuming that your program is reading in the whitespace-separated words from one file, and writing out encoded versions of the words one-per-line?

If that's the case, first, you're sizing index_t array incorrectly. You don't want an index-entry per word in your file, you want an index-entry per character in your string. If you use vector<int> instead of a C-style int-array, the compiler (or the vector<> class, really) will take care of making sure it's as big as you need to store each encoded string as you encounter it. Otherwise, keep track of the currently-allocated size of index_t and whenever you get a string longer than that, reallocate it to be long enough for that string.

Other than that, what value do you store in index_k or index_t if the character isn't in your ALPH set?

Finally, since it doesn't look as though you access index_t outside of your for(i...) loop, you don't need to maintain an array of indices for the word anyway -- just compute the encoded letter and write it to your output file..

But I think maybe you're hacking away at the problem instead of thinking through each step, and what needs to happen as part of that step.

Since you've already written "read an input file into a list-of-strings" functionality, then write some kind of "write a list-of-strings to an output file" functionality, and test that if you just write out the same list you read in, you get what you expect in your output file.

Then add the "do something part", which might either replace each string in your list, or create a new list based on the starting list, depending on your needs.

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.