'No syntax errors but code not showing the output
I wrote the code for multiple pre-defined functions, however, the output is not showing at all. What should I do?
It should be showing a text for the encrypted and decrypted messages but the output is blank there.
Here's the code:
#include "enigma.h"
const char *ROTOR_CONSTANTS[] = {
"ABCDEFGHIJKLMNOPQRSTUVWXYZ", // Identity Rotor (index 0 - and useful for testing):
"EKMFLGDQVZNTOWYHXUSPAIBRCJ",
"AJDKSIRUXBLHWTMCQGZNPYFVOE",
"BDFHJLCPRTXVZNYEIWGAKMUSQO",
"ESOVPZJAYQUIRHXLNFTGKDCMWB",
"VZBRGITYUPSDNHLXAWMJQOFECK",
"JPGVOUMFYQBENHZRDKASXLICTW",
"NZJHGRCXMYSWBOUFAIVLPEKQDT",
"FKQHTLXOCBJSPDZRAMEWNIUYGV",
};
void String_func(int flag, char in[], char out[]) {
flag = (flag > 0) ? flag : 0;
int i = 0;
for (int i = 0; in[i] != '\0'; i++) {
out[i+flag] = in[i];
}
out[i+flag] = '\0';
}
// This method reads a character string from the keyboard and
// stores the string in the parameter msg.
// Keyboard input will be entirely uppercase and spaces followed by
// the enter key.
// The string msg should contain only uppercase letters spaces and
// terminated by the '\0' character
// Do not include the \n entered from the keyboard
void Get_Message(char msg[]){
int i = 0;
char ch;
while ((ch=getchar()) != '\n') {
msg[i] = ch;
++i;
}
msg[i] = '\0';
return;
}
// This function reads up to 4 characters from the keyboard
// These characters will be only digits 1 through 8. The user
// will press enter when finished entering the digits.
// The rotor string filled in by the function should only contain
// the digits terminated by the '\0' character. Do not include
// the \n entered by the user.
// The function returns the number of active rotors.
int Get_Which_Rotors(char which_rotors[]){
char arr[5];
scanf("%4s", arr);
int value = 0; //stores integer value of which rotor to be called
int i = 0;
while (arr[i] != '\0') {
if (arr[i] != ' ')
value++;
i++;
}
String_func(0, arr, which_rotors);
return value;
}
// This function reads an integer from the keyboard and returns it
// This number represents the number of rotations to apply to the
// encryption rotors. The input will be between 0 and 25 inclusive
int Get_Rotations(){
int rot; //rotations
scanf("%d", &rot);
return rot;
}
// This function copies the rotors indicated in the which_rotors string
// into the encryption_rotors. For example if which rotors contains the string
// {'3', '1', '\0'} Then this function copies the third and first rotors into the
// encryption rotors array in positions 0 and 1.
// encryptions_rotors[0] = "BDFHJLCPRTXVZNYEIWGAKMUSQO"
// encryptions_rotors[1] = "EKMFLGDQVZNTOWYHXUSPAIBRCJ"
void Set_Up_Rotors(char encryption_rotors[4][27], char which_rotors[5]){
int i, enc = 0;
i = 0;
while (which_rotors[i] != '\0') {
if (which_rotors[i] != ' ') {
String_func(0, ROTOR_CONSTANTS[int(which_rotors[i])], encryption_rotors[enc++]);
}
i++;
}
if (i < 3) {
int j = i;
while (j <= 3) {
encryption_rotors[j][0] = '\0';
++j;
}
}
return;
}
// This function rotates the characters in each of the active encryption rotors
// to the right by rotations. For example if rotations is 3 encryption_rotors[0]
// contains "BDFHJLCPRTXVZNYEIWGAKMUSQO" then after rotation this row will contain
// SQOBDFHJLCPRTXVZNYEIWGAKMU. Apply the same rotation to all for strings in
// encryption_rotors
void Apply_Rotation(int rotations, char encryption_rotors[4][27]) {
int index = 0;
int i = 0;
char rot[rotations+1];
char rotator[27+rotations];
while (encryption_rotors[i][0] != '\0') {
int j = 26-rotations;
while (j < 26) {
rot[index++] = encryption_rotors[i][j];
++j;
}
rot[index] = '\0';
String_func(rotations, encryption_rotors[i], rotator);
int k = 0;
while (j < rotations) {
rotator[k] = rot[k];
++k;
}
rotator[26] = '\0';
String_func(0, rotator, encryption_rotors[i]);
index = 0;
++i;
}
return;
}
// This function takes a string msg and applys the enigma machine to encrypt the message
// The encrypted message is stored in the string encryped_msg
// Do not change spaces, make sure your encryped_msg is a \0 terminated string
void Encrypt(char encryption_rotors[4][27], int num_active_rotors, char msg[], char encrypted_msg[]){
String_func(0, msg, encrypted_msg);
int i = 0;
while (i < num_active_rotors) {
int j = 0;
while (encrypted_msg[j] != '\0') {
if (encrypted_msg[j] == ' ')
continue;
int index = encrypted_msg[j] - 65;
encrypted_msg[j] = encryption_rotors[i][index];
++j;
}
++i;
}
return;
}
// This function takes a string msg and applys the enigma machine to decrypt the message
// The encrypted message is stored in the string encryped_msg and the decrypted_message
// is returned as a call by reference variable
// remember the encryption rotors must be used in the reverse order to decrypt a message
// Do not change spaces, make sure your decrytped_msg is a \0 terminated string
void Decrypt(char encryption_rotors[4][27], int num_active_rotors, char encrypted_msg[], char decrypted_msg[]) {
String_func(0, encrypted_msg, decrypted_msg);
int i = num_active_rotors - 1;
while (i >= 0) {
int j = 0;
while (decrypted_msg[j] != '\0') {
if(decrypted_msg[j] == ' ')
continue;
int whichIndex = LookForIndex(decrypted_msg[j], encryption_rotors[i]);
decrypted_msg[j] = ROTOR_CONSTANTS[0][whichIndex];
++j;
}
--i;
}
}
And the code for the driver is this:
#include "enigma.h"
int main() {
char message[80];
char encrypted_message[80];
char decrypted_message[80];
char which_rotors[5];
char encryption_rotors[4][27];
int rotations;
int num_active_rotors;
printf("Enter the message to be encrypted or decrypted: ");
Get_Message(message);
printf("\nWhich rotors will be used to encrypt the message: ");
num_active_rotors = Get_Which_Rotors(which_rotors);
printf("\nEnter the number of rotations to apply to the encryption rotors: ");
rotations = Get_Rotations();
Set_Up_Rotors(encryption_rotors, which_rotors);
Apply_Rotation(rotations, encryption_rotors);
Encrypt(encryption_rotors, num_active_rotors, message, encrypted_message);
Decrypt(encryption_rotors, num_active_rotors, encrypted_message, decrypted_message);
printf("The encrypted message is: %s", encrypted_message);
printf("The decrypted message is: %s", decrypted_message);
return 0;
}
Do I edit the enigma_driver.c? I am really unsure what is going on as c is very new to me and it took a lot of time to write this project.
Solution 1:[1]
int i = 0;
for (int i = 0; in[i] != '\0'; i++) {
out[i+flag] = in[i];
}
out[i+flag] = '\0';
You have two variables called i. The one declared in the for statement is the one that gets incremented. So when you execute the last line, i is always zero.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | David Schwartz |
