Flash Flash Revolution

Flash Flash Revolution (http://www.flashflashrevolution.com/vbz/index.php)
-   Technology (http://www.flashflashrevolution.com/vbz/forumdisplay.php?f=74)
-   -   C++ Trouble (http://www.flashflashrevolution.com/vbz/showthread.php?t=123098)

justin_ator 03-18-2012 11:03 PM

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.

justin_ator 03-19-2012 12:13 AM

Re: C++ Trouble
 
bump ;__;

Patashu 03-19-2012 12:14 AM

Re: C++ Trouble
 
oh, I found the problem

for(int i=0; i <= encrypted.length()+10; i++)
//...
i++;
}

you increment i twice each time through the loop so it's skipping over the & and # depending on whether they're odd or even parity

mwehehehe

happy C++ writing

justin_ator 03-19-2012 12:15 AM

Re: C++ Trouble
 
hnngggggggggggg thank youuuuu I'm fixing it, I'll be back if that doesn't solve the problem. How did I not see that -__________-

Patashu thank you, I do believe that fixed the problem. Fuccin derp, I've spent like the last two hours trying to figure out why C++ hates me, and I was adding 2 to i each time...akfl;jdfklajsd;fjadkflj

Patashu 03-19-2012 12:20 AM

Re: C++ Trouble
 
Quote:

Originally Posted by justin_ator (Post 3660279)
hnngggggggggggg thank youuuuu I'm fixing it, I'll be back if that doesn't solve the problem. How did I not see that -__________-

Patashu thank you, I do believe that fixed the problem. Fuccin derp, I've spent like the last two hours trying to figure out why C++ hates me, and I was adding 2 to i each time...akfl;jdfklajsd;fjadkflj

isn't programming so fun? :)

justin_ator 03-19-2012 12:34 AM

Re: C++ Trouble
 
I actually do enjoy it so far... it's just sometimes I get lost or can't think of where to start because I know so little and my professor has taught us such a limited amount of stuff pertaining to each topic...we've been over arrays for example, but I can do very little with them still because I simply don't know what I'm doing ;___;

ps the program works beautifully, I can now have secret conversations with anyone who has my decrypt and encrypt file :')

MaxGhost 03-19-2012 12:50 AM

Re: C++ Trouble
 
Sorry didn't see this until too late :(

justin_ator 03-19-2012 12:59 AM

Re: C++ Trouble
 
Ah don't worry about it. I only posted the link to you because I knew you had programming knowledge and wouldn't mind looking at it. If you really want to see what it was about you can quote my post still, it's all there


All times are GMT -5. The time now is 04:21 PM.

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright FlashFlashRevolution