4 Replies - 283 Views - Last Post: 10 October 2012 - 10:03 PM Rate Topic: -----

#1 sammygirl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-October 12

Decoder Project

Posted 10 October 2012 - 10:35 AM

The following is the assignment:

1. Start with a sequence of three integers, each between 0 and 26 inclusive.
2. Each character in the message is applied to one of the three integers to produce an encrypted message,
as in the example below:
Three-integer key is: 12 5 16
Text is: Mr. Whiskers
Each character in the text is applied to one of the three keys, in sequence:
This translates to: Yw. Bxuxaqwi
M r . W h i s k e r s
12 5 16 12 5 16 12 5 16 12 5 16

In the encryption, each key is added to its corresponding letter. For example, ‘M’ + 12 = ‘Y’. Also
notice that letters “wrap-around” the alphabet, as in ‘W’ + 5 = ‘B’.
Furthermore, note that the space in the example is treated as a character and has a corresponding key,
but the calculation is not applied. The same holds true for the ‘.’ character. The encryption is only
applied to uppercase and lowercase alphabetic letters. All other characters in the input stream are left
unchanged and echoprinted to the output.
Your job is to write a program that will decrypt Austin Powers’ secret messages in the exact opposite
way they are encrypted. Austin will provide the encrypted messages to you. Your program must do the
following:
1. Ask the user for a file name to decrypt. File names cannot contain blanks and must have at most 25
characters.
2. Accept three integer keys as input. These integers must be values between 0 and 26, inclusive. You
may assume that the user does enter an integer whenever asked for an integer.
3. Echo the keys to the output for verification.
3. Read each character in the file and output the decrypted message to the console (you do not have to
output to a file). **NOTE: Read the characters one by one. For each character read in, first decrypt it,
and then output the decrypted character to the console. THEN go get the next character.
4. Ask if the user wants to continue. If yes, repeat. If no, then print a closing message and terminate the
program



I have done the following code, but I am kind of lost on how to use a counter with the three keys and how to make it print as a paragraph and not one character per line.

The following is an example of a message and the key:

Fq vv jcr, anej km vgr feqnia qcxf ns sug 1960u sqq Ctfvha
Obydeu, Kmggqacsvqmnn Zcm qe Oxfvdea. Dzpm gjda, yqur vnu
pcqrhqrg, cmq sug bpkl sukmt sucs yzf cnpfrtnhu jcr Fq. Dikk.
Ce. Rxhy vnpsrf gq qgrgtnl sug cnzags, vnpsrf gq xkky Zhusvp
Cqvrtr, zaf jqqfg, yzavdq digqlqmr sb ar z uphcqr.
fqteed: ggvo://jyv.ctfvharnjgqf.bbo/fjzt2.uvly

keys: 13 2 25


#include <iostream>
#include <fstream>
#include <iomanip> 
#include <string>
#include <cmath>
#include <cctype>
#include <cstdlib> 

using namespace std;
int main ()
{
	char		decrypt;
	char		choice;
	int			keyOne;
	int			keyTwo;
	int			keyThree;
	const int	MIN = 0;
	const int	MAX = 26;
	string		fileName;
	ifstream	inFile;
	char		fileOutput;


	// print out introductory heading
	cout <<"------------------------------------------------" << endl;
	cout <<" Welcome to the Austin Powers Decryption Program" << endl;
	cout <<"------------------------------------------------" << endl << endl;

	

	cout << "Please enter a file name" << endl;
	cin >> fileName;

	
	{inFile.open( fileName.c_str() );

	

	if  (!inFile)
	{
		cout << endl << "Incorrect file name.  Please re-enter file name" << endl;
	cin >> fileName;
	inFile.open ( fileName.c_str() );
	}
	
	else 
	{
	
	cout << "Please enter the first decryption key between 0 and 26" << endl;
	cin >> keyOne;

	while (keyOne < 0 || keyOne > 26)
	{
		cout << "This decryption key is not valid. Pleae re-enter the first key." << endl;
		cin >> keyOne;
	}

	cout <<"Please enter the second decryption key between 0 and 26" << endl;
	cin >> keyTwo;

	while (keyTwo < 0 || keyTwo >26)
	{
		cout << "This decryption key is not valid. Pleae re-enter the second key." << endl;
		cin >> keyTwo;
	}


	cout <<"Please enter the third decryption key between 0 and 26" << endl;
	cin >> keyThree;

	while (keyThree < 0 || keyThree >26)
	{
		cout << "This decryption key is not valid. Pleae re-enter the third key." << endl;
		cin >> keyThree;
	}
	
	cout << "These are the decryption keys you entered" << endl;
	cout << keyOne << endl << endl;
	cout << keyTwo << endl << endl;
	cout << keyThree << endl << endl;
	
	



	while (inFile >> fileOutput)
     { 	int MyAsciiValue = static_cast<int>(fileOutput);
		 if (MyAsciiValue >= 65)
		 {decrypt = MyAsciiValue-keyOne;}
		 cout << decrypt<< endl;
	}

	cout << " \n \n Would you like to decrypt another file? "<< endl;
	cout << "Please enter Y or y for yes and N or n for no." << endl;
	cin >> choice;

	while (choice != 'n' && choice !='N' && choice !='y' && choice != 'Y')
	{ 
		cout << "This is not a valid choice. Please try again." << endl;
		cin >> choice;
	}
 
	while (choice == 'Y' || choice == 'y' );

	}}
	  // end of do
	return (0);
}



Is This A Good Question/Topic? 0
  • +

Replies To: Decoder Project

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,004
  • Joined: 08-August 08

Re: Decoder Project

Posted 10 October 2012 - 11:05 AM

Functions would make your life a lot easier.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3458
  • View blog
  • Posts: 10,665
  • Joined: 05-May 12

Re: Decoder Project

Posted 10 October 2012 - 11:55 AM

As an aside, it looks like you've gotten a lot further than acidfire00: http://www.dreaminco...yption-program/
Was This Post Helpful? 0
  • +
  • -

#4 hoople  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 8
  • Joined: 06-September 12

Re: Decoder Project

Posted 10 October 2012 - 08:23 PM

And this guy: http://www.reddit.co..._program_stuck/
Was This Post Helpful? 0
  • +
  • -

#5 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Decoder Project

Posted 10 October 2012 - 10:03 PM

I think in lines 52,61 and 71, it will be a lot better if you could use conditional statements using 'if' instead of while.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1