• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Need some help with homework.

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

ryanmartini

Member
Joined
Oct 1, 2004
Location
douglasville ga
Ok, so Im writing this program that will decrypt files. I just want to get the loops and such working first.

The problem is is that no matter the text file you put into the prompt, it starts. It overall is a big do while loop. Ive modeled it right after the examples in my book. If you can spot my mistake, please give me a hint. Im out of ideas.

Right now I have working:
It only accepts decryption codes 0-26 like its supposed to and memorizes them despite not putting in 0-26.

It has an input for the file name, but does not check to see if it is a valid file. The do while which 98% of the program is enclosed in should check the file using the ifstream inputfile;

Again, thank you for any help.

Code:
#include <iostream> // include iostream standard
#include <iomanip> // include io manipulation
#include <fstream> // include file stream
#include <cmath> // include cmath libraries
#include <cctype> // include cctypes. 
using namespace std; // standard namespace 
// end header 

// start main function 
int main ()
{	// declaration for char length,
	const int length = 26;
	
	// length of allowed files using const of 26 characters.
	char readfile[length]; 

	// Begin Printing Main Menu. 
	cout << "================================================" << endl;
	cout << "Welcome to the Austin Powers Decryption Program!" << endl;
	cout << "================================================" << endl;
	// End Printing Main Menu 

	ifstream inputFile; // inputFile declared name for function to read .txt's
	do
	{
		//Print prompt for file entry 
		cout << "Please type the name of the file to decrypt:";

		

		cin >> readfile; // Entry for filename using variable readfile
		inputFile.open(readfile); // open file entered for readfile

		

		cout << "\n File loaded..." << endl << endl;
		cout << "Please enter the three encryption codes." << endl << endl;
		cout << "Enter integrals 0-26 for each code." << endl << endl;
		
		//Variables Declared for the three entries. 
		int entry1; // First entry for cypher
		int entry2; // Second entry for cypher
		int entry3; // Third entry for cypher
		
		// User input for encryption codes BEGIN
		// Entry for first integer
		cout << "\nEnter first integer here--->";
		cin >> entry1;  

		//Check for valid first integer
		while (entry1 < 0 || entry1 > 26)
		{
			cout << "\n ERROR: Invalid Number";
			cout << "\n \n Please enter your first number again--->";
			cin >> entry1;
		}
		
		// Entry for second integer
		cout << "\nEnter the second integer here--->";
		cin >> entry2; 
		
		// Check for valid second integer
				while (entry2 < 0 || entry2 > 26)
		{
			cout << "\n ERROR: Invalid Number";
			cout << "\n \n Please enter the second integer again--->";
			cin >> entry2;
		}
		
		// Entry for third integer
		cout << "\n Enter third integer here--->";
		cin >> entry3; 

		// Check for valid third integer
		while (entry3 < 0 || entry3 > 26)
		{
			cout << "\n ERROR: Invalid Number";
			cout << "\n \n Please enter the third number again--->";
			cin >> entry3;
		}

		// Display the integers picked
		cout << "\n You have inputed the following codes:" << endl << endl;
	    cout << entry1 << endl << endl;
		cout << entry2 << endl << endl;
		cout << entry3 << endl << endl;
	
		} while ( !inputFile ); //test for errors [invalid / non-existant file names]

	return 0;
}
 
Why not take care of making sure the input file is valid before doing all the number choosing stuff?

Code:
cin >> readfile; // Entry for filename using variable readfile
cout << "readfile has been inputted to: " << readfile << endl;
inputFile.open(readfile); // open file entered for readfile
if(!inputFile)
{
            cout << "inputFile invalid" << endl;
            return -1;
}
Leads to:

Please type the name of the file to decrypt:fake.txt
readfile has been inputted to: fake.txt
inputFile invalid
Figure out how to loop that until you get a valid file, and ask for encryption codes after you have a valid file.
 
@ Trombe,

Thanks for the reply. I came up with a similar solution. Yours works for sure but she only wants one return statement in our programs.

Now I have it reading the file and converting it into ascii numbers. Now the main goal of the program is to take the variables entry1 entry2 and entry 3 to decode the message.

ex: entry1=12
entry2=5
entry3=16

Yw. Bxuxaqwi is in the file, so it converts to ascii and I have to unscramble it by sequencing the letters in order using 12 5 16. The numbers are used in 0-25 being the alphabet.

so: Yw. Bxuxaqwi 12 5 16 12 5 16 12 5 16... etc..

What do you think the best way to do this is ? Some weird form of switch statements?


Code:
#include <iostream> // include iostream standard
#include <iomanip> // include io manipulation
#include <fstream> // include file stream
#include <cmath> // include cmath libraries
#include <cctype> // include cctypes. 
using namespace std; // standard namespace 
// end header 

