Hi all,
I am trying to sort out a problem with my code. This is a school homework assignment so if you don't want to give me the answer in understand. My program works fine until it gets to the scanf under the decrypt data section. At the console for scanf, you can enter integer after integer and nothing happens.
What am I doing wrong? Code below.
Decrypt Function:
Any advice would be great.
JohnMC
I am trying to sort out a problem with my code. This is a school homework assignment so if you don't want to give me the answer in understand. My program works fine until it gets to the scanf under the decrypt data section. At the console for scanf, you can enter integer after integer and nothing happens.
What am I doing wrong? Code below.
Code:
#include <iostream>
#include <fstream>
#include "stdio.h"
#include "string.h"
#define STRING_LENGTH 120
#define MAX_SIZE 4096
using namespace::std;
extern void encript(char *UnEncriptArray, char EncriptArray[], int Key, int stringLength);
extern void decript(char *EncriptArray, char UnEncriptArray[], int Key, int stringLength);
int main (int argc, char * const argv[]) {
char inFileName[STRING_LENGTH];
char outFileName[STRING_LENGTH];
char arrayIn[MAX_SIZE];
char arrayOut[MAX_SIZE];
int Key;
cout << "Welcome to <Encrypt/Decrypt>!!! \n" << endl;
// DECODE FUNCTION
cout << "You are in Decrypt Mode. \n" << endl;
//asks user for file names
cout << "Please enter the name of the file to get text from:" << endl;
cin >> inFileName;
cout << "Please enter the name of the file to save text to:" << endl;
cin >> outFileName;
//creates file streams from file names given above
ifstream dataIn(inFileName, ios::in);
ofstream dataOut(outFileName, ios::out);
//FOR TESTING ONLY
//prints status of file streams
cout << "In File: " << inFileName << dataIn.is_open() << endl;
cout << "Out File: " << outFileName << dataOut.is_open() << endl;
//loads data from input location into data array
int positionIn = 0;
char positionChar;
while (!dataIn.eof()) {
dataIn >> positionChar;
positionChar = toupper(positionChar);
arrayIn[positionIn] = positionChar;
positionIn++;
}
arrayIn[strlen(arrayIn)-1] = 0; //removes the "extra" char
//decrypt data
cout << "Please enter the key value <int>: ";
scanf("%d", &Key);
decript(arrayIn, arrayOut, Key, strlen(arrayIn));
cout << "The key decrypted the text as: " << endl;
cout << arrayOut << endl;
//output data
dataOut << "Output from Encrypt/Decrypt." << endl;
dataOut << "Decryption Key Used: ";
dataOut << Key << endl;
dataOut << "The decrypted text is: ";
dataOut << arrayOut << endl;
cout << "Thank You for using Encrypt/Decrypt. Your data has been saved to your output file.";
return 0;
}
Decrypt Function:
Code:
void decript(char *EncriptArray, char UnEncriptArray[], int Key, int stringLength) {
int decodeWorking;
int wrapValue;
for (int i = 0; i < stringLength; i++) {
decodeWorking = (int)EncriptArray[i] - Key;
if (decodeWorking < 64) {
wrapValue = 64 - decodeWorking;
decodeWorking = 90 - wrapValue;
}
UnEncriptArray[i] = (char)decodeWorking;
}
}
Any advice would be great.
JohnMC