0

Hi I am trying to find out what I am doing wrong with this code...I need it to load a file and encrypt it, print it, encrypt it again(decrypt it), print it then save it to another file. the original and the new file should be the same...Here is what I have so far (I'm not sure it's right since it won't compile) any help would be GREATLY appreciated.

#include <iostream> //includes library of terms that allows streams 
#include <string> //includes library of terms that allows strings 
#include <fstream> //includes library of terms that allow file I\O
using namespace std; // Basic library of terms
struct Message //defining structure called message
{
 string message; // represents message itself
 int key;  // int to represent the key used to encrypt/decrypt the message
 void encrypt(int charNumber);
 void encrypt();
};
class Encrypt
{
 private:
  Message ms[10];//array of messages, max number of 10
  int numMessages; // indicates the current number of messages in the array
 public:
  Encrypt();     //my default constructor
  void addMessage(Message m); //takes in message and adds it to the array
  void encrypt (int index); //takes in int and encrypts only the at that index in the array 
  void encrypt ();   //takes in no parameters encrypts every message in array 
  void load(string filesname);//load func which takes in string and loads each message from that file into the array
  void print();    //prints out each message (but not the keys)to the screen
  void save(string filesname);//takes in a string and saves all of the messages to that file 
};
int main()
{
 Encrypt e;
 e.load("file.txt");  //loads file
 e.print();    //prints file 
 e.encrypt();   //encrypts file
 e.print();    //prints encrypted file
 e.save("filenew.txt"); //saves encrypted file to new location
 e.encrypt();   //unencrypts file
 e.print();    //prints file again(should be same as before encryption)
  return 0;
}
void Message::encrypt(int charNumber)
{
 message[charNumber] += key;
}
void Message::encrypt ()
{
 for(int i = 0; i < message.size ; i++)
  { 
   encrypt(i);
  }
 key *= -1;
}
void Encrypt::encrypt(int index)
{
 ms[index].encrypt();
}
void Encrypt::addMessage(Message m)
{
 if(numMessages<10)
 {
  Messages[numMessages].key = m.key;
  Messages[numMessages].mess = m.mess;
  numMessages ++;
 }
}
void Encrypt::encrypt(int index) // this function changes the message into ascii numbers
{
 int temporaryKey.ms[index].key;
 string temp = ms[index].part;
 for(i = 0; i < temporary.size(); i++)
 {
  int ascii = temporary[i];
  ascii = temporaryKey;
  ms[index].message[i] = ascii;
 } 
}
void Encrypt()// This function takes in no parameters and encrypts every message in the array of messsages
{
}
void Encrypt::load(string filesname)//Function that loads the file to be encrypted 
{  
 ifstream in;
 in.open(filesname.c_str());  
 if(in.fail()) //if file fails to open do this...
 {
  cout<<"unable to open file";
 }
 else//other wise do this 
  {
   while(!in.eof())//while not at the end of the file
   {
    in >>message[numMessages].key;
    in.get();
    getline(in.message[numMessages].ms;
    numMessages++;
   } in.close();
  }
}
void print()//This function prints out the message
{

}
void save(string filesname)
{
 ostream out;    
 out<<
}
2
Contributors
1
Reply
2
Views
11 Years
Discussion Span
Last Post by John A
0

OK, first off:

for(int i = 0; i < message.size ; i++)

message is part of the struct Message. You have to refer to an instance, or else you will get an error.

void Encrypt::encrypt(int index)

And here, it should be Encrypt:Encrypt, and since constructors can't return anything, remove the void.

Messages[numMessages].key = m.key;
Messages[numMessages].mess = m.mess;

Here, you previously declared the array of messages as ms, and there's no member ever declared "mess" anywhere in your code.

void Encrypt::encrypt(int index)

Same problem as before, remove void and captalize 'E'. Also, you're redefining Encrypt::Encrypt(int index). Don't know what you were trying to do.

int temporaryKey.ms[index].key;

I have no idea what you're trying to accomplish here. You have to declare any variables that are going to be inside a structure inside of it.

string temp = ms[index].part;

Where did part come from?

for(i = 0; i < temporary.size(); i++)

Should be:

for(int i = 0; i < temporary.size(); i++)
ascii = temporaryKey;

Results from the int temporaryKey.ms[index].key; statement above earlier.

    in >>message[numMessages].key;
    in.get();
    getline(in.message[numMessages].ms;

Since message is inside a struct, you must have at least one instance of struct Message

ostream out;    
out<<

I think it should be ofstream instead of ostream, and you obviously forgot to finish the second line.

Think that's everything! ;)

Hope this helps

Edited by pyTony: fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.