
-----------------------------------
Geminias
Thu Jan 05, 2006 1:21 pm

decrypting ROT13 (vectors)
-----------------------------------
hi, i need some help with this code... this vector class is giving me a really rough time.  

in case you don't know ROT13 just means rotate 13, so any letter you pick in the alphabet you just count upward 13 letters.  this code should take a text file encrypted with ROT13 and decrypt it to console.

 

#include 
#include 
#include 
#include 
#include 
#define countof( array ) ( (sizeof( array )/sizeof( array[0] )) - 1 )

using namespace std;

void decrypt (vector::iterator iWords_beg,
              vector::iterator iWords_end,
              int size)
{


  
         for (int i = 0 ; i  97

Can be rewritten as:

some_char_var > 'a'

It is now much simpler and easier to understand.

If you are going to use casts, though, do not use C-style casts. Use things like "static_cast".

-----------------------------------
Geminias
Thu Jan 05, 2006 1:51 pm


-----------------------------------
no i didnt know you could do that in c++, i thought that was another turing thing.  (the chars being integers)  

well thanks, your like a portable debugger lol.   i'll hopefully be able to fix up this code in no time and post a ROT13 decrypter! yay!

-----------------------------------
Geminias
Thu Jan 05, 2006 1:54 pm


-----------------------------------
by the way, what is your stance on how i grabbed each individual character.  did i miss something in the vector class that could have made life easier?  or is the vector class just not optomized for things like this?

-----------------------------------
wtd
Thu Jan 05, 2006 1:57 pm


-----------------------------------
Well, if you have the beginning and end of a vector, then looping over it...

for (; beginning_of_vector != end_of_vector; beginning_of_vector++)
{
   cout ' to terminate the template argument list
fuck.cpp:21: error: expected primary-expression before "int"
fuck.cpp:21: error: expected `;' before "int"
fuck.cpp:21: error: expected primary-expression before "int"
fuck.cpp:21: error: expected `)' before "int"
fuck.cpp:21: error: declaration does not declare anything
fuck.cpp:21: error: name lookup of `s' changed for new ISO `for' scoping
fuck.cpp:21: error:   using obsolete binding at `s'
fuck.cpp:21: error: expected `;' before ')' token
fuck.cpp: In function `int main()':
fuck.cpp:43: error: no matching function for call to `decrypt(__gnu_cxx::__norma
l_iterator, __gnu_cxx::__normal_iterator, size_t)'



i'm worried about this "int is not a template" thing... i can't have an "int size" as an argument?

-----------------------------------
wtd
Thu Jan 05, 2006 2:03 pm


-----------------------------------
C++ is not Turing.  People actually use it.  ;)

Thus if you find yourself thinking, "there has to be a better way to do this", there probably is.

-----------------------------------
wtd
Thu Jan 05, 2006 4:38 pm


-----------------------------------
So, you're trying to ROT13 a string?

This is pretty simple, and it's easier if you break it down.

Write a function which performs a ROT13 transformation on a character.  Then to ROT13 an entire file, just use the STL functions to apply that function to every character in the file.

-----------------------------------
Geminias
Fri Jan 06, 2006 12:46 am


-----------------------------------
i narrowed down my largest problem to the fact that this: 


char word [] = *iWords_beg;  

is an illegal initializer.  

char word [25];

word = *iWords_beg;  


*iWords_beg is an incompatible type.  However.. i do not understand this being that if i just ask for a: 

cout ' to terminate the template argument list
fuck.cpp:18: error: expected primary-expression before "int"
fuck.cpp:18: error: expected `;' before "int"
fuck.cpp:18: error: expected primary-expression before "int"
fuck.cpp:18: error: expected `)' before "int"
fuck.cpp:18: error: declaration does not declare anything
fuck.cpp:18: error: name lookup of `s' changed for new ISO `for' scoping
fuck.cpp:18: error:   using obsolete binding at `s'
fuck.cpp:18: error: expected `;' before ')' token


is my compiler not fully equipped with the stl?  keep in mind i'm not using gcc right now.

-----------------------------------
Geminias
Fri Jan 06, 2006 1:47 am


-----------------------------------
never mind i found the errors, it compiles now it just doesn't work lol.

-----------------------------------
wtd
Fri Jan 06, 2006 1:56 am


-----------------------------------
Are you using VC++ 6?

-----------------------------------
Geminias
Fri Jan 06, 2006 2:05 am


-----------------------------------
no, mingw.  it works now, all is well... now i'll have to research those functions you gave me.  

part, well actually, the biggest problem i have is i can't find the source of "stl::copy ()"  or "stl::sort ()"  and without it i found it difficult to pass the iterators into my decrypt function and manipulate the vector.  partly because as you saw i had to guess what type they were, (the iterators) .   

one question:  how do i keep spaces?  i found that this program eliminates the spaces in its output... how would i keep them?

-----------------------------------
wtd
Fri Jan 06, 2006 2:24 am


-----------------------------------
Yes, that has to do with the default way istreams work.  I had failed to take this into account.

As for figuring out how functions like copy and transorm work:

http://www.sgi.com/tech/stl/

It's all there.  The place to start would be the table of contents.

-----------------------------------
wtd
Fri Jan 06, 2006 2:25 am


-----------------------------------
Oh, and with MinGW you are using G++.  Minimalist GNU for Windows.

-----------------------------------
Geminias
Fri Jan 06, 2006 2:38 am


-----------------------------------
oh, lol.  yeah it wasn't the compiler after all.

-----------------------------------
wtd
Fri Jan 06, 2006 2:47 am


-----------------------------------
My rot13 function's body was also all wrong.  My brain is apparently having some issues.  Still, my advice about the general stuff... dead on.

-----------------------------------
Geminias
Fri Jan 06, 2006 3:06 am


-----------------------------------
i didn't realize you're rot13 function was intended to be a rot13 function.  i thought you just dug something up as an example.  

yes, it was entirely flawed here's a working source:  


#include 
#include 
#include 
#include 

using namespace std;

char rot13(char input);

int main()
{
   ifstream file("decrypt.txt");

   vector transformed_chars;

   transform(istream_iterator(file),
             istream_iterator(),
             back_inserter(transformed_chars),
             rot13);

   copy(transformed_chars.begin(),
        transformed_chars.end(),
        ostream_iterator(cout));

   return 0;
}

char rot13(char input)
{
   if (input = 'm' && input = 'M' && input 