3 Replies - 2621 Views - Last Post: 09 April 2009 - 11:55 PM Rate Topic: -----

#1 sramirez3585  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 29-September 08

caesar cipher- reading in and output to a file

Posted 09 April 2009 - 09:58 AM

So this code is supposed to read in a file encrypt or decrypt it, save the output into a file.
Im having trouble with this because when I open the file where the output should be it is empty, I am not sure what function is going wrong here.

Here is my code:

void Caesar::encrypt()
{
  string indata, outdata;
  int k=3;
  char ch;

  readline(indata);
	for(int i=0;i<indata.length(); i++)
	{
	  ch=indata[i]+k;
	  if(ch > 'z')
		ch-=26;
	}
	outdata.append(1, ch);

	saveline(outdata);
}

void Caesar::decrypt()
{
  string indata, outdata;
  int k=3;
  char ch;

  readline(indata);
	for(int i=0; i < indata.length(); i++)
	{
	  ch=indata[i]-k;
	  if(ch > 'z') ch+=26;
	}
	outdata.append(1, ch);
	saveline(outdata);

}


void Caesar::readline(string &indata)
{
  string infilename;
  ifstream inputFile;

  cout<<"Enter the filename you want to encrypt\n";
  cin>>infilename;

  inputFile.open(infilename.data());
  getline(inputFile, indata);
  inputFile.close();
}
void Caesar::saveline(string &outdata)
{
  ofstream outputFile;

  outputFile.open("caesar.txt");
  outputFile<<outdata;
  cout<<"File Saved\n";
  outputFile.close();

}



If anyone can help me out I would appreciate it, thanks.

This post has been edited by sramirez3585: 09 April 2009 - 10:07 AM


Is This A Good Question/Topic? 0
  • +

Replies To: caesar cipher- reading in and output to a file

#2 Zerobu  Icon User is offline

  • Black Hatter

Reputation: 13
  • View blog
  • Posts: 1,822
  • Joined: 14-January 08

Re: caesar cipher- reading in and output to a file

Posted 09 April 2009 - 03:33 PM

Can you post the code, where actually write to the file?
Was This Post Helpful? 0
  • +
  • -

#3 sramirez3585  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 29-September 08

Re: caesar cipher- reading in and output to a file

Posted 09 April 2009 - 07:07 PM

Ive been working on and off on the code an I believe im messing up somewhere with the encryption or in the way im trying to save the output.

void Caesar::encrypt()
{
  int k=3;
  char ch;

  readline(indata);
	for(int i=0;i<indata.length(); i++)
	{
	  ch=indata[i]+k;
	  if(ch > 'z')
		ch-=26;
	}
	outdata.append(1, ch);
	//cout<<indata<<endl; checking if the input data was read, it was
	//cout<<outdata<<endl; checking if encrption occured, didnt...
	saveline(outdata);
}



its reading the input data from a file as variable indata. I commented in the code about this.
again, thanks for any help.
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: caesar cipher- reading in and output to a file

Posted 09 April 2009 - 11:55 PM

Have a look at your for() loop
	for(int i=0;i<indata.length(); i++)
	{
	  ch=indata[i]+k;
	  if(ch > 'z')
		ch-=26;
	}



You loop through changing "ch" as many times as characters in the string but you don't store the result of your work.

If the string was 8 characters long at the end of your for() loop the value in "ch" will be the result of your work on the 8th character in the string.

The work you did on the first 7 string characters has been lost.

You need to build a new string inside the for() loop.

Declare a string before entering the loop and then concatenate the result of each "ch" manipulation onto the end of the string before looping to the next original string character.

Then use the new string you have build.

Make sense?

Hints here
http://www.cplusplus.../string/append/

This post has been edited by janotte: 09 April 2009 - 11:57 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1