decrypting ROT13 (vectors)
Author |
Message |
wtd
|
Posted: Fri Jan 06, 2006 12:53 am Post subject: (No subject) |
|
|
You can't turn a std::string into a character array? Someobody should tell the C++ standards committee.
code: | std::string foo = "hello";
char *bar = foo.c_str(); |
But you don't need this to deal with this problem. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Geminias
|
Posted: Fri Jan 06, 2006 12:55 am Post subject: (No subject) |
|
|
i dont know how. i intend to learn c++ inside and out, but there are just other things in my life so its taking a lot longer than i'd have wanted. plus i'm new to the whole concept of programming... i first understood what a string was just 2 weeks into september of last year.
So go easy on me... and i think the only other way i would have known how to do this would be to read all the information into an array, a big array, and then deal with it from there, but i wanted to try out vectors so i could learn them. and this experience has taught me a lot about vectors, which i wouldn't trade for anything. So to complete this experience it would be nice to be able to do this using the structure i already have. I will do it, with or without your help. |
|
|
|
|
 |
Geminias
|
Posted: Fri Jan 06, 2006 1:02 am Post subject: (No subject) |
|
|
code: |
std::string foo = "hello";
char *bar = foo.c_str();
|
this code will not compile. Invalid conversion from a constant char to a char.[/code] |
|
|
|
|
 |
wtd
|
Posted: Fri Jan 06, 2006 1:10 am Post subject: (No subject) |
|
|
Once you have that function, you can use std::transform to rot13 every character in the file. |
|
|
|
|
 |
Geminias
|
Posted: Fri Jan 06, 2006 1:12 am Post subject: (No subject) |
|
|
i dont know what your talking about
 |
|
|
|
|
 |
wtd
|
Posted: Fri Jan 06, 2006 1:12 am Post subject: (No subject) |
|
|
Contemplate.
code: | #include <iostream>
#include <vector>
#include <fstream>
#include <iterator>
using namespace std;
char rot13(char input);
int main()
{
ifstream file("file.txt");
vector<char> transformed_chars;
transform(istream_iterator<char>(file),
istream_iterator<char>(),
back_inserter(transformed_chars),
rot13);
copy(transformed_chars.begin(),
transformed_chars.end(),
ostream_iterator<char>(cout, "\n"));
return 0;
}
char rot13(char input)
{
if (input >= 'a' && input <= 'z')
{
return input + 32;
}
else if (input >= 'A' && input <= 'Z')
{
return input - 32;
}
else
{
return input;
}
} |
|
|
|
|
|
 |
wtd
|
Posted: Fri Jan 06, 2006 1:35 am Post subject: (No subject) |
|
|
Geminias wrote: i narrowed down my largest problem to the fact that this:
code: |
char word [] = *iWords_beg;
|
is an illegal initializer.
iWords_beg is not only a really bad variable name, but it's also an iterator. When you "dereference" is with *, you get a string. Not a pointer to a string. Further, this is a std::string, rather than a char array.
This examplains a lot of your other misconceptions.
Geminias wrote: 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?
No, you do not. A function can be a template. |
|
|
|
|
 |
Geminias
|
Posted: Fri Jan 06, 2006 1:44 am Post subject: (No subject) |
|
|
c++: |
#include <iostream>
#include <vector>
#include <fstream>
#include <iterator>
using namespace std;
char rot13(char input);
int main()
{
ifstream file("fuck2.txt");
vector<char> transformed_chars;
transform(istream_iterator<char>(file),
istream_iterator<char>(),
back_inserter(transformed_chars),
rot13);
copy(transformed_chars.begin(),
transformed_chars.end(),
ostream_iterator<char>(cout, "\n"));
return 0;
}
char rot13(char input)
{
if (input <= 'm' || A <= input && M >= input )
{
return input + 13;
}
else if (input >= 'm' && input <= 'z')
{
return ((input % 77) + 64);
}
else if (input >= 'M' && input <= 'Z')
{
return ((input % 109) + 96);
else
{
return input;
}
}
|
code: |
fuck.cpp:16: error: invalid initializer
fuck.cpp:18: error: `int' is not a template
fuck.cpp:18: error: missing `>' 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. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Geminias
|
Posted: Fri Jan 06, 2006 1:47 am Post subject: (No subject) |
|
|
never mind i found the errors, it compiles now it just doesn't work lol. |
|
|
|
|
 |
wtd
|
Posted: Fri Jan 06, 2006 1:56 am Post subject: (No subject) |
|
|
Are you using VC++ 6? |
|
|
|
|
 |
Geminias
|
Posted: Fri Jan 06, 2006 2:05 am Post subject: (No subject) |
|
|
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
|
Posted: Fri Jan 06, 2006 2:24 am Post subject: (No subject) |
|
|
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
|
Posted: Fri Jan 06, 2006 2:25 am Post subject: (No subject) |
|
|
Oh, and with MinGW you are using G++. Minimalist GNU for Windows. |
|
|
|
|
 |
Geminias
|
Posted: Fri Jan 06, 2006 2:38 am Post subject: (No subject) |
|
|
oh, lol. yeah it wasn't the compiler after all. |
|
|
|
|
 |
wtd
|
Posted: Fri Jan 06, 2006 2:47 am Post subject: (No subject) |
|
|
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. |
|
|
|
|
 |
|
|