// start main function 
int main ()
{	// declaration for char length,
	const int length = 26;
	
	// length of allowed files using const of 26 characters.
	char readfile[length]; 
	// variable to do another decryption
	char doagain;

	// Begin Printing Main Menu. 
	cout << "================================================" << endl;
	cout << "Welcome to the Austin Powers Decryption Program!" << endl;
	cout << "================================================" << endl;
	// End Printing Main Menu 

	ifstream inputFile; // inputFile declared name for function to read .txt's
	do
	{
		//Print prompt for file entry 
		cout << "Please type the name of the file to decrypt:";


		cin >> readfile; // Entry for filename using variable readfile
		inputFile.open(readfile); // open file entered for readfile
		while (!inputFile)
		{
			inputFile.clear();
			cout << "\n Invalid File. Enter file name again please--->";
			cin >> readfile; 
			inputFile.open(readfile);
		}
		
		cout << "\n File loaded..." << endl << endl;
		cout << "Please enter the three encryption codes." << endl << endl;
		cout << "Enter integrals 0-26 for each code." << endl << endl;
		
		//Variables Declared for the three entries. 
		int entry1; // First entry for cypher
		int entry2; // Second entry for cypher
		int entry3; // Third entry for cypher
		
		// User input for encryption codes BEGIN
		// Entry for first integer
		cout << "\nEnter first integer here--->";
		cin >> entry1;  

		//Check for valid first integer
		while (entry1 < 0 || entry1 > 26)
		{
			cout << " ERROR: Invalid Number";
			cout << "\n Please enter your first number again--->";
			cin >> entry1;
		}
		
		// Entry for second integer
		cout << "Enter the second integer here--->";
		cin >> entry2; 
		
		// Check for valid second integer
				while (entry2 < 0 || entry2 > 26)
		{
			cout << "\n ERROR: Invalid Number";
			cout << "\n Please enter the second integer again--->";
			cin >> entry2;
		}
		
		// Entry for third integer
		cout << "Enter third integer here--->";
		cin >> entry3; 

		// Check for valid third integer
		while (entry3 < 0 || entry3 > 26)
		{
			cout << "\n ERROR: Invalid Number";
			cout << "\n Please enter the third number again--->";
			cin >> entry3;
		}

		// Display the integers picked
		cout << "\n You have inputed the following codes:" << endl << endl;
	    cout << entry1 << endl << endl;
		cout << entry2 << endl << endl;
		cout << entry3 << endl << endl;

		cout << "Using the keys:";
		cout << entry1 << entry2 << entry3;
		cout << "\n \n The decrypted message is as follows:";

		//*****************************DECRYPTION CODE*************************
		char characters; //variable used for reading characters in file
		int conversion1; //variable to store characters for conversion

		while (inputFile.get(characters))
		{
			if (isupper(characters))
			{
				conversion1 = static_cast<int>(characters);
			}
			else if (islower(characters))
			{
				conversion1 = static_cast<int>(characters);
			}
			else
			{
			}

			cout << conversion1;



		}
		//**********************END DECRYPTION CODE****************************
		cout << "\n \n Would you like to decrypt another message?";
		cout << "\n \n Please use Y or y for yes and N or n for no--->";
			cin >> doagain;
		while ( doagain != 'N' && doagain != 'n' && doagain != 'Y' && doagain != 'y' )
		{
			cout << "\n Not a valid option, please read the prompt.";
			cout << "\n \n Would you like to decrypt another file?--->";
			cin >> doagain;
		}
		} while ( doagain == 'Y' || doagain =='y' ); //test for errors [invalid / non-existant file names]

	return 0;
}
 
I am not sure if this is getting too tricky for an introductory type class (what this looks like), but you should research the modulus operator (%). You will then want to put your shifts into a small, 3 position, array and use the index in the file modulus 3 to find the proper shift. Are spaces shifted as well? I sure hope so...
 
I am not sure if this is getting too tricky for an introductory type class (what this looks like), but you should research the modulus operator (%). You will then want to put your shifts into a small, 3 position, array and use the index in the file modulus 3 to find the proper shift. Are spaces shifted as well? I sure hope so...

cannot use arrays. >.< Trust me , I wanted to lol
 
You can still use the modulus operator, though. Your if statement just ends up being something like if index%3==0, then shift by 12, else if index%3==1, then shift by 5, else if index%3==2, then shift by 16. The array I suggested was to just store the values. I guess you could use an enumeration... that would make more sense than an array, anyway. Not sure what was wrong with me earlier. Of course, at this level, there is nothing wrong with just using the numbers in the if statements.
 
You can still use the modulus operator, though. Your if statement just ends up being something like if index%3==0, then shift by 12, else if index%3==1, then shift by 5, else if index%3==2, then shift by 16. The array I suggested was to just store the values. I guess you could use an enumeration... that would make more sense than an array, anyway. Not sure what was wrong with me earlier. Of course, at this level, there is nothing wrong with just using the numbers in the if statements.

hmm ok. makes sense. I have a formula I came up with that works. Ill try to put it in.

