不知道为什么我的函数在switch中调用不了

weixin_38910147 2017-07-07 07:30:22

#include<iostream>
#include<fstream>

using namespace std;

class Encryption_and_Dencryption
{
private:
char *article;
char *document;
int num;
int fibonacci[2][10000],
words[3][10000],
encryption_1[50000],
encryption_2[2][50000];
protected:
void input_words();
void input_encryption_document();
void encryption();
void input_code();
void input_dencryption_document();
void dencryption();
public:
Encryption_and_Dencryption();
~Encryption_and_Dencryption();
void encryption_words();
void encryption_document();
void dencryption_code();
void dencryption_document();
};

void Encryption_and_Dencryption::input_words()
{
char *temp = new char[10000];
cout << "Please input the words!" << endl;
cin.getline(temp, 10000);
delete article;
article = new char[strlen(temp) + 1];
strcpy_s(article, strlen(temp) + 1, temp);
delete temp;
}

void Encryption_and_Dencryption::input_encryption_document()
{
char *ref_document = new char[20];
cout << "Please input the name of the document!" << endl;
cin.getline(ref_document, 20);
delete document;
document = new char[strlen(ref_document) + 1];
strcpy_s(document, strlen(ref_document) + 1, ref_document);
char *temp = new char[10000];
int n = 0;
ifstream ifs;
ifs.open(document, ios::in);
if (ifs)
{
ifs.seekg(ios::beg);
char ch;
while (ifs.peek() != EOF)
{
ifs.get(ch);
temp[n++] = ch;
}
temp[n] = '\0';
delete article;
article = new char[strlen(temp) + 1];
strcpy_s(article, strlen(temp) + 1, temp);
delete temp;
}
ifs.close();
}

void Encryption_and_Dencryption::encryption()
{
int len = strlen(article);
int p = 0;
for (int n = 0; n < len; n++)
{
for (int add5 = 0; add5 < 5; add5++)
{
++article[n];
}
int ref = article[n];
for (int x = 2; x >= 0; x--)
{
words[x][n] = ref % 10;
ref = ref / 10;
}
for (int x = 0; x < 3; x++)
{
encryption_1[p++] = words[x][n];
}
for (int x = 0; x < 2; x++)
{
encryption_1[p++] = fibonacci[x][n];
}
}
for (num = 0; num < p; num++)
{
int ref = encryption_1[num] + 48;
for (int x = 1; x >= 0; x--)
{
encryption_2[x][num] = ref % 10;
ref = ref / 10;
}
}
}

void Encryption_and_Dencryption::input_code()
{
char *temp = new char[100000];
cout << "Please input the code!" << endl;
cin.getline(temp, 100000);
char *code = new char[strlen(temp) + 1];
strcpy_s(code, strlen(temp) + 1, temp);
int p = 0;
num = 0;
do
{
for (int x = 0; x < 2; x++)
{
encryption_2[x][num] = code[p++] - '0';
}
num++;
} while (p < strlen(code));
delete temp;
delete code;
}

void Encryption_and_Dencryption::input_dencryption_document()
{
char *ref_document = new char[20];
cout << "Please input the name of the document!" << endl;
cin.getline(ref_document, 20);
delete document;
document = new char[strlen(ref_document) + 1];
strcpy_s(document, strlen(ref_document) + 1, ref_document);
char *temp = new char[100000];
int n = 0;
ifstream ifs;
ifs.open(document, ios::in);
if (ifs)
{
ifs.seekg(ios::beg);
char ch;
while (ifs.peek() != EOF)
{
ifs.get(ch);
temp[n++] = ch;
}
temp[n] = '\0';
char *code = new char[strlen(temp) + 1];
strcpy_s(code, strlen(temp) + 1, temp);
int p = 0;
num = 0;
do
{
for (int x = 0; x < 2; x++)
{
encryption_2[x][num] = code[p++] - '0';
}
num++;
} while (p < strlen(code));
delete temp;
delete code;
}
ifs.close();
}

