Go Back   Flash Flash Revolution > General Discussion > Technology
Register FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
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
Old 03-19-2012, 12:13 AM   #2
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 Re: C++ Trouble

bump ;__;
__________________
justin_ator is offline   Reply With Quote
Old 03-19-2012, 12:14 AM   #3
Patashu
FFR Simfile Author
Retired StaffFFR Simfile Author
 
Patashu's Avatar
 
Join Date: Apr 2006
Location: we traced the call...it's coming from inside the house
Age: 33
Posts: 8,609
Send a message via AIM to Patashu Send a message via MSN to Patashu Send a message via Yahoo to Patashu
Default 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
__________________
Patashu makes Chiptunes in Famitracker:
http://soundcloud.com/patashu/8bit-progressive-metal-fading-world
http://img.photobucket.com/albums/v216/Mechadragon/smallpackbanner.png
Best non-AAAs: ERx8 v2 (14-1-0-4), Hajnal (3-0-0-0), RunnyMorning (8-0-0-4), Xeno-Flow (1-0-0-3), Blue Rose (35-2-0-20), Ketsarku (14-0-0-0), Silence (1-0-0-0), Lolo (14-1-0-1)
http://i231.photobucket.com/albums/ee301/xiaoven/solorulzsig.png
Patashu is offline   Reply With Quote
Old 03-19-2012, 12:15 AM   #4
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 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
__________________
justin_ator is offline   Reply With Quote
Old 03-19-2012, 12:20 AM   #5
Patashu
FFR Simfile Author
Retired StaffFFR Simfile Author
 
Patashu's Avatar
 
Join Date: Apr 2006
Location: we traced the call...it's coming from inside the house
Age: 33
Posts: 8,609
Send a message via AIM to Patashu Send a message via MSN to Patashu Send a message via Yahoo to Patashu
Default Re: C++ Trouble

Quote:
Originally Posted by justin_ator View Post
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? :)
__________________
Patashu makes Chiptunes in Famitracker:
http://soundcloud.com/patashu/8bit-progressive-metal-fading-world
http://img.photobucket.com/albums/v216/Mechadragon/smallpackbanner.png
Best non-AAAs: ERx8 v2 (14-1-0-4), Hajnal (3-0-0-0), RunnyMorning (8-0-0-4), Xeno-Flow (1-0-0-3), Blue Rose (35-2-0-20), Ketsarku (14-0-0-0), Silence (1-0-0-0), Lolo (14-1-0-1)
http://i231.photobucket.com/albums/ee301/xiaoven/solorulzsig.png
Patashu is offline   Reply With Quote
Old 03-19-2012, 12:34 AM   #6
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 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 :')
__________________
justin_ator is offline   Reply With Quote
Old 03-19-2012, 12:50 AM   #7
MaxGhost
FFR Veteran
FFR Veteran
 
MaxGhost's Avatar
 
Join Date: Feb 2008
Location: Ottawa, Canada
Age: 30
Posts: 2,141
Send a message via Skype™ to MaxGhost
Default Re: C++ Trouble

Sorry didn't see this until too late :(
MaxGhost is offline   Reply With Quote
Old 03-19-2012, 12:59 AM   #8
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 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
__________________
justin_ator is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 01:47 AM.


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