在函数之间共享变量 - rand()srand

时间:2015-01-06 00:02:34

标签: c++ variables encryption random srand

我必须创建一个程序,它将收集用户的输入,然后加密&解密那些字符。

我对编码不是很有信心,所以我确信我的代码的很多部分都写得不好而且没有遵循最佳实践,因此我编写了一个简单的算法版本,程序只需添加/从ASCII值中减去2的值,但我发现使用了rand()和srand函数,但我不确定如何在我的加密和解密函数中将它们作为单个值使用(静态变量?)。

目前,这是我的完整代码。

#include <iostream >
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cstring>
#include <fstream>
using namespace std;

char getMenuSelection (char m);
void encrypt (char key[]);
void decrypt (char code[]);

int main ()
{
    ifstream input;
    ifstream in;
    ofstream out;
    char menu;
    char message[80];
    //char codekey;
    //char myname[128];
     menu = 0;

    menu=getMenuSelection(menu);
    if (menu == 'e' || menu == 'E') //Option to Encrypt
    {
        cout << "Enter Message to be encrypted \n";
        cin >> message;
        input.open (message);
        encrypt (message);
        input.close();
         cout << "The Ciphertext is: " << message << endl;
        return main();
    }
     if (menu == 'd' || menu == 'D')
    {
         cout << "Enter Ciphertext to decrypt \n";
         cin >> message;
         input.open (message);
         decrypt (message);
         input.close();
         cout << "The Decrypted message is: " << message << endl;
        return main();
     }
    if (menu == 'q' || menu == 'Q')
     {
         cout << "Program exiting.";
         cin.ignore();
         return 0;
    }

    cin.ignore();
    return 0;
     }

 char getMenuSelection (char m) // Get the menu function
 {
     cout << "|||_______Welcome To Elliotts Encryption_______|||\n";
     cout << "|||__________and Deciphering Program___________|||\n";
     cout << "|||____________________________________________|||\n";
     cout << "|||______Please select 'E' to encrypt a________|||\n";
     cout << "|||____message, 'D' to decipher a message______|||\n";
     cout << "|||______________Or 'Q' to Quit________________|||\n";
     cin >> m;

 switch (m)
     {
        case 'e':
        case 'E':   cout << "Encryption." << endl;
                  break;
        case 'd':
        case 'D':   cout <<  "Decryption." << endl;
                    break;
            case 'q':
        case 'Q':   cout <<  "Quitting" << endl;
                break;
        default: cout << "Error, Please try another message. ";
                 return getMenuSelection(m);
    }
    return (m);
 }
int random (int r)
 {
    srand(time(0));
    r=rand();
    return r;
  }
void encrypt (char key[], int r) //encryption function.
{
    for(int i = 0; i <= 40; i++) key[i] += r;
}
 void decrypt (char code[], int r) //decryption function.
 {
     for(int i = 0; i <= 40; i++) code[i] -= r;
 }

由于&#39;此代码目前无法执行未定义参考&#39;加密(char *)&#39;

问题

我问如何生成随机数并将其合并到我的加密和解密函数中。

  1. 使用静态变量还是全局变量会使其按原样工作吗?
  2. 我是否需要为这两个函数创建单独的类文件?
  3. 还有其他可能吗?
  4. 我知道我不会完整地给出正确的代码,但如果有人能以正确的方式指出我或者告诉我哪里出错了,那将对我有所帮助。

    感谢。

1 个答案:

答案 0 :(得分:1)

  
      
  1. 使用静态变量还是全局变量会使其按原样工作吗?
  2.   

不准确。这里的问题只是你要声明和调用的函数签名:

void encrypt (char key[]);

与您定义的函数的签名不匹配:

void encrypt (char key[], int r);

这使它成为一个单独的函数(由于函数重载)。

使r成为全局变量可能是解决方案的部分,但您仍需要使此函数的签名保持一致。

就你如何产生这个价值而言,这取决于你。据推测,它可能需要由用户设置或者成为消息的一部分;除非您随机生成相同的数字,否则使用随机数将无法解密消息。

  
      
  1. 我是否需要为这两个函数创建单独的类文件?
  2.   

没有。你不需要任何课程(或多个文件)。