char vs string

Discussion in 'C++' started by azygous420, Mar 10, 2007.

  1. azygous420

    azygous420 New Member

    Joined:
    Mar 10, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    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.
    Code:
    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.close();
            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)
            {
                    file.get(ch);
                    while (!file.fail())
                    {
                            cout << hex << ch;
                            n = n++;
                            file.get(ch);
                    }
            }
            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:
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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.
     
  3. azygous420

    azygous420 New Member

    Joined:
    Mar 10, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    k ill check that out thanks
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice