Hey there again guys,
I am still having a little trouble with this ceasar shift program that I am doing for uni. I have got most of the program down and it seems to work pretty well, there is just one small glitch that I cannot seem to get around, and it is something relatively strait forward. The problem is when a space is encountered it seems to get processed in the function decryptEncryptLine() and the output is moved along and is not right.
input = my name is trevor
output = PB BQDPH HLV VWUHYRU
it seems that whenever i try and incorporate a way of ignoring spaces the output ends up all over the place.
Can someone run my code and look over it and point me in the direction that I should be heading. Its a work in progress and I have only been learning C for a couple of months.
Thanks heaps
Code:
//------------------------------------------------------------------------------
// Author: Airmax (DON'T USE YOUR REAL NAME OR STUDENT NUMBER
// Student No.: 12345678 FOR PEER REVIEWED ASSIGNMENTS.)
// Assessment: Assignment 4
// Purpose: A program to accept alpha characters and encrypt or decrypt them
// using a ceasar shift cycle.
//------------------------------------------------------------------------------
// included libraries
#include <stdio.h>
#include <ctype.h>
//------------------------------------------------------------------------------
// constants
const int UPPER_LIMIT = 3; // Maximum menu choice
const int LOWER_LIMIT = 0; // Minimum menu choice
const int LOWEST_SHIFT = 1; // Lowest shift possible
const int HIGHEST_SHIFT = 25; // Highest shift possible
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Function prototypes
int getValidIntegerInRange(int lowestAllowed, int highestAllowed,char *prompt);
// Purpose: To make sure that the user has entered a valid
// integer within range
// Arguments: Minimum and maximum allowable integers and pointer
// Ret.Val: returns a valid menu choice
// Pre: lowest and highest int are declared and an array storing the
// prompt must exist
// Post: The valid menu choice is passed back to main()
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
int getShiftValue(int minVal, int maxVal);
// Purpose: To get a valid shift value from the user
// Arguments: Minimum and maximum allowabe integers
// Ret.Val: A shift value
// Pre: minVal and maxVal must be declared
// Post: The appropriate shift value will be returned
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void decryptEncryptLine(int shift);
// Purpose: To encrypt or decrypt text using a ceasar shift cycle
// Arguments: The shift value requested by the user
// Ret.Val: Void
// Pre: The shift value has been recieved from the user
// Post: The encrypted or decrypted text will output to the terminal
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//main
int main() {
int choice;
int choice2;
int shiftValue=3; // default shift value
char menu[] = "\nMenu\n====================\n 1. Enter shift value\n 2. "
"Encode\n 3. Decode\n 0. Quit\n====================\nChoice: \0";
while (choice != 0) {
// get valid menu choice
choice = (getValidIntegerInRange(LOWER_LIMIT, UPPER_LIMIT, menu));
switch (choice) {
case 0:
return 0;
break;
case 1:
shiftValue = getShiftValue(LOWEST_SHIFT, HIGHEST_SHIFT);
choice2 = (getValidIntegerInRange(2, 3, menu));
if (choice2 == 2) {
decryptEncryptLine(shiftValue);
}
else if (choice2 == 3) {
decryptEncryptLine(-(shiftValue));
}
break;
case 2:
decryptEncryptLine(shiftValue);
break;
case 3:
decryptEncryptLine(-(shiftValue));
break;
}
}
return 0;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Functions
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// function to get valid menu choice and re prompt if input not valid
int getValidIntegerInRange(int lowestAllowed, int highestAllowed,char *prompt) {
int menuChoice;
int inputsGathered;
// get users choice
printf("%s\n", prompt);
inputsGathered = scanf("%i",&menuChoice);
//validate users input
while(inputsGathered !=1 || menuChoice < lowestAllowed
|| menuChoice > highestAllowed) {
// clear input pipe
scanf("%*[^\n]");
scanf("%*c");
//allow user to re enter inout
printf("Invalid input. Please enter a choice between %i and %i: "
,lowestAllowed, highestAllowed);
inputsGathered = scanf("%i", &menuChoice);
}
// clear input pipe
scanf("%*[^\n]");
scanf("%*c");
//return users choice
return menuChoice;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// a function to get the user to input the shift value
int getShiftValue(int minVal, int maxVal) {
int shiftVal;
int inputsGathered;
// get users shift value
printf("Please enter a shift value between %i and %i: ", minVal, maxVal);
inputsGathered = scanf("%i",&shiftVal);
//validate users input
while(inputsGathered !=1 || shiftVal < minVal || shiftVal > maxVal) {
// clear input pipe
scanf("%*[^\n]");
scanf("%*c");
//allow user to re enter inout
printf("Invalid input. Please enter a shift value between"
" %i and %i: ", minVal, maxVal);
inputsGathered = scanf("%i", &shiftVal);
}
// clear input pipe
scanf("%*[^\n]");
scanf("%*c");
//return users choice
return shiftVal;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// a function to encrypt or decrypt the input string and
// return the encrypted/encrypted equvilent
void decryptEncryptLine(int shift) {
char input;
char output;
const char SENT = '\n';
printf("Please enter characters to be encrypted/decrypted\n\n");
scanf("%c", &input);
while (input != SENT) {
if (input == ' ') {
printf("%c",input);
}
else if (input != ' ') {
input = (toupper(input)); // convert lowercase to uppercase
output = input + (shift); // apply ceasar shift
if (output > 90) { // encode wrap around
output -= 26;
}
else if (output < 65) { // decode wrap around
output += 26;
}
}
printf("%c",output);
scanf("%c", &input);
}
}