The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

C++ help!

clsCorwinclsCorwin Registered User regular
edited January 2007 in Help / Advice Forum
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;
}

clsCorwin on

Posts

  • ecco the dolphinecco the dolphin Registered User regular
    edited January 2007
    Don't mix C/C++ "strings" if you can help it. You've got the C strings (char encodingString[80]) and C++ strings std::string and you're mixing them up. Choose one, and stick with it.

    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.

    ecco the dolphin on
    Penny Arcade Developers at PADev.net.
  • ecchiecchi Registered User regular
    edited January 2007
    Just for future reference, next time you ask for help with your code, put it in
    [code]
    
    [/code] tags. It's a lot easier to read!

    ecchi on
  • SmasherSmasher Starting to get dizzy Registered User regular
    edited January 2007
    A char is a single character, char * is an array of them. Either way, as eecc said you're better off ignoring them entirely with c++, as the string class is much easier to use. You get the advantage of being able to do operations on the whole string at once (assigning one string to another, concatinating strings together, etc.), while still being able to access and modify individual characters within the string easily.

    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 :p

    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++.

    Smasher on
  • clsCorwinclsCorwin Registered User regular
    edited January 2007
    Yea, that was a hodgepodge of me trying to fix the program very wrongly. After talking with a friend I got it fixed. everything should be string except the return type of encrypt, and newString needed to be initialized, cuz it was giving me a runtime error.

    clsCorwin on
Sign In or Register to comment.