2013-08-01 80 views
0

編譯時,我得到以下錯誤...C++宣佈愷撒密碼

錯誤:「文本」是不是在這個範圍內聲明

錯誤:「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; 

}

+0

好部分,您使用了一個名爲從未宣稱'text'變量。該錯誤消息看起來很清楚。對於'strlen',包含'string.h'或'stdlib.h'(注意''和''是兩個不同的,基本上不相關的頭文件)。 –

+0

如果你沒有告訴我們這個程序在做什麼,我們如何提出改進建議? – 0x499602D2

+0

@IgorTandetnik你能告訴我在哪裏宣佈「文本」變量? – Cris

回答

1

可變text從來沒有宣佈。您可能使用char*或爲了更方便std::string並使用其成員函數c_str()傳遞給strlen。 (除了事實,你可以使用字符串的size()成員函數)

的strlen是<cstring>

#include <cstring>