简体   繁体   English

如何将邮件转换为密文?

[英]how to convert message to cipher message?

#include <iostream>
using namespace std;

int main()
{
    string message;
    cout << "Ahlan ya user ya habibi." <<endl;

    cout <<"what do you like to do today?" <<endl;
    cout <<"please enter the message:" <<endl;
    getline(cin,message);

    for(int i=0;i<message.size();i++)
    {
        if(string(message[i]==32))
        {
            cout<<char(message[i]);
        }
        else if(string( message[i])>=110)
        {
            int x = int(message[i])-13;
            cout<<char(x);
        }
        else
        {
            int x = string (message[i])+13;
            cout<<char(x);
        }
    }
    return 0;
}

E:\\my programe\\quiz\\main.cpp|20|error: no matching function for call to 'std::__cxx11::basic_string<char>::basic_string(char&)'| E:\\ my programe \\ quiz \\ main.cpp | 20 |错误:没有匹配的函数调用'std :: __ cxx11 :: basic_string <char> :: basic_string(char&)'|

E:\\my programe\\quiz\\main.cpp|20|error: invalid conversion from 'char' to 'const char*' [-fpermissive]| E:\\ my programe \\ quiz \\ main.cpp | 20 |错误:从'char'到'const char *'的无效转换[-fpermissive] |

E:\\my programe\\quiz\\main.cpp|27|error: no matching function for call to 'std::__cxx11::basic_string<char>::basic_string(char&)'| E:\\ my programe \\ quiz \\ main.cpp | 27 |错误:没有匹配的函数调用'std :: __ cxx11 :: basic_string <char> :: basic_string(char&)'|

E:\\my programe\\quiz\\main.cpp|27|error: invalid conversion from 'char' to 'const char*' [-fpermissive]| E:\\ my programe \\ quiz \\ main.cpp | 27 |错误:从'char'到'const char *'的无效转换[-fpermissive] |

std::string::operator[] returns a char& reference. std::string::operator[]返回char&引用。 You are trying to construct temporary std::string objects with single char values as input, but std::string does not have any constructors that take only a single char as input. 您正在尝试构造具有单个char值作为输入的临时std::string对象,但是std::string没有任何仅将单个char作为输入的构造函数。 That is why you are getting errors. 这就是为什么您会出错。

Even if you could construct a std::string from a single char , you can't compare a std::string to an integer anyway. 即使可以从单个char构造std::string ,也无论如何都不能将std::string与整数进行比较。

You don't need all of those string() (and char() ) casts at all (BTW, your first string() cast is malformed anyway). 您根本不需要所有这些string() (和char() )强制转换(顺便说一句,您的第一个string()强制转换始终是格式错误的)。 char is a numeric type. char是数字类型。 You can compare a char value directly to an integer, and add/subtract and integer directly to/from a char value to produce a new char value. 您可以比较一个char直接值为整数,并添加/直接从减和整数/ char值,以产生新的char值。

Try this instead: 尝试以下方法:

#include <iostream>
using namespace std;

int main()
{
    string message;
    cout << "Ahlan ya user ya habibi." << endl;

    cout << "what do you like to do today?" << endl;
    cout << "please enter the message:" << endl;
    getline(cin, message);

    for(int i = 0; i < message.size(); i++)
    {
        if (message[i] == 32)
        {
            cout << message[i];
        }
        else if (message[i] >= 110)
        {
            char x = message[i] - 13;
            cout << x;
        }
        else
        {
            char x = message[i] + 13;
            cout << x;
        }
    }
    return 0;
}

Live Demo 现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM