I just started using C++ and i have to create a program where the user enters a file name to decrypt and three integers 0-26 corresponding to the letters in the alphabet. M+12=Y. How can I use a static_cast to create the decryption code? I'm not sure how to set it up. My program also won't open the file. Any help would be greatly appreciated!
This is what I have so far:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
cout << "===============================================" << endl; //display title menu
cout << "Welcome to the Austin Powers Decryption Program" << endl;
cout << "===============================================" << endl << endl;
ifstream inputFile;
do
{
cout << "Please enter the name of the file to decode: " << endl; //ask the user for fileName
cin >> fileName;
inputFile.open (fileName); //open fileName
while (inputFile == false)
{
inputFile.clear ();
cout << fileName << " is not a valid file name. Please re-enter: ";
cin >> fileName;
inputFile.open ("fileName");
}
cout << "Next you will be prompted for the 3 decryption codes." << endl;
cout << "Please enter integers in the range 0 to 26 for each code." << endl;
int integer1; // identify integers
int integer2;
int integer3;
cout << "Please enter the first integer: "; // ask user for the first integer
cin >> integer1;
while (integer1 < 0 || integer1 > 26) // check that integer is valid
{
cout << "Invalid. Please enter again: ";
cin >> integer1;
}
cout << "Please enter the second integer: "; // ask user for the second integer
cin >> integer2;
while (integer2 < 0 || integer2 >26) // check that the integer in valid
{
cout << "Invalid. Please enter again: ";
cin >> integer2;
}
cout << "Please enter the third integer: "; // ask user for third integer
cin >> integer3;
while (integer3 < 0 || integer3 > 26) // check that the integer is valid
{
cout << "Invalid. Please enter again: ";
cin >> integer3;
}
First note that an unsigned integer will always be a positive value, this simplifies your "if(...)" checks.
Next, this is a Ceasar shift encryption wtf are integer1, 2 and 3 for? I'm sorry I see what you are doing with them. This input method was unexpected.
As for your files name, you are passing a literal string to the open function of the istream object, so not only is it case sensitive but you need to include the file extention as well. Also please note that open does not utilize environmental variables such as PATH; so your file needs to either be specifically located, in UNC format or in the same directory as your executable.
Also, this inputFile.open ("fileName"); located 23 lines from your Main(...) entry point is incorrect. The double quotes indicate a string not a variable so no matter what you enter for the variable fileName, the open function will look for a file literally named "fileName"
Yeah, you take the integer value and cast it to a char, but if that's the case then you need to check it against the ASCII table instead of what you are doing here. http://en.wikipedia.org/wiki/ASCII_table