Ok, so I'm coming to C++ from Java, and how C++ handles strings is really pissing me off. I have to write this problem to encode a string by an inputted int value, and then spit out the new encoded phrase. The logic of my code is right, but having to try and cobble it together to get C++ to like the strings is killing me. Help point me in the right direction.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class REDCOATS
{
private:
char encrypt(int x, int encodingInt, char encodingString[])
{ //will add the int value to the char at position x and then return the new string
encodingString[x] += encodingInt;
return encodingString[x];
}
public:
char getMessage() //gets the string to be encoded
{
char encodingString[80];
cout << "Enter a string to be encoded.\n";
cin.getline(encodingString, 80);
return encodingString[80];
}
int getCode() //gets the int to encode the string
{
int encodingInt;
cout << "Enter an integer to encode the string.\n";
cin >> encodingInt;
return encodingInt;
}
char encryptAll()
{
string message = getMessage();
strcpy(encodingString, message);
int encodingInt = getCode();
int x;
char newString[80];
for(x = 0; encodingString[x] != 0; x++)
{ //calls encrypt unless it finds a space
if(encodingString[x] == 32)
newString[x] = encodingString[x];
else
newString[x] = encrypt(x, encodingInt, encodingString);
cout << newString[x];
}
// strcpy(newString, encodingString);
return newString[];
}
void printOut(char newString[])
{
cout << endl;
cout << newString[];
}
};
int main()
{
REDCOATS redcoats;
redcoats.printOut( redcoats.encryptAll() );
return 0;
}
Posts
I recommend the C++ strings for you in this application.
They are two different things, and the way you're using them seems like you're confused about them.
For example, in encryptAll(), you're converting into a C++ string from getMessage(), and then relying on an implicit type cast (does that even compile? I don't think it should - last I looked, std::string didn't have a const char * operator overload) to strcpy over the C++ string to a C string. This raises the question of what the C++ string was doing there in the first place if the start with a C string and the end was a C string.
string message = getMessage(); // <- string message is a C++ string. Reading your code, it seems like you want getMessage() to return a C string.
strcpy(encodingString, message); // <- you only strcpy into C strings, so your intent appears to end up with a C string from a C string by temporarily converting to a C++ string half way through. It's almost like me typing:
long someFunction()
{ return 0L; }
double tmpVariable = someFunction();
long someOtherVariable = tmpVariable; // What's with the conversion to a double?
Also, you're confusing characters with strings (you've got getMessage() returning a character, instead of the message).
You might want to brush up on the difference between C strings and C++ std::strings, and then stick to C++ std::strings. I believe that the C++ std::strings behave far more like the Java equivalent, but not knowing Java, I couldn't say with absolute certainty.
I'm sorry I can't be more specific and point to any specific place in your code and say that that's the problem, but it's all a bit confused and very strange.
Other assorted issues with your code, for learning's sake:
You use encodingString in the encryptAll() function, but it's not a class member and you don't declare it in that function.
In getMessage(), returning encodingString[80] is wrong for two reasons. First, you're returning the 81st character, not the entire string. Second, since encodingString only has 80 characters, accessing position 80 has undefined results. And unlike Java, c++ won't be nice and tell you you've gone out of bounds.
This one isn't wrong so much as a style issue, but you should use character literals instead of numbers when comparing elements of a string. Thus, the end of the string is '\0' instead of 0 and a space is ' ' instead of 32. It'll work either way, it's just easier to read and understand with the literals, since not even programmers necessarily memorize the ascii character chart
On the plus side your logic is fine, and it's obvious you know how to program in some language, so you've got a leg up there on a lot of people learning c++.
See how many books I've read so far in 2010