Now what would you do to make it print letters from the ascii ? If I use the formula to even get the correct ascii, its in ascii?
 
Doh! I just noticed that you take the shift values from the user... sorry, just put your variables in the if statements. Anyway, just cast your int as a char - cout << (char)conversion1

You can look at this reference to see the ASCII codes and what letters they represent.. If you need some more help, you can look for ROT13 code to get ideas.

Thank you much sir. I got it spitting out letters now.. Hoorah!

One question, see my while loop? shouldnt it prevent the punctuation marks and spaces from turning into letters?

If you compile this, put it with the attached whiskers.txt file to see what I mean.

Code:
#include <iostream> // include iostream standard
#include <iomanip> // include io manipulation
#include <fstream> // include file stream
#include <cmath> // include cmath libraries
#include <cctype> // include cctypes. 
using namespace std; // standard namespace 
// end header 

// start main function 
int main ()
{	// declaration for char length,
	const int length = 26;
	
	// length of allowed files using const of 26 characters.
	char readfile[length]; 
	// variable to do another decryption
	char doagain;

	// Begin Printing Main Menu. 
	cout << "================================================" << endl;
	cout << "Welcome to the Decryption Program!" << endl;
	cout << "================================================" << endl;
	// End Printing Main Menu 

	ifstream inputFile; // inputFile declared name for function to read .txt's
	do
	{
		//Print prompt for file entry 
		cout << "Please type the name of the file to decrypt:";


		cin >> readfile; // Entry for filename using variable readfile
		inputFile.open(readfile); // open file entered for readfile
		while (!inputFile)
		{
			inputFile.clear();
			cout << "\n Invalid File. Enter file name again please--->";
			cin >> readfile; 
			inputFile.open(readfile);
		}
		
		cout << "\n File loaded..." << endl << endl;
		cout << "Please enter the three encryption codes." << endl << endl;
		cout << "Enter integrals 0-26 for each code." << endl << endl;
		
		//Variables Declared for the three entries. 
		int entry1; // First entry for cypher
		int entry2; // Second entry for cypher
		int entry3; // Third entry for cypher
		
		// User input for encryption codes BEGIN
		// Entry for first integer
		cout << "\nEnter first integer here--->";
		cin >> entry1;  

		//Check for valid first integer
		while (entry1 < 0 || entry1 > 26)
		{
			cout << " ERROR: Invalid Number";
			cout << "\n Please enter your first number again--->";
			cin >> entry1;
		}
		
		// Entry for second integer
		cout << "Enter the second integer here--->";
		cin >> entry2; 
		
		// Check for valid second integer
				while (entry2 < 0 || entry2 > 26)
		{
			cout << "\n ERROR: Invalid Number";
			cout << "\n Please enter the second integer again--->";
			cin >> entry2;
		}
		
		// Entry for third integer
		cout << "Enter third integer here--->";
		cin >> entry3; 

		// Check for valid third integer
		while (entry3 < 0 || entry3 > 26)
		{
			cout << "\n ERROR: Invalid Number";
			cout << "\n Please enter the third number again--->";
			cin >> entry3;
		}

		// Display the integers picked
		cout << "\n You have inputed the following codes:" << endl << endl;
	    cout << entry1 << endl << endl;
		cout << entry2 << endl << endl;
		cout << entry3 << endl << endl;

		cout << "Using the keys:";
		cout << entry1 << entry2 << entry3;
		cout << "\n \n The decrypted message is as follows:";

		//*****************************DECRYPTION CODE*************************
		char characters; //variable used for reading characters in file
		int conversion1; //variable to store characters for conversion

		while (inputFile.get(characters))
		{
			if (isupper(characters))
			{
				conversion1 = static_cast<int>(characters);
			}
			else if (islower(characters))
			{
				conversion1 = static_cast<int>(characters);
			}
			else
			{
			}

			cout << (char)conversion1;



		}
		//**********************END DECRYPTION CODE****************************
		cout << "\n \n Would you like to decrypt another message?";
		cout << "\n \n Please use Y or y for yes and N or n for no--->";
			cin >> doagain;
		while ( doagain != 'N' && doagain != 'n' && doagain != 'Y' && doagain != 'y' )
		{
			cout << "\n Not a valid option, please read the prompt.";
			cout << "\n \n Would you like to decrypt another file?--->";
			cin >> doagain;
		}
		} while ( doagain == 'Y' || doagain =='y' ); //test for errors [invalid / non-existant file names]

	return 0;
}
 

Attachments

  • whiskers.txt
    12 bytes · Views: 78
i dont think you updated your code when you posted last. that code doesnt spit out letters like you say... you dont have anything in your 'else' function... you would have to say conversion1 = characters //characters remain unchanged if not upper or lower case letter.
what code di you use to decrypt the text?
 
Sorry for dropping out of the conversation for a bit. Are you still having trouble? I tried running your code, but it did not seem to read the file at all for me. It's been years since I did anything in C++, so I am sure it was something I did wrong.
 
Back