My code is finally running but not properly, I think it the offset but it might be something else, could someone have a look my piece of code and tell me what is wrong and how I can fix it please. Thank you.
#include <iostream>
#include <string>
#include <stdio.h>
#include <string.h>
usingnamespace std;
int main()
{
char Letter, LetterBeingExamined, LetterBeingExaminedPlusOffset;
int Offset = 0;
int MessageLength = 0;
int i = 0;
int ASCIICodeLetter[100];
Offset = 5;
cout << "Enter letter to be encrypted: ";
cin >> Letter;
MessageLength = 1;
cout << "\nThe letter you would like to Encrypt is: " << Letter;
cout << "\n\nThe length of the Letter you want to Encrypt is " << MessageLength << " byte(s) (including spaces).\n";
for (int i = 0; i < MessageLength; i = i + 1)
{
Letter = LetterBeingExamined;
cout << "ASCII number value of letter you have input" << LetterBeingExamined << "is: " << (int) LetterBeingExamined << endl;
ASCIICodeLetter[i] = (int) LetterBeingExamined;
}
if ((int(LetterBeingExamined) >= 65) && (int(LetterBeingExamined)<= 90))
LetterBeingExaminedPlusOffset = (int) LetterBeingExamined + Offset;
cout << "\nValue of letter plus Offset "<< int(LetterBeingExaminedPlusOffset);
if (LetterBeingExaminedPlusOffset > 90)
cout <<"\n\nValue of letter plus offset is greater than 90.";
LetterBeingExaminedPlusOffset = LetterBeingExaminedPlusOffset - 26;
LetterBeingExaminedPlusOffset = int(LetterBeingExamined);
cout <<"\n\nValue of Encrypted Letter after subracting 26 (if necessary) is " << int(LetterBeingExaminedPlusOffset);
ASCIICodeLetter[i] = LetterBeingExaminedPlusOffset;
ASCIICodeLetter[i] = LetterBeingExaminedPlusOffset;
cout << "\nEncrypted letter is " << LetterBeingExaminedPlusOffset << ".\n";
return 0;
}
This is what the screenshot looks like;
Enter letter to be encrypted: A
The letter you would like to Encrypt is: A
The length of the letter you want to encrypt is 1 byte<s> (including spaces).
ASCII number value of letter you have input is: 0
Value of letter plus offset 0
Value of encrypted letter after subtracting 26 <if necessary> is 0
Encrypted letter is .
When executing this code, Letter contains the letter the user wrote, but LetterBeingExamined contains a random value (you never actually set the variable, so it isn't initialized). Here the input of the user will be discarded and overwritten by an uninitialized value. It's hard to tell what happens after this, since the assignment is undefined behavior (using an uninitialized variable). This might be causing your problem.