创建一个有三种方法可供选择的加密程序!它有问题,我不知道如何解决

Creating an encryption program with a choice of three methods! It has issues I am not sure how to fix

本文关键字:有问题 我不知道 何解决 解决 程序 有三种 一个 方法 创建 选择 加密      更新时间:2023-10-16
#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时,它会读取一些数据

相关文章: