Thread: Text Encoder/ Decoder in C++

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    4

    Text Encoder/ Decoder in C++

    Yo, I currently have a project for my (Basic) Computer programming class which consists of making a simple Text Encoder/Decoder.

    It'll read text from a file (named as "input.txt"), transformed/encoded using an encoding algorithm (to be created by me.) and saved into another file (named as "output.txt"). The decoder is simply this process in reverse only with the file named as "decoded.txt".

    The problem I'm facing is I have no idea how an encoding algorithm (as well as the script that allows the encoder/decoder to read from the file) works or is made. This past term was literally my first experience with C++.

    So far, here's what I've managed to come up with: (Note: I'm using Bloodshed Dev-C++)

    Code:
    #include <math.h>
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h> // The libraries
    
    int main(){
        
        int option; // Variable for selection.
        
        printf("Select a Number to select a Function\n"); // The choices and selection.
        printf("1. Encode\n");
        printf("2. Decode\n");
        printf("3. Close\n\n");
        scanf("%i",&option);
        
        switch(option){case 1: printf("\nYOU SHALL ENCODE!"); // Placeholder for Encoding script
                       break;
                       case 2: printf("\nYOU SHALL DECODE!"); // Placeholder for Decoding script
                       break;
                       case 3: printf("\nPress any key to continue."); // Script for cancellation.
                       break;
                       default: printf("\nINVALID, TRY AGAIN NEXT TIME.");
                       }
        
        getch();}
    Any form of advice or tutorials (which a complete idiot could understand XD) would be much appreciated.

    Regards,

    Joe

    C++ Programming neophyte

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    the basic idea is that you apply some sort of transformation to the plain text - xor for example - that when applied to the encoded text, gives the original plain text.

    on a side note: dev-C++ is ANCIENT in terms of develpment environments. get code::blocks or eclipse or MSVC++ Express

  3. #3

  4. #4
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    94
    Have a look at XOR cipher - Wikipedia, the free encyclopedia

    Very simple algorithm; in order to encrypt, do a xor on each byte of your text and store that result. In order to de-crypt, again do xor's with same value on every byte of the previously encrypted text et voila...

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    4
    Right, I'll take a look at them later. (I have Finals today, so yeah.)

    @Elkvis: I'll keep that in mind when I get LBYEC72, which would be two terms from now. (The next course after this course I'm taking.)

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Did you accidentally post this in the wrong section?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    4
    No I didn't, I originally had no idea how to go about things but now I have an inkling as to how to do it.

    My only problem now is the encryption part of the program. (and the subsequent writing into another file.)

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then how come you posted C in C++?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    4
    That was C? I didn't know. :/

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Joe_Allen View Post
    That was C?
    Yep. The most significant difference is in the #includes at the top -- those are C library includes. The C++ library uses slightly different versions of those:

    Code:
    #include <cmath>
    #include <cstdio>
    #include <conio.h> // dunno about this one
    #include <cstdlib>
    These allow you to use the standard C functions you are using in a C++ program (notice the form: stdio.h = cstdio, etc). You should not use a C++ compiler with the other headers; altho in practice they are nearly identical and so work, there could be slight differences which will lead to problems.

    All I know about conio.h is that it is from a (non-standard) Borland C library originally for MS-DOS. It may work fine with a C++ compiler, but then again, it may not. Unless you know you need it for something, just leave it out.

    Of course, if you are looking to learn C++, you might want to use native C++ stuff from <iostream>, like cout, instead of printf from <cstdio>.
    Last edited by MK27; 12-15-2011 at 06:42 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    103
    Please don't use Conio.h!

    Its a horrible, horrible thing, and it'll leave you with no friends.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > That was C? I didn't know. :/
    Isn't it a bit late in the course to discover that you've been learning the wrong language?

    If your tutor has just been feeding you stuff on the basis "well it compiles", then you need to get your money back!
    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.

  13. #13
    Registered User
    Join Date
    Dec 2011
    Posts
    7
    Quote Originally Posted by Joe_Allen View Post
    Yo, I currently have a project for my (Basic) Computer programming class which consists of making a simple Text Encoder/Decoder.

    It'll read text from a file (named as "input.txt"), transformed/encoded using an encoding algorithm (to be created by me.) and saved into another file (named as "output.txt"). The decoder is simply this process in reverse only with the file named as "decoded.txt".

    The problem I'm facing is I have no idea how an encoding algorithm (as well as the script that allows the encoder/decoder to read from the file) works or is made. This past term was literally my first experience with C++.

    So far, here's what I've managed to come up with: (Note: I'm using Bloodshed Dev-C++)

    Code:
    #include <math.h>
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h> // The libraries
    
    int main(){
        
        int option; // Variable for selection.
        
        printf("Select a Number to select a Function\n"); // The choices and selection.
        printf("1. Encode\n");
        printf("2. Decode\n");
        printf("3. Close\n\n");
        scanf("%i",&option);
        
        switch(option){case 1: printf("\nYOU SHALL ENCODE!"); // Placeholder for Encoding script
                       break;
                       case 2: printf("\nYOU SHALL DECODE!"); // Placeholder for Decoding script
                       break;
                       case 3: printf("\nPress any key to continue."); // Script for cancellation.
                       break;
                       default: printf("\nINVALID, TRY AGAIN NEXT TIME.");
                       }
        
        getch();}
    Any form of advice or tutorials (which a complete idiot could understand XD) would be much appreciated.

    Regards,

    Joe

    C++ Programming neophyte
    It's been said before, but this is "C". The easiest way to tell is that all standard headers require the .h extension (only non-standard in c++) and that you are using print/scanf instead of cin/out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating Video Encoder/Decoder Question
    By chiefmonkey in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 11:02 AM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. ASCII Art Decoder and Encoder
    By jessweetd in forum C Programming
    Replies: 7
    Last Post: 09-05-2004, 07:12 PM
  4. Need RSA encoder / decoder / keygen
    By Rak'kar in forum Networking/Device Communication
    Replies: 4
    Last Post: 07-20-2004, 09:40 PM
  5. string encoder / decoder
    By mbell in forum C Programming
    Replies: 0
    Last Post: 09-12-2001, 09:45 PM