gpt4 book ai didi

C++ 声明凯撒密码

转载 作者:行者123 更新时间:2023-11-28 07:29:38 25 4
gpt4 key购买 nike

编译时出现以下错误...

错误:“文本”未在此范围内声明

错误:“strlen”未在此范围内声明

我如何通过声明它们...来修复此错误?

我的代码是否遵循凯撒密码规则...

如何改进此代码以使其更高效???

程序将输入包含凯撒密码的文件,并将解密代码输出到另一个文本中......

#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <string.h>
using namespace std;

int main()

{

// Declarations

string reply;

string inputFileName;

ifstream inputFile;

char character;



cout << "Input file name: ";

getline(cin, inputFileName);



// Open the input file.

inputFile.open(inputFileName.c_str());
// Check the file opened successfully.

if ( ! inputFile.is_open()) {

cout << "Unable to open input file." << endl;

cout << "Press enter to continue...";

getline(cin, reply);

return 1;


}

// This section reads and echo's the file one character (byte) at a time.

while (inputFile.peek() != EOF) {

inputFile.get(character);

//cout << character;
//Don't display the file...

char cipher[sizeof(character)];


//Caesar Cipher code...
int shift;
do {
cout << "enter a value between 1-26 to encrypt the text: ";
cin >> shift;
}
while ((shift <1) || (shift >26));

int size = strlen(character);
int i=0;

for(i=0; i<size; i++)
{
cipher[i] = character[i];
if (islower(cipher[i])) {
cipher[i] = (cipher[i]-'a'+shift)%26+'a';
}
else if (isupper(cipher[i])) {
cipher[i] = (cipher[i]-'A'+shift)%26+'A';
}
}

cipher[size] = '\0';
cout << cipher << endl;


}

cout << "\nEnd of file reached\n" << endl;

// Close the input file stream

inputFile.close();

cout << "Press enter to continue...";

getline(cin, reply);

return 0;

最佳答案

变量 text从未被宣布。您可以使用 char*或者为了更方便 std::string并使用它的成员函数 c_str()传递给 strlen . (除了你可以使用字符串的 size() 成员函数之外)

Strlen 是 <cstring> 的一部分

#include <cstring>

关于C++ 声明凯撒密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17983464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com