void Encryption_and_Dencryption::dencryption()
{
int n;
for (n = 0; n < num; n++)
{
encryption_1[n] = encryption_2[0][n] * 10 + encryption_2[1][n] - 48;
}
n = 0;
int m = 0;
do
{
for (int q = 0; q < 3; q++)
{
words[q][m] = encryption_1[n++];
}
m++;
n += 2;
} while (n < num);
char *temp = new char[10000];
for (n = 0; n < m; n++)
{
temp[n] = words[0][n] * 100 + words[1][n] * 10 + words[2][n] - 5;
}
temp[m] = '\0';
delete article;
article = new char[strlen(temp) + 1];
strcpy_s(article, strlen(temp) + 1, temp);
delete temp;
}

Encryption_and_Dencryption::Encryption_and_Dencryption()
{
int fib[10000]{ 1,1 };
for (int n = 2; n < 10000; n++)
{
fib[n] = (fib[n - 1] + fib[n - 2]) % 10;
}
for (int n = 0; n < 10000; n++)
{
int ref = fib[n] + 48;
for (int x = 1; x >= 0; x--)
{
fibonacci[x][n] = ref % 10;
ref = ref / 10;
}
}
article = NULL;
document = NULL;
}

Encryption_and_Dencryption::~Encryption_and_Dencryption()
{
if (article)
delete article;
if (document)
delete document;
}

void Encryption_and_Dencryption::encryption_words()
{
input_words();
encryption();
for (int n = 0; n < num; n++)
{
for (int x = 0; x < 2; x++)
{
cout << encryption_2[x][n];
}
}
cout << endl;
}

void Encryption_and_Dencryption::encryption_document()
{
input_encryption_document();
encryption();
ofstream ofs;
ofs.open(document, ios::out);
if (ofs)
{
for (int n = 0; n < num; n++)
{
for (int x = 0; x < 2; x++)
{
ofs << encryption_2[x][n];
}
}
}
ofs.close();
}

void Encryption_and_Dencryption::dencryption_code()
{
input_code();
dencryption();
cout << article << endl;
}

void Encryption_and_Dencryption::dencryption_document()
{
input_dencryption_document();
dencryption();
ofstream ofs;
ofs.open(document, ios::out);
if (ofs)
{
ofs << article;
}
ofs.close();
}

int main()
{
Encryption_and_Dencryption casual;
cout << "Welcome to use my product!" << endl;
system("pause");
system("cls");
int choose;
int judge = 1;
do
{
cout << "Please choose which function you want to use:" << endl
<< endl << endl
<< "1. Encrypt the words!" << endl
<< "2. Encrypt the file!" << endl
<< "3. Decrypt the words!" << endl
<< "4. Decrypt the file!" << endl
<< "5. Exit the system!" << endl;
cin >> choose;
switch (choose)
{
case 1:casual.encryption_words(); break;
case 2:casual.encryption_document();break;
case 3:casual.dencryption_code();break;
case 4:casual.dencryption_document(); break;
case 5:judge = 0;break;
default:
break;
}
system("cls");
} while (judge == 1);
return 0;
}

求帮忙看看!
...全文
357 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_38910147 2017-07-07
  • 打赏
  • 举报
回复
引用 1 楼 jianwen0529 的回复:
cin >> choose; cin.ignore(INT_MAX, '\n'); //把上一行遗留的字符清空,否则getline可能读取到\n,最后字符串为空
一不小心忽视了这一点,谢谢啦
paschen 版主 2017-07-07
  • 打赏
  • 举报
回复
可以调用啊,你是指什么? 建议单步跟踪程序运行,观察分析原因
幻夢之葉 2017-07-07
  • 打赏
  • 举报
回复
cin >> choose; cin.ignore(INT_MAX, '\n'); //把上一行遗留的字符清空,否则getline可能读取到\n,最后字符串为空

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