Thread: Can anyone help?

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    9

    Unhappy Can anyone help?

    Please, can anyone tell me what is wrong with my simple encrypter?
    Code:
    #include <iostream.h>
    #include <fstream.h>
    int main()
    {
        char string[256], note2[256];
        int b;
        cout<<"Write your note:"<<endl;
        cin.getline(string, 256, '\n');
        cout<<"loading..."<<endl;
        for(int a=0;a<256;a++)
        {
            b=(int)string[a];
            note2[a]=(char)(b+15);
        }
        ofstream a_file("note.txt");
        a_file<<note2;
        a_file.close();
        cout<<"Your note is now encrypted into the file: 'note.txt'"<<endl;
        cin.get();
        return 0;
    }
    Thank you!
    -KaibaFan321

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > for(int a=0;a<256;a++)
    1. You don't use strlen() to only output the number of chars you typed in
    2. There is no \0 at the end of the string you create
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    9
    I understood the first answer, but I didnt understand the second, I am a complete noob.
    thanks
    -KaibaFan321

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    note2[a] = '\0';

    When you get to the end of the string
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    9
    O, Thanks!
    -KaibaFan321
    PS. My teachers are going to hate this program!

Popular pages Recent additions subscribe to a feed