reading data from a file

Assignment:


In this assignment, you will write a program that will allow for all Caesar Ciphers, using all rotation values (13 being an example above, from ROT13). You will also allow for rotation from both the left and right.

You will place a message, up to 200 characters (all in lowercase), in an input file. Your main() function shall read data from the file. Since this message may include spaces and newlines, we will use .get to read every character. Your program will also have 7 functions:

• A function to print a character array. This will print to both the screen and to an output file. Hint: to print to an output file from a function, the ofstream needs to be passed by reference.
• A function to initialize a character array, filling it with the alphabet.
• A function called caesar_cipher that accepts the message to encrypt, the size, the rotation amount, the rotation direction (left or right), and the alphabet. This function will call the other functions below to do the encryption. The direction will NOT be passed to any other function; it is ONLY used in its current form in caesar_cipher.
• Another (overloaded) function called caesar_cipher that accepts the message to encrypt, the size, the rotation direction, and the alphabet. Since the rotation amount is undefined, use 13 for ROT13 here. This function will call the other functions below to do the encryption.
• A function called rotate_left that takes the entire message and processes each character. If the character is a letter, then it is sent to the rotate_letter function below, along with rotation amount. If the character is a space or newline, then that that is copied (unchanged) to the encrypted message, in the same position as it was in the original message.
• A function called rotate_right that takes the entire message and processes each character. If the character is a letter, then it is sent to the rotate_letter function below, along with rotation amount. If the character is a space or newline, then that that is copied (unchanged) to the encrypted message, in the same position as it was in the original message.
• A function called rotate_letter that takes one letter from the original message, the rotation amount, and the alphabet array. This function will use the rotation amount to encrypt the one letter, and sends the one letter back. Note that if the rotation is large enough, you may wrap around the alphabet – you need to account for this. You will also need this information about int/char ASCII relationships: http://www.asciitable.com/. We will ONLY be using lowercase letters in the messages.




Below, with the help of a friend, made a code that will successfully print the Caesar Ciphers, using all rotation values (I can type in a message and I get the correct output).

However, I need the code to read data from a file, and translate it. I am unsure how to achieve this, as I am not very familiar with files.

Any help would be GREATLY appreciated :)



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
175
176
177
178
179
180
181
#include <iostream>
#include <conio.h>

using namespace std;

///////////////////////////////////////////////////////
////////.////// CONSTANT VARIABLES ////////////////////
///////////////////////////////////////////////////////

const int LOWER_A = 97;
const int UPPER_A = 65;

const int LOWER_M = 109;
const int UPPER_M = 77;

const int LOWER_N = 110;
const int UPPER_N = 78;

const int LOWER_Z = 122;
const int UPPER_Z = 90;

///////////////////////////////////////////////////////
////////////// FUNCTION PROTOTYPES ////////////////////
///////////////////////////////////////////////////////

char* initialize(char *array);
char* rot(char * input, int i);
char* rotate_left(int n , char* input);
char* rotate_right(int n , char* input);
char rotate_letter(char input , int i);
char *caeser_cipher(char* input);
void print(char *arr);

///////////////////////////////////////////////////////
///////////////// MAIN FUNCTION ///////////////////////
///////////////////////////////////////////////////////

int main(){

char input[200] = {};

cin.getline(input,200,'\n');

cout<<endl;

print(caeser_cipher(input));

cout<<endl<<"Press any key to continue..";

getch();

}

///////////////////////////////////////////////////////
////////////////// FUNCTION - 1 ///////////////////////
///////////////////////////////////////////////////////

char* initialize(char *array)
{

for(int i = 0 ; i<200;i++)

array[i] = '\0';

return array;

}

///////////////////////////////////////////////////////
////////////////// FUNCTION - 2 ///////////////////////
///////////////////////////////////////////////////////

char* rot(char * input, int i)
{

int index = 0;

while(input[index] != '\0' ) {

if(input[index] >= LOWER_A && input[index] <= LOWER_Z)

input[index] = input[index] + i;

if(input[index] > LOWER_Z)

input[index] = (input[index] + i)%26 + LOWER_A;

if(input[index] >= UPPER_A && input[index] <= UPPER_Z)

input[index] = input[index] + i;

if(input[index] > UPPER_Z)

input[index] = (input[index] + i)%26 + UPPER_A;

index++;
}
return input;
}

///////////////////////////////////////////////////////
////////////////// FUNCTION - 3 ///////////////////////
///////////////////////////////////////////////////////

char* rotate_left(int n , char* input)
{
input = rot(input , n);
}

///////////////////////////////////////////////////////
////////////////// FUNCTION - 4 ///////////////////////
///////////////////////////////////////////////////////

char* rotate_right(int n , char* input)
{
input = rot(input , n);
}

///////////////////////////////////////////////////////
////////////////// FUNCTION - 5 ///////////////////////
///////////////////////////////////////////////////////

char rotate_letter(char input , int i)
{
if(input >= LOWER_A && input <= LOWER_Z)
{
input = input + i;
if(input > LOWER_Z)
input = (input + i)%26 + LOWER_A;
}
if(input >= UPPER_A && input <= UPPER_Z)
{
input = input + i;
if(input > UPPER_Z)
input = (input + i)%26 + UPPER_A;
}
}

///////////////////////////////////////////////////////
////////////////// FUNCTION - 6 ///////////////////////
///////////////////////////////////////////////////////

char *caeser_cipher(char* input)
{
int index = 0;

while(input[index] != '\0') {

if(input[index] >= LOWER_A && input[index] <= LOWER_M)

input[index] = input[index] + 13;

else if(input[index] >= LOWER_N && input[index] <= LOWER_Z)

input[index] = input[index] - 13;

else if(input[index] >= UPPER_A && input[index] <= UPPER_M)

input[index] = input[index] + 13;

else if(input[index] <= UPPER_N && input[index] <= UPPER_Z)

input[index] = input[index] - 13;

index++;
}
return input;
}

///////////////////////////////////////////////////////
////////////////// FUNCTION - 7 ///////////////////////
///////////////////////////////////////////////////////

void print(char *arr)
{
int i = 0;
while(arr[i] != '\0'){
cout<<arr[i];
i++;
}
}
11
11
Topic archived. No new replies allowed.