Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   char vs string (http://www.go4expert.com/forums/char-vs-string-t3343/)

azygous420 11Mar2007 00:40

char vs string
I'm new to C++ and can only write really simple dos programs. I was attempting to make a hex encoder and decoder just to get me to learn C++ better.

int main(int argc, char* argv[])
        int code;
        int test;
        int n;
        test = 0;
        int maintest;
        maintest = 0;
        float word;
        char yn;
        n = 0;

        while (maintest == 0)
        fstream file("decode.txt", ios::out);
        char ch, password[51];
        cout << "\nEnter a password (max 8 letters): ";
        cin >> password;
        file << password;
        file.open("decode.txt", ios::in);
        while (test != 1)
                cout << "\nHit 1 to Encode, Hit 2 to Decode: ";
                cin >> code;
                if (code == 1 || code == 2)
                        test = test++;
        if (code == 1)
                while (!file.fail())
                        cout << hex << ch;
                        n = n++;
        if (code == 2);

        cout << "\nFinish? (Y or N)";
        cin >> yn;
        switch (yn)
                case 'y': maintest = 1;
                case 'n': break;
                default : cout << "\nError";
        return 0;

That's all i have so far, I'm struggling to figure out how to decode hex. I had it encoding for a bit but something went wrong and i don't know the peramiters for using the hex command. It keeps giving me an error about chars can be converted to string, and i can't figure out how to do something like "cin >> word" if word is a string. it wont imput, only chars will. thanks :cool:

DaWei 11Mar2007 17:40

Re: char vs string
Your question is not entirely clear. A string could be the ordinary char array with a nul terminator, or it could be the string class. I presume array because I see no 'string' declaration. You should also give the message exactly, not a paraphrase. At any rate, you should check out the operation of the extraction operator, >>, for parsing as well as for what generates errors and what those errors mean for subsequent operation. Always test your functions for success. They do not guarantee to work properly under all conditions, but to inform you if you abuse them or if they fail for other reasons. Ask them. One common problem with cin is unexpected (at least to the novice that hasn't read the docs) input remaining in the buffer and garfling up the next use. Another is inability to convert input to a specified type, which breaks the stream, which then will not work again until fixed.

As far as outputting hex, see iomanip and things like setbase. I can't tell if you've been there, because your snippet doesn't show your #includes.

azygous420 12Mar2007 12:47

Re: char vs string
k ill check that out thanks

All times are GMT +5.5. The time now is 14:43.