Thread: C++ Trouble
View Single Post
Old 03-18-2012, 11:03 PM   #1
justin_ator
🥓<strong><span style="col
Resident Overseer
FFR Veteran
 
justin_ator's Avatar
 
Join Date: Mar 2007
Location: Kingsport, TN
Posts: 7,648
Default C++ Trouble

Alright so I'm in my first C++ class. I don't know a whole lot about programming, have to start somewhere...the code is probably inefficient as fuc but the professor doesn't care right now. I don't want you guys to write the program for me or rewrite what I have, I just want to know what is causing the problem I'm having...now for the actual meat of the post.

I'm supposed to write a program to decrypt an input file with an already encrypted message. The encrypted message would look something like this:

Code:
nggnpxongx3933&7#10
The message written there is "attack at 0600". The "&" signifies the end of the message, and the numbers after that signify the location of the spaces in that list of characters, separated by "#". All characters were shifted 13 to the right on encryption, with spaces being 4 to the right from the previous letter.

I have this as my program:

Code:
#include <iostream>
#include <fstream>
using namespace std;

int length(string);

int spaces[100];

string decrypt(string s);

int main()
{
    system("Title Decryption Program");
    
    //declare variables
    string encrypted, s;
    int o=0;
    int number=0, current=0;
        
    //draw input
    ifstream input;
    input.open("secret.dat");
    if(!input)
        {
              cout << "Error, file not found. Please verify that ";
              cout << "secret.dat is in the correct directory. ";
              system("pause");
              return 1;
        }
    else
        cout << "File has loaded, beginning data processing. " << endl;
    
    //draw data from file
    getline(input, encrypted);
        
    for(int i=0; i <= encrypted.length()+10; i++)
      {
        if(encrypted[i] == 35 || encrypted[i] == 38)
          {
            current = encrypted[i+1]-48;
            if(encrypted[i+2] < 58 && encrypted[i+2] > 47)
              {
                number = current;
                current = encrypted[i+2]-48;
                number = number*10 + current;
                if(encrypted[i+3] < 58 && encrypted[i+3] > 47)
                  {
                    current = encrypted[i+3]-48;
                    number = number*10 + current;
                    if(encrypted[i+4] < 58 && encrypted[i+4] > 47)
                      {
                        current = encrypted[i+4]-48;
                        number = number*10 + current;
                      }
                  }
                spaces[o] = number;
              }
            else
              {
                spaces[o] = current;
              }
            number = 0;
            current = 0;
            //cout << spaces[o]; (was using this to check the space locations as I went)
            o++;
          }
        i++;
      }

    //decrypt start
    s = decrypt(encrypted);
    
    for(int i=0; i <= s.length(); i++)      
      {
        if(s[i] == '&')
          break;
        cout << s[i];
      }
    
    cout << endl;
    
    system("pause");
    return 0;
}

int length(string s)
{
    int l=0;
    while(s[l] != '\0')
            l++;
    return l;
}

string decrypt(string s)
{
    for(int i=0; i < length(s); i++)
      {
        bool matches = false;
        for(int j = 0; j < 100; j++)
          {
            if(i+1 == spaces[j])
              {
                matches = true;
              }
            if(matches == true)
              {
                s[i] = ' ';
              }
          }
        if (s[i] >= 97 && s[i] <= 109)
            s[i] = s[i] + 13;
        else if (s[i] >= 110 && s[i] <= 122)
            s[i] = s[i] - 13;
        else if (s[i] >= 48 && s[i] <= 57)
            {
              s[i] = s[i] - 13;
              if (s[i] < 48)
                 s[i] = s[i] + 10;  
            }
      }
    return s;   
}
When I use the sample message, everything works fine. I get exactly what I am supposed to. It's when the message gets changed that I start having issues, which I will list here:

Changing the two space locations (ie changing &7#10 to &9#12) gives me what it should, with spaces at 9 and 12 instead of 7 and 10.

Changing characters in the message itself (ie changing attack to suffer) gives me the correct decrypted message and space location.

Changing the length of the message (ie adding an x to the beginning, or removing a letter completely) results in no spaces being registered, not even at the point I was testing my space locations with the cout statement I've commented out.

Adding extra spaces to any point beyond 10 results in the new spaces not being registered (ie adding #12 to the end of the file)


I really don't know what the problem is and I've been talking it over with reuben for a little bit now, but without having him rewrite my code I can't really get the help I'm looking for from him. I do have to thank him for a few of the ideas in here though, because I've actually gotten a lot farther tonight since I could kinda hear what he thought about some of it.

EDIT: If I add in an extra character it only recognizes spaces greater than 10 x____x ?


Problem solved.
__________________

Last edited by justin_ator; 03-19-2012 at 12:34 AM..
justin_ator is offline   Reply With Quote