Can anyone help?

This is a discussion on Can anyone help? within the C++ Programming forums, part of the General Programming Boards category; Please, can anyone tell me what is wrong with my simple encrypter? Code: #include <iostream.h> #include <fstream.h> int main() { ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21