Posted: Thu Jan 05, 2006 1:21 pm Post subject: 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.
void decrypt (vector<int>::iterator iWords_beg,
vector<int>::iterator iWords_end,
int size)
{
for (int i = 0 ; i <= size ; i++)
{
char word [] = *iWords_beg [i];
for (int s = 0 ; int < (countof ( word )) ; s++)
{
if ((int) word [s]) <= 75 || 97 <= (int) word [s]) <= 122 )
{ word [s] = char (((int) word [s]) + 13 )); }
else if ((int) word [s]) < 97)
{ word [s] = char ((((int) word [s]) % 77) + 64); }
else if ((int) word [s]) >= 97)
{ word [s] = char ((((int) word [s]) % 109) + 96); }
}
*iWords_beg[i] = word;
}
}
int main ()
{
ifstream file("file.txt");
vector<string> words =
vector<string> (istream_iterator<string>(file),
istream_iterator<string>());
decrypt (words.begin(), words.end(), words.size() );
note: i couldn't find anything on accessing individual characters in a vector<string> so i decided to transfer it to an array as a quick fix. also the "countof" constant is yet another quick fix.
wtd
Posted: Thu Jan 05, 2006 1:30 pm Post subject: (No subject)
The compiler will infer the types provided, so you can call decrypt the same way you do now.
Geminias
Posted: Thu Jan 05, 2006 1:37 pm Post subject: (No subject)
i haven't gotten to the template part in my sams teach yourself c++ in 21 days lol. (more like teach me in 5 months)
does the template automatically change a type to the type of variable? cause i see you named it Iter, but no where did you tell it Iter = vector<string>::iterator
wtd
Posted: Thu Jan 05, 2006 1:46 pm Post subject: (No subject)
void decrypt (vector<int>::iterator iWords_beg,
vector<int>::iterator iWords_end,
int size)
{
for (int i = 0; i <= size; i++)
{
// Switch vector data to array.
char word[] = *iWords_beg[i];
for (int s = 0; int < countof(word); s++)
{
if ((int)word[s]) <= 75 || 97 <= (int)word[s]) <= 122)
{
word[s] = char(((int)word[s]) + 13 ));
}
else if ((int)word[s]) < 97)
{
word[s] = char((((int)word[s]) % 77) + 64);
}
else if ((int)word[s]) >= 97)
{
word[s] = char((((int)word[s]) % 109) + 96);
}
}
*iWords_beg[i] = word;
}
}
int main ()
{
ifstream file("file.txt");
vector<string> words =
vector<string> (istream_iterator<string>(file),
istream_iterator<string>());
decrypt(words.begin(), words.end(), words.size());
Now that we can read this code, we can find the errors and just plain mistakes.
code:
else if ((int)word[s]) < 97)
Let's count parentheses! I see two open parentheses, but three closing parentheses. Welcome to the wonderful world of syntax errors.
I also see this with things like:
code:
word[s] = char(((int)word[s]) + 13));
Now, with respect to other errors...
You do realize that characters are integral types?
code:
static_cast<int>(some_char_var) > 97
Can be rewritten as:
code:
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
Posted: Thu Jan 05, 2006 1:51 pm Post subject: (No subject)
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
Posted: Thu Jan 05, 2006 1:54 pm Post subject: (No subject)
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
Posted: Thu Jan 05, 2006 1:57 pm Post subject: (No subject)
Well, if you have the beginning and end of a vector, then looping over it...
Posted: Thu Jan 05, 2006 2:03 pm Post subject: (No subject)
Quote:
fuck.cpp:11: error: expected `,' or `...' before '>' token
fuck.cpp: In function `void decrypt(Iter, Iter)':
fuck.cpp:17: error: `size' undeclared (first use this function)
fuck.cpp:17: error: (Each undeclared identifier is reported only once for each f
unction it appears in.)
fuck.cpp:19: error: initializer fails to determine size of `word'
fuck.cpp:21: error: `int' is not a template
fuck.cpp:21: error: missing `>' 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<std::string*, std::vector<std::string, std::allocator<std::string> >
>, __gnu_cxx::__normal_iterator<std::string*, std::vector<std::string, std::allo
cator<std::string> > >, size_t)'
i'm worried about this "int is not a template" thing... i can't have an "int size" as an argument?
wtd
Posted: Thu Jan 05, 2006 2:03 pm Post subject: (No subject)
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
Posted: Thu Jan 05, 2006 4:38 pm Post subject: (No subject)
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
Posted: Fri Jan 06, 2006 12:46 am Post subject: (No subject)
i narrowed down my largest problem to the fact that this:
code:
char word [] = *iWords_beg;
is an illegal initializer.
code:
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 << *iWords_beg
it displays a lovely little string, (the right string and everything)
heres my code so far, and i didn't use templates because dont you have to make a struct or a class to implement them?
for(int s = 0 ; int < ((countof ( word )) ; s++) { if( word [s] <= 75 || 97 <= word [s] <= 122)
word [s] = char((((int) word [s]) + 13));
elseif( word [s] < 97)
word [s] = char((((int) word [s]) % 77) + 64);
elseif( word [s] >= 97)
word [s] = char((((int) word [s]) % 109) + 96);
}
*iWords_beg = word;
} }
int main () {
ifstream file("file.txt");
vector<string> words =
vector<string> (istream_iterator<string>(file),
istream_iterator<string>());