Thread: Making a simple encrypter

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

    Making a simple encrypter

    This encrypter only has problems when your message has a space in it:
    Code:
    #include <iostream.h>
    #include <fstream.h>
    int main()
    {
        char string[257], note2[257];
        int b;
        ofstream a_file("note.txt", ios::trunc);
        cout<<"Write your note:"<<endl;
        cin.getline(string, 256, '\n');
        cout<<"loading..."<<endl;
        for(int a=0;a<strlen(string);a++)
        {
            b=(int)string[a];
            note2[a]=(char)(b+39);
        }
        a_file<<note2;
        a_file.close();
        cout<<"Your note is now encrypted into the file: 'note.txt'"<<endl;
        cin.get();
        return 0;
    }
    and here is the decrypter:
    Code:
    #include <iostream.h>
    #include <fstream.h>
    int main()
    {
        char string[257], note2[257];
        int b;
        ifstream b_file("note.txt"); 
        b_file>>string;
        cout<<"'Note.txt' is now being decrypted.\nPlease wait..."<<endl;
        for(int a=0;a<strlen(string);a++)
        {
            b=(int)string[a];
            note2[a]=(char)(b-39);
        }
        cout<<note2<<endl;
        b_file.close();
        cin.get();
        return 0;
    }
    Please tell me if anything is wrong.
    Thanks,
    -KaibaFan321

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >b_file>>string;
    This will only read characters up to the first whitespace character. So if your encrypted message has any whitespace, you won't decrypt all of it. Try using getline instead.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    9
    What would you write for getline, and the problem is it has extra characters at the end when you type a space
    Thanks for putting up with a complete noob!
    -KaibaFan321

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What would you write for getline
    The same thing you wrote in the encrypter except using the file stream object instead of cin:
    Code:
    b_file.getline(string, sizeof string);
    >and the problem is it has extra characters at the end when you type a space
    It's best to solve one problem at a time. I'll tell you right now that you're missing a header and note2 isn't actually a string because you never terminate it with '\0'. It really would be better if you'd stick with modern C++ (ie. string objects and modern headers) rather than the dated code you're using now. If you need to replace your compiler, there are plenty of free ones.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Socialising Chat Bots
    By bengreenwood in forum C++ Programming
    Replies: 10
    Last Post: 11-28-2007, 08:42 AM
  2. Making a file encrypter!
    By RamzaB in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2006, 03:22 PM
  3. simple 2d "putpixel" api for windows?
    By spiky in forum C Programming
    Replies: 2
    Last Post: 10-27-2005, 02:44 PM
  4. Simple simple graphics
    By triplem in forum C Programming
    Replies: 2
    Last Post: 05-19-2003, 02:52 AM
  5. Making a Simple Database System
    By Speedy5 in forum C++ Programming
    Replies: 1
    Last Post: 03-14-2003, 10:17 PM