Help with a Decoder!

This is a discussion on Help with a Decoder! within the C++ Programming forums, part of the General Programming Boards category; Hey guys, first time here and I tried looking in other posts to see if I found what I needed.. ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    1

    Question Help with a Decoder!

    Hey guys, first time here and I tried looking in other posts to see if I found what I needed.. But I didn't...
    I am currently making a decoder, which consists of having a base text of: "the quick brown fox jumps over the lazy dog" and then having the user input a name file, opening the file and reading the first line. This line in the file will have the same number of chars as my base text, which will lead to knowing what is the new code... After that, the program must re-read the file with the remaining lines and it will be decoded with what I already have... And then create an output file with the decoded lines... Does it make any sense?

    For example:
    baseText.txt :
    the quick brown fox jumps over the lazy dog

    Decoder.txt :
    gsv jfrxp yildm ulcvh qfnkh levi gsv ozab wlth
    gsv jfrxp yildm ulc qfnkh levi z gsivv wlth
    gsv jfrxp yildm ulc qfnkh levi gsv ozab wlt
    gsv ozab wlt qfnkh levi gsv jfrxp yildm ulc
    gsvjfrxp yildm ulc qfnkh levi gsvv ozab wlt

    output.txt:
    the quick brown foxes jumps over the lazy dogs
    the quick brown fox jumps over a three dogs
    the quick brown fox jumps over the lazy dog
    the lazy dog jumps over the quick brown fox
    thequick brown fox jumps over thee lazy dog


    And what I have so far is
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    
    using namespace std;
    
    
    int main(){
        ifstream textoBase;
        baseText.open("BaseText.txt");
        string base[50];
        while(baseText >> base[50])
        {
            cout << "Texto Base: " << base << endl;
    
    
        }
        cout <<endl;
        baseText.close();
        system("pause");
        return 0;
    
    
    }
    I tried that to see if I could see the base text, but it doesn't...
    And I'm a newbie in this, so I don't know that much of programming...
    Anything is appreciated, thanks!

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    You want to 'map' each character in the alphabet to your own 'encoding'.

    1) Read in the first line in your file
    2) Iterate over each character in the line
    3) Store each character in an array that represents a character mapping
    4) Read and decode the remaining lines

    I want to guess you do not know how many lines are in the file. Don't use an array of strings like you have done, instead use a vector<string>.
    The >> operator will read 1 token at a time (i.e. one word), instead use getline(stream, str).



    A nice way to hold your 'mapping' is to use an array.
    Imagine you want to map the letter 'f' to the letter 'g'.
    We can do the following:
    Code:
    char * map = new char[26];
    map['f' - 'a'] = 'g';
    Now you can hold a mapping you each 26 characters, each accessible using a single array. (We subtract 'a' so that the character 'f' is turned into a number between 0 and 25)
    Last edited by rodrigorules; 09-09-2012 at 09:36 PM.
    Check out my programming / algorithm blog @ http://www.swageroo.com

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Where is baseText defined? I only see a definition for textoBase

    If you have errors, you need to post them. We can't see your screen from here.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,772
    rodrigorules: Why are you using the new operator here?
    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.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    Quote Originally Posted by Elysia View Post
    rodrigorules: Why are you using the new operator here?
    Ah, bad habit. You can do without it since we know that the array will be a constant size of 26.
    Check out my programming / algorithm blog @ http://www.swageroo.com

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    Quote Originally Posted by rodrigorules
    Ah, bad habit. You can do without it since we know that the array will be a constant size of 26.
    Heh, you would at least have done better to suggest a std::vector<char> or a std::string instead
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. packet decoder in C
    By eshoja in forum C Programming
    Replies: 2
    Last Post: 06-05-2012, 11:42 AM
  2. Help with a resistor decoder
    By usernameisvalid in forum C Programming
    Replies: 5
    Last Post: 06-25-2011, 05:21 PM
  3. Mpeg2 decoder
    By thangdc01_02 in forum C Programming
    Replies: 6
    Last Post: 11-21-2010, 03:09 PM
  4. decoder
    By jalenamichelle in forum C++ Programming
    Replies: 1
    Last Post: 11-03-2010, 06:24 AM
  5. MP3 Decoder
    By Pete in forum Projects and Job Recruitment
    Replies: 4
    Last Post: 05-02-2005, 08:35 AM

Tags for this Thread


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