Decoding in arrays

This is a discussion on Decoding in arrays within the C++ Programming forums, part of the General Programming Boards category; If anybody could help me I would appreciate it. I am trying to decode using the following: Key string OCTRZSIENVWABFGHJKLMPQUXY ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    81

    Decoding in arrays

    If anybody could help me I would appreciate it.

    I am trying to decode using the following:

    Key stringOCTRZSIENVWABFGHJKLMPQUXY

    Decode string: ABCDEFGHIJKLMNOPQRSTUVWXYZ

    Infile string: CDMLEFB LIR AFMBLDEBK DJR ZMWW FZ IEWWK

    Infile string should decode to: CAUTION THE MOUNTAINS ARE FULL OF HILLS

    What I have been trying to do is find the position of the incoming letters from the file. My problem is that I only get the first letter of the infile (C being at position 2, which equals to C, the next one should be D at position 0, which equals to A, etc). If I add an index to "final", it gives me a compiler error. Maybe I'm going at it the wrong way? Please help!


    char str[80];
    char string3[27] = "DOCTRZSIENVWABFGHJKLMPQUXY";
    char final[80];
    int i;
    char *pdest;
    int result;

    void main(void)
    {
    cls;
    infile.open ("F1949.txt", ios::in);

    while (infile)
    {
    infile.get (final[i]);
    cout << final[i];

    pdest = strstr( string3, final );
    result = pdest - string3;

    if( pdest != NULL )
    {
    cout << endl << final[i] << " found at position " << result << endl;

    }

    i++;

    }

    }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    The key string should be

    DOCTRZSIENVWABFGHJKLMPQUXY

    I don't know how that smiley face got there.
    "Our greatest glory consists not in never failing,
    but in rising every time we fall."

    Oliver Goldsmith (1730-1774).
    Anglo-Irish writer, poet and playwright.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    Well, I didn't get any answers from this post but by doing more research, I got it to work. I would like to thank everybody who took the time to take a look at my post.

    If anyone wishes to add any comments on this code, like if it can be improved, please feel free to do so. It would actually be very much appreciated.

    char str[80];
    char string3[28] = "DOCTRZSIENVWABFGHJKLMPQUXY ";
    char decode[28] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
    char final[80];

    int i, result;
    char *pdest;

    void main(void)
    {
    cls;
    infile.open ("F1949.txt", ios::in);

    cout << "The decoded phrase is:" << endl << endl;
    while (infile)
    {
    infile.get (final[i]);
    pdest = strchr( string3, final[i] );
    result = pdest - string3;

    if( pdest != NULL )
    {
    cout << decode[result];

    } // END IF

    i++;

    } // END WHILE

    cout << endl << endl;

    } // END MAIN
    "Our greatest glory consists not in never failing,
    but in rising every time we fall."

    Oliver Goldsmith (1730-1774).
    Anglo-Irish writer, poet and playwright.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Avoid global variables when possible, they destroy the idea of data hiding and encapsulation.

    >void main(void)
    main returns an int, nothing else.

    >cls;
    What is this?

    >while (infile)
    This is an illegal construct, I believe you meant something more like:
    while ( infile.good())

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    Thanks for the tips! It is very much appreciated. I will adjust the program correctly.

    >void main(void)
    >main returns an int, nothing else.

    I know, but that is how teacher wants it.

    >cls;
    >What is this?

    I should of included the whole thing. It's a command to clear the output screen whenever it is needed. Done by adding:

    #define cls system ("cls")

    I just didn't want to overpost. It's another one of the teacher's request. But while we are on the subject, our teacher makes us type:

    #include "a:bcc.h"

    Where on a:\ ALL the #include are stored, hence no need to type them everytime. Do you think it's a good idea? I mean I have to do it for now, but should I keep doing this in the future or do you think it's bad programming?

    Thanks again!
    "Our greatest glory consists not in never failing,
    but in rising every time we fall."

    Oliver Goldsmith (1730-1774).
    Anglo-Irish writer, poet and playwright.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I know, but that is how teacher wants it.
    Teacher is a moron then

    >#define cls system ("cls")
    Not only is the practice of redefining something simple like system ( "CLS" ) a practice in obfuscation, the system function is godawful slow and should be avoided like the plague when possible.

    >Do you think it's a good idea?
    No, if you have a specialized header that only includes standard headers you'll do nothing but annoy readers. I can usually tell if a program will be able to compile on my system by reading the headers, if they aren't there then I'll assume the program won't compile and move on.

    >I mean I have to do it for now
    No you don't, tell your teacher he/she is wrong and why. Especially with void main, an instructor that insists you use it is of poor quality and shouldn't be teaching anyway.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    I'm laughing so hard I'm crying!!

    He is a moron, but he is very set in his ways. I'm getting an A in the class up to now and I want to keep it that way. I know it sucks and I should rebel, but I've seen what happens to people who disagree with him. Old fart. Hence, I'm just going to do what I am told for now keeping in mind that the way he wants things isn't the proper practice. And if I'm not sure of something, I'll ask the pros.

    Hell, he doesn't teach anything anyway, we pretty much have to figure out everything on our own. More than half of the students have dropped the class because they couldn't keep up. That sure tells ME that something is wrong with the teacher!!

    By the way, I found the URL's in your signature very informative.
    "Our greatest glory consists not in never failing,
    but in rising every time we fall."

    Oliver Goldsmith (1730-1774).
    Anglo-Irish writer, poet and playwright.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  4. Help with arrays and pointers please...
    By crazyeyesz28 in forum C++ Programming
    Replies: 8
    Last Post: 03-17-2005, 12:48 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

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