所以我的程序中的某些内容并不像我认为的那样。如果我能得到一些帮助,我会很感激。我将解释它应该如何工作并跟进我的问题。
所以我写了一个加密程序,要求用户输入一个字符串然后加密它并创建一个名为" secret.dat"并将加密的短语放在那里。
如果用户要输入短语:
hello world 123
它会将其发送到文件中:
11spwwzshzcwoh234&6#12
" 11"表示字母向右移动了多少个字母。接下来是他加密输入的短语。 '&'字符显示加密结束的位置。他的短语中的每个空格都使用前一个字母并将其移过4,最后在'&'之后。字符它告诉空格分隔的位置的数字位置#'#'字符。
我正在编写的当前程序解密" secret.dat"文件并在屏幕上显示他的短语。
这是我到目前为止所做的:
#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;
}
这就是我得到的结果
11
spwwzshzswoh234
hellohworldw123
4704512
0
显然最后两行不能正常工作(注意:这不是我要如何显示它,我只是将它们打印到屏幕上,以便我可以看到结果是什么并确保它是正确的,哪个它不是。
所以,我的问题是为什么我的while循环没有正常工作。它在第一个数组插槽中给出了一堆随机数,它应该在第一个位置放置一个6,然后它应该跳过下一个字符,然后在数组中的第二个位置放一个12,它只是在那里放0 。如果我只是在while循环之外的文件中调用一个整数,它给我一个6没问题,所以我不确定它为什么这样做。我想它会将第一个整数放在数组的第一个槽中,然后跳过下一个字符,然后将下一个整数放入数组中并跳过下一个字符,依此类推,直到文件结束,这就是为什么我这样做了在while循环中,首先调用一个整数,然后将该整数放入数组中,然后调用一个我不会使用的字符,并将其重复直到文件末尾。我这样做的原因是我有一个数组,其中包含空格的位置,这样我就可以使用数组来替换应该是空格字母的字母&#39; &#39;
感谢愿意提供帮助的任何人!