so there's something in my program that is not going the way I feel like it should go. If I could get some help I would appreciate it. I'll explain how it's supposed to work first and follow up with my question.
So I wrote an encryption program that asks the user to input a string and then it encrypts it and creates a file called "secret.dat" and puts the encrypted phrase in there.
If the user were to put in the phrase:
hello world 123
it would send this into the file:
11spwwzshzcwoh234&6#12
The "11" indicates how many letters the letter was shifted to the right by. This is followed by the phrase he entered in encrypted. The '&' character shows where the encryption ends. Each space in his phrase uses the previous letter and shifts it over by 4 and finally after the '&' character it tells the number location of where the spaces are seperated by a '#' character.
The current program I am writing decrypts the "secret.dat" file and shows his phrase on the screen.
This is what I have so far:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
//Declare Variables
int shift;
ifstream inData;
string input;
string output;
int length;
//Open file
inData.open("secret.dat");
//Begin program
inData >> shift;
getline(inData, input, '&');
length = input.length();
for (int count = 0; count < length; count++)
{
if (input[count] >= 'a' && input[count] <= 'z')
{
output += ((input[count] - 'a' - shift + 26) % 26) + 'a';
}
else if (input[count] >= '0' && input[count] <= '9')
{
output += ((input[count] - '0' - shift + 10) % 10) + '0';
}
}
//Declare variables for location of spaces
int i = 0;
char ignore;
int spaces[20];
int location;
//Begin finding the spaces
while (!EOF)
{
inData >> location;
spaces[i] = location;
inData >> ignore;
}
//Preview each result to compare and make sure they are working right
cout << shift << endl;
cout << input << endl;
cout << output << endl;
cout << spaces[0] << endl;
cout << spaces[1] << endl;
return 0;
}
This is what I get as a result
11
spwwzshzswoh234
hellohworldw123
4704512
0
Obviously the last 2 lines are not working correctly (NOTE: this is not how I am going to display it, I just printed these to the screen so that I could see what the result was and make sure it was correct, which it isn't).
So, my question is why my while loop isn't working correctly. It gives me a bunch of random numbers in the first array slot when it should put a 6 in the first spot, then it should skip the next character and then put a 12 in the second spot in the array which it just puts a 0 there. If I just call an integer from the file outside of the while loop it gives me a 6 no problem so I'm unsure on why it is doing this. I figured it would put the first integer in the first slot in the array, then skip the next character, then put the next integer in the array and skip the next character and so on until the end of the file which is why I did it in a while loop and called an integer first, then put that integer into the array, and then called a character which I won't use and have it repeat until the end of the file. The reason I'm doing this is so that I have an array with the location of the spaces in it so that I can use the array to replace the letters that should be spaces with a space character ' '.
Thanks for anyone willing to help!