Help with C++ opening and reading file
Mar 4, 2013 at 5:36pm UTC
I've included the code I have written but I am stuck. I created a text file but I'm not sure where to save it to have it open when I input the name. Also, I need to include in the encrypt function that it is writing the encrypted message to a text file called output.txt and I'm not sure how to do that.
Any help would be greatly appreciated! Thanks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void encrypt ()
{
string fileName;
string message;
cout <<"Enter the file name containing the message you would like to encrypt: " ;
cin >> fileName;
ifstream inFile;
inFile.open(fileName);
getline(inFile, message);
for (int x=0; x<message.length(); x++)
{
if (message[x] == 'A' || message[x] == 'a' )
message[x] = 'B' ;
else if (message[x] == 'B' || message[x] == 'b' )
message[x] = 'C' ;
else if (message[x] == 'C' || message[x] == 'c' )
message[x] = 'D' ;
else if (message[x] == 'D' || message[x] == 'd' )
message[x] = 'E' ;
else if (message[x] == 'E' || message[x] == 'e' )
message[x] = 'F' ;
else if (message[x] == 'F' || message[x] == 'f' )
message[x] = 'G' ;
else if (message[x] == 'G' || message[x] == 'g' )
message[x] = 'H' ;
else if (message[x] == 'H' || message[x] == 'h' )
message[x] = 'I' ;
else if (message[x] == 'I' || message[x] == 'i' )
message[x] = 'J' ;
else if (message[x] == 'J' || message[x] == 'j' )
message[x] = 'K' ;
else if (message[x] == 'K' || message[x] == 'k' )
message[x] = 'L' ;
else if (message[x] == 'L' || message[x] == 'l' )
message[x] = 'M' ;
else if (message[x] == 'M' || message[x] == 'm' )
message[x] = 'N' ;
else if (message[x] == 'N' || message[x] == 'n' )
message[x] = 'O' ;
else if (message[x] == 'O' || message[x] == 'o' )
message[x] = 'P' ;
else if (message[x] == 'P' || message[x] == 'p' )
message[x] = 'Q' ;
else if (message[x] == 'Q' || message[x] == 'q' )
message[x] = 'R' ;
else if (message[x] == 'R' || message[x] == 'r' )
message[x] = 'S' ;
else if (message[x] == 'S' || message[x] == 's' )
message[x] = 'T' ;
else if (message[x] == 'T' || message[x] == 't' )
message[x] = 'U' ;
else if (message[x] == 'U' || message[x] == 'u' )
message[x] = 'V' ;
else if (message[x] == 'V' || message[x] == 'v' )
message[x] = 'W' ;
else if (message[x] == 'W' || message[x] == 'w' )
message[x] = 'X' ;
else if (message[x] == 'X' || message[x] == 'x' )
message[x] = 'Y' ;
else if (message[x] == 'Y' || message[x] == 'y' )
message[x] = 'Z' ;
else if (message[x] == 'Z' || message[x] == 'z' )
message[x] = 'A' ;
inFile.close();
}
cout <<"The encrypted message is: \n" << message;
}
void decrypt ()
{
string fileName;
string message;
cout <<"Enter the file name containing the message you would like to encrypt: " ;
cin >> fileName;
ifstream inFile;
inFile.open(fileName);
getline(inFile, message);
for (int x = 0; x < message.length (); x++)
{
if (message[x] == 'A' || message[x] == 'a' )
message[x] = 'Z' ;
else if (message[x] == 'B' || message[x] == 'b' )
message[x] = 'A' ;
else if (message[x] == 'C' || message[x] == 'c' )
message[x] = 'B' ;
else if (message[x] == 'D' || message[x] == 'd' )
message[x] = 'C' ;
else if (message[x] == 'E' || message[x] == 'e' )
message[x] = 'D' ;
else if (message[x] == 'F' || message[x] == 'f' )
message[x] = 'E' ;
else if (message[x] == 'G' || message[x] == 'g' )
message[x] = 'F' ;
else if (message[x] == 'H' || message[x] == 'h' )
message[x] = 'G' ;
else if (message[x] == 'I' || message[x] == 'i' )
message[x] = 'H' ;
else if (message[x] == 'J' || message[x] == 'j' )
message[x] = 'I' ;
else if (message[x] == 'K' || message[x] == 'k' )
message[x] = 'J' ;
else if (message[x] == 'L' || message[x] == 'l' )
message[x] = 'K' ;
else if (message[x] == 'M' || message[x] == 'm' )
message[x] = 'L' ;
else if (message[x] == 'N' || message[x] == 'n' )
message[x] = 'M' ;
else if (message[x] == 'O' || message[x] == 'o' )
message[x] = 'N' ;
else if (message[x] == 'P' || message[x] == 'p' )
message[x] = 'O' ;
else if (message[x] == 'Q' || message[x] == 'q' )
message[x] = 'P' ;
else if (message[x] == 'R' || message[x] == 'r' )
message[x] = 'Q' ;
else if (message[x] == 'S' || message[x] == 's' )
message[x] = 'R' ;
else if (message[x] == 'T' || message[x] == 't' )
message[x] = 'S' ;
else if (message[x] == 'U' || message[x] == 'u' )
message[x] = 'T' ;
else if (message[x] == 'V' || message[x] == 'v' )
message[x] = 'U' ;
else if (message[x] == 'W' || message[x] == 'w' )
message[x] = 'V' ;
else if (message[x] == 'X' || message[x] == 'x' )
message[x] = 'W' ;
else if (message[x] == 'Y' || message[x] == 'y' )
message[x] = 'X' ;
else if (message[x] == 'Z' || message[x] == 'z' )
message[x] = 'Y' ;
}
cout << "The decrypted message is: \n" << message;
}
void display_menu ()
{
int num;
cout <<"Welcome to the Engima Program!" ;
cout <<"Which of the following would you like to do? <Enter the Number>:" <<endl<<"\t1. Create an encrypted message" <<endl<<"\t2. Decrypt a message" <<endl<<"\t3. Quit" <<endl<<endl;
cin >>num;
if (num ==1)
{
encrypt();
}
else if (num ==2)
{
decrypt();
}
else
{
cout << "Goodbye" ;
}
}
int main()
{
display_menu();
return 0;
}
Last edited on Mar 4, 2013 at 8:18pm UTC
Topic archived. No new replies allowed.