i=0 is failing me
Jan 27, 2013 at 10:51am UTC
This is a vigenere encryption program for school. For some reason when i = 0
Things aren't working properly and I'm not sure why. Here's what I got. Every other value of i is doing what it's meant to.
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
#include <iostream>
#include "algorithms.h"
#include <cctype>
#include <cstring>
#include "Assign2.h"
#define MAX_MSG_SIZE 256
#define MIN_ASCII_VALUE ('a')
#define MAX_ASCII_VALUE ('z')
#define NUM_CHARACTERS (MAX_ASCII_VALUE - MIN_ASCII_VALUE + 1)
#define CHAR_OUT_OF_RANGE(c) ( (c) < MIN_ASCII_VALUE || (c) > MAX_ASCII_VALUE )
using namespace std;
int positiveMod(int x)
{
int mod = x % NUM_CHARACTERS;
if (mod < 0)
mod += NUM_CHARACTERS;
return mod;
}
char codeBounds(int key , char msgBlock)
{
int difference = msgBlock + key - MIN_ASCII_VALUE;
int value = MIN_ASCII_VALUE + positiveMod(difference);
return value;
}
void vigenereEncrypt( const char plaintext[],
char ciphertext[],
const char key[] )
{
int fieldSize = strlen(plaintext);
int keySize = strlen(key);
int i = 0;
for (int inc = 0 ; inc < fieldSize ; ++inc , ++i)
{
if (isspace(plaintext[inc]) || isdigit(plaintext[inc]) || ispunct(plaintext[inc]) )
{
ciphertext[inc] = plaintext[inc];
continue ;
}
if (CHAR_OUT_OF_RANGE(plaintext[inc] + (key[i] - MIN_ASCII_VALUE)))
{
ciphertext[inc] = codeBounds((key[i] - MIN_ASCII_VALUE ) , plaintext[inc]);
}
else
ciphertext[inc] = plaintext[inc] + (key[i] - MIN_ASCII_VALUE );
if (i == keySize )
i = 0;
}
ciphertext[fieldSize] = '\0' ;
}
Last edited on Jan 27, 2013 at 10:52am UTC
Topic archived. No new replies allowed.