我正在尝试编写一个加密程序,它接受一个字符串并将每个变量转换为ascii值,然后将文件保存到文本文件

时间:2015-07-29 01:07:28

标签: c++ string encryption ascii offset

#include <iostream>
#include <fstream>
#include <string>
#include <string.h>

using namespace std;

int get_ascii_int(char ch);

int main()
{
    string test;
    char ch = 0;
    ofstream method1;
    int i = 0;
    int decision;
    int value;

    method1.open("method1.txt");

    cout << "Input a word or name: ";
    cin >> test;

    cout << test << endl;

    for (; i < test.size(); i++);
    {
        test[i];
        value = get_ascii_int(test[i]);
    }

    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 " << value << endl;
    }
    else
    {
        if (method1.is_open())
        {
            method1 << "The encrpyted code is " << value << endl;
            method1.close();
        }
        else
            cout << "Unable to open file." << endl;
    }

    return 0;

}
int get_ascii_int(char ch)
{
    return ((int)ch);
}

当我运行程序时,它说加密为零。如何修复for循环以运行每个字符并获取ascii值?

#include <iostream>
#include <string.h>
#include <string>
#include <fstream>
using namespace std;


int get_offset_ascii(char ch, int offset);

int main()
{
    string test;
    int i = 0;
    int offset = 0;
    ofstream method2;
    int decision;
    int value = 1;

    method2.open("method2.txt");

    cout << "Input a word or name: ";
    getline(cin, test);
    test;

    for (; i < test.length(); i++);
    {
        char x = test.at(i);
        cout << int(x) << endl;
    }
    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 " << value << endl;
    }
    else
    {
        if (method2.is_open())
        {
            method2 << "The encrypted code is " << value << endl;
            method2.close();
        }
        else
            cout << "Unable to open file." << endl;
    }

    return 0;
}

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);
}

这是一个类似的设置,但它应该打印出字符串中每个字符的ascii值偏移量。我让他们分开是因为我正在尽我所能来解决它​​但不能。我只是不断得到0 ??

的答案

请帮助我真的很沮丧!

3 个答案:

答案 0 :(得分:1)

你的两个程序都有一个非常微妙的错误:

for (; i < test.size(); i++);

最后看到分号?

让我们重新格式化您的代码,以便您可以准确了解自己的目标。

for (; i < test.size(); i++)
    ;

{
    test[i];
    value = get_ascii_int(test[i]);
}

你认为你的for循环,真的是一个空循环。你认为是一个循环的主体,只是一个内部块,正在执行i设置为字符串的长度。严格来说,它是未定义的行为,但它可能正在做的是读取您输入的任何文本字符串的空终止字节。

答案 1 :(得分:0)

我打算回答这个问题,但Sam Varsnavik比我快......: - )

他指出的问题是你收到0的原因,因为他说你认为循环体实际上只运行一次,因为它在循环体外面。

无论如何,即使有了这个修复程序,你的程序也无法达到你想要的目标,即将每个字符转换为一个值&#34;因为你只存储一个值。

您应该使用值向量来分别存储每个值。这是一个可以完成工作的示例代码:

#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
#include <vector>

using namespace std;

int get_ascii_int(char ch);

int main()
{
    string test;
    char ch = 0;
    ofstream method1;
    int i = 0;
    int decision;
    vector<int> values;

    method1.open("method1.txt");

    cout << "Input a word or name: ";
    cin >> test;

    cout << test << endl;

    for (; i < test.size(); i++)
    {
        values.push_back(get_ascii_int(test[i]));
    }

    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 ";
        for (i=0; i<values.size(); ++i)
            cout << values[i] << " ";
        cout << endl;
    }
    else
    {
        if (method1.is_open())
        {
            method1 << "The encrpyted code is ";
            for (i=0; i<values.size(); ++i)
                cout << values[i] << " ";
            cout << endl;
            method1.close();
        }
        else
            cout << "Unable to open file." << endl;
    }

    return 0;

}
int get_ascii_int(char ch)
{
    return ((int)ch);
}

答案 2 :(得分:-2)

    value = 0 ;


  for (; i < test.size(); i++)
        {
            test[i];
            value = (value*i) + get_ascii_int(test[i]);
        }