创建一个有三种方法可供选择的加密程序!它有问题,我不知道如何解决
Creating an encryption program with a choice of three methods! It has issues I am not sure how to fix
#include <iostream>
#include <string.h>
#include <string>
#include <fstream>
using namespace std;
int get_ascii_int(char ch);
int get_offset_ascii(char ch2, int offset);
//int print_string_ints(string test_string);
int method3_substitution_abc();
//above are the function declarations
int main()
{
string test_string;//input of string
char ch = 0;//used in method1
char ch2 = 0;//used in method2
int index1 = 0;//used in for loop method1
int index2 = 0;//used in for loop method2
int offset = 0;//input of how much to offset
int new_ascii = 0;//the new ascii with offset
int ascii_value1 = 0;//the ascii value of the char
int option;//the menu choice of encryption method
int decision;//the decision to save or display
ofstream method1;//method 1 text file
ofstream method2;//method 2 text file
ofstream method3;//method 3 text file
string test_string_copy;//copy of string method 2
//Below is a description of the methods of encryption
cout << "There are three methods of encryption, listed below, to choose from: " << endl;
cout << "1. Converting characters into the corresponding ASCII values. " << endl;
cout << "2. Shifting characters right/left using the ASCII value of the characters ";
cout << "and a set offset amount. " << endl;
cout << "3. Using a reverse alphabet, so each letter will be replaced with the letter ";
cout << "on the opposite end of the alphabet. For example, A would become Z. " << endl;
cout << "Which encryption method would you like to use, 1, 2, 3? ";
cin >> option;
switch (option)
{
case '1':
method1.open("method1.txt");
cout << "Input a word or name: ";
getline(cin, test_string);
for (; index1 < test_string.size(); index1++);
{
ascii_value1 = get_ascii_int(test_string[index1]);
}
cout << "Would you like to display the file or save it, enter 1 for display or 2 for save?";
cin >> decision;
if (decision == '1')
{
cout << "The encrypted code is " << ascii_value1 << endl;
}
else
{
if (method1.is_open())
{
method1 << "The encrpyted code is " << ascii_value1 << endl;
method1.close();
}
else
cout << "Unable to open file." << endl;
}
break;
case '2':
method2.open("method2.txt");
cout << "Input a word or name: ";
getline(cin, test_string);
test_string_copy = test_string;
for (; index2 < test_string_copy.size(); index2++);
{
new_ascii = get_offset_ascii(test_string_copy[index2], ch2);
}
cout << "Would you like to display the file or save it, enter 1 for display or 2 for save?";
cin >> decision;
if (decision == '1')
{
cout << "The encrypted code is " << new_ascii << endl;
}
else
{
if (method2.is_open())
{
method2 << "The encrypted code is " << new_ascii << endl;
method2.close();
}
else
cout << "Unable to open file." << endl;
}
break;
case '3':
method3.open("method3.txt");
method3_substitution_abc();
break;
}
return 0;
}
//listed below are the function definitions
int get_ascii_int(char ch)
{
return ((int)ch);
}
int get_offset_ascii(char ch2, int offset)
{
int new_offset_value;//the value after adding the determined offset to the ascii value of the letter
new_offset_value = (int)ch2 + offset;
(char)new_offset_value;
return (new_offset_value);
}
//int print_string_ints(string test_string)
//{
//for (int i = 0; i < test_string.size(); i++)
//{
//(int)test_string[i++];
//}
//return 0;
//}
int method3_substitution_abc()
{
char test_string[100];
cout << "Enter a name or phrase: ";
cin >> test_string;
if (isupper((int)test_string))
{
int stalpha = 65;//start of alphabet
int endalpha = 90;//end of alphabet
char b[100];//array to reverse the alphabet
for (int i = 0; test_string[i] != ' '; i++)
{
b[i] = endalpha - (test_string[i] - 65);
}
}
else if (islower((int)test_string))
int stalpha = 97;//start of alphabet
int endalpha = 122;//end of alphabet
char b[100];//array to reverse the alphabet
for (int i = 0; test_string[i] != ' '; i++)
{
b[i] = endalpha - (test_string[i] - 97);
}
return 0;
}
我正在尝试编写这个加密程序。我真的很困惑为什么它不能运行。
例如switch语句没有正确运行,它将转到正确的大小写,然后跳过字符串的输入?
这是我第一次使用c++,所以我很难调试。
在用户选择保存或显示后,我有将文件保存到文本文件的问题?它必须在switch语句的每个case之后执行。
我也知道我使用的for循环是不正确的方法1和2?谁能检查一下,告诉我有什么问题吗?我很确定这与for循环的参数有关。
我不知道我应该用字符串还是数组?(在用户输入字符串
至少您发现的第一个问题(使用switch
语句)非常简单明了。
您已经将option
定义为int
,因此当您读取它时,它被读取为整数。这意味着如果用户输入1
,它将具有与'1'
不同的值1
。'1'
是一个字符,(例如)将打印为1
,但在大多数字符集中,它的值实际上是49。
您有两个明显的选择:将option
更改为char
,因此它将被读取为字符而不是整数,或者将switch语句中的值从'1'
, '2'
等更改为1
, 2
等。
猜测一下,当您将字符串提取器(例如,cin >> my_string;
)与std::getline
混合使用时,您从getline中看到的问题是相当常见的。字符串提取器从流中提取字符串,但将new-line
字符留在流缓冲区中。然后,当您调用std::getline
时,它将新行读取为空字符串,因此它不会等待您输入更多的输入。
如果您真的必须以这种方式混合使用这两种方法,您可能需要添加对std::cin.ignore
的调用,以读取并忽略任何不超过并包括换行字符的数据。然后当你调用std::getline
时,它会读取一些数据
相关文章:
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 我正在尝试使用 c++ 创建一个货币转换程序,我不知道如何继续
- 我不知道这条线是做什么的
- 如果我不知道每个列表中有多少个数字,我如何将给定数量的数字列表作为输入?
- 我不知道导致错误的原因 (C3074)
- 我不知道为什么这段代码会让核心被转储?
- 我正在尝试制作一个自平衡机器人,但编译时存在错误。我不知道如何解决它
- 我不知道如何使用代码块win32 gui
- 没有函数模板的实例与我不知道为什么的参数列表匹配
- 由于某种原因,我的代码中出现了 [json.exception.type_error.302]。我知道错误意味着什么,但我不知道哪里有故障
- 为什么我的结果中有 0?我不知道它从哪里来
- 我不知道为什么会发生堆损坏(关于内存分配问题)
- 我不知道这个 MCMF 代码有什么问题
- 是否存在链接类问题?我不知道发生了什么事
- LNK2005,我不知道我能做些什么来解决这个问题
- 删除数组时出现问题(我不知道这个错误)
- 我的返回语句有一个错误,我不知道要修复
- 这个C代码有什么问题?我想知道输入字符串中1的个数
- 创建一个有三种方法可供选择的加密程序!它有问题,我不知道如何解决