It prints out three a's instead of only 1. Does anyone see a quick fix to this? I tried changing it to find and changing the j + 1 to j + 3 but both of those changes made it print out a62.
I'm kind of confused my self as the person who gave it to me never explained it but I know it loops through the phrase that the user types in looking for the characters specified and changes it to the string on the 3rd line. Thats what I know must happen becuase thats what my Java program does(I'm trying to make this program in C++ and Java), but the problem is that the 2nd one makes 3 a's instead of 1 a. Also if I use getline(cin, pdphrase); its supposed to get the whole line even if there are no spaces. It won't work in this program though. Does it have to do with the way the max length of the string is calculated?
...I have the impression you want to do an easy encryption thing like substitution of letters in a text - am I right? If you don't understand the library you are using perhaps you better work it out yourself? something like this:
Alright I'll try and figure out a new way to do it. By the way, your program crashes after it prints out original clear text: hello world. I don't know if that was intended or not, but I'm just pointing that out.
I took it out and it still crashes. I've come to the conclusion that DevC++ sucks. I have programs that use the same syntax and they don't do the same thing, it says there are no matching function calls when there are...and suggestions for a new IDE with a different compiler?
..u are right - c++ really sucks!! its old and the compilers are really lame in supporting usefull error messages...
and all this stuff like pointers adresses delete clauses - with a modern IDE u do not even have to think about that things at all. I mean hello? did these c++ guys ever hear about a garbage collector? its all stupid automatism that these old compilers want you to do when developing with c++.
my advise: go and get yourself a modern IDE and develop with an up to date programming language like C#! IDE is for free: Visual Studio Express 2008 or 2010 ;)
because sometimes someone just decides u ve to do it in c++...
but if one has free choice of language and is sane he will allways do it with c# - its default choice.
and by the way: c++ is not the name of a religion - its just a tool. ... and there are better tools. modern languages really support u as a developer by giving precise error messages and doing all that stuff like garbage collection... so u can concentrate on your work and that are these things the computer can not do - like develop tricky algorithms - not like collect garbage!!
I actually have to program this in C++. I'm doing a science fair project and for the project I have to make programs in Java and C++ that do the exact same thing. It sucks though because I'm a lot better at Java than C++ so the program in C++ are a lot harder for me to make. The only program that I have exactly the same is a program that does the distance formula. I have to make a C++ program that encrypts and decrypts stings which is what I'm trying to figure out here, and I have to make a calculator, I have squaring numbers, getting the square root, and the other basic operations but i have to add doing sine cosine and tangent and the inverses of those in the C++ program. All of these programs are done in Java but I just need to figure them out in C++.
cdoubleplus
you don't need to collect the garbage by yourself at most of the time by using C++
if you do need to do that, it means you don't know how to use C++ effectively
language itself is innocent, before you blame the language, maybe you should
ask yourself : the language fail me or I fail to take a good use of the language
besides, garbage collection is not that difficult with the help of destructor and smart pointer
If you're wondering why the code crashes, it's that by using char*, you opened up a possibility of a segmentation fault, which happened at line 27. I don't think it's the ancient MinGW compiler's fault this time.
EDIT: @OP: I'd be curious to see your whole code and the string you're running through it.
You should use find(), instead of find_first_of(). The former searches for a whole string, the latter searches for any character present in the string. ;)
I would do the encryption/decryption differently, however that's not too important. For reference, though, I'll put it down here.
I would define two maps, or one map and two functions to change it. One would have the alphabet as keys and the encrypted letters as values, the other would have the encrypted letters as keys and the decrypted letters as the values. From there, it's a simple single for loop without any ifs. This could be tricky for someone unfamiliar with maps, though.
i don't know what do you want to do
do you want to map 'a' to 'aaa' , 'b' to '6' or somthing like that?
maybe "map" or "multimap" would be a good choice
just some easy example
1 2 3 4 5 6 7 8 9
std::map<char, char> dictionary;
dictionary.insert(std::make_pair('a', 'A')); //in this example, 'a' is the key, 'A' is the value
dictionary.insert(std::make_pair('b', 'B'));
dictionary.insert(std::make_pair('4', 'r'));
std::map<char, char>::iterator it = dictionary.find('a');
cout<<it->first<<", "<<it->second<<"\n";
it = dictionary.find('4');
cout<<it->first<<", "<<it->second<<"\n";
you could build one map to encryption and one to decryption like Albatross told you
Pretty easy and don't need to do any garbage collection jobs like cdoubleplus said
if you need to map a key to different values, you could use multimap