0

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

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

错误:未在此范围内声明“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;   

}

4

1 回答 1

1

变量text从未被声明过。您可以使用char*or 为了更方便std::string,并使用其成员函数c_str()传递给strlen. (除了你可以使用字符串的size()成员函数)

Strlen 是其中的一部分<cstring>

#include <cstring>
于 2013-08-01T09:26:10.087 回答