简体   繁体   中英

C++ declaring Caesar Cipher

I get the following error when compiling...

error: 'text' was not declared in this scope

error: 'strlen' was not declared in this scope

how can i fix this error by declaring them...?

is my code following the rules of Caesar Cipher...

How can I improve this code to make it more efficient???

The program will input file that contains the Caesar cipher, and will output the decipher code in another text...

#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;   

}

Variable text was never declared. You might use char* or for more convenience std::string and use its member function c_str() to pass to strlen . (Apart from the fact that you could use string's size() member function)

Strlen is part of <cstring>

#include <cstring>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM