凯撒密码 - 怎么了?

Caesar cipher - What's wrong?

本文关键字:怎么了 密码 凯撒      更新时间:2023-10-16

我在启动加密程序时遇到问题。对于单个单词字符串,它运行良好(例如,abcd),但是当我键入两个或三个或更多单词(一个句子,例如abcd ab ac)时,它不会要求键,而是重写我键入的句子。我做错了什么?提前谢谢。代码:

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <conio.h>
#include <string>
#include <stdlib.h>
#include <stdio.h>
int main()
{
        int key, l;
        char choose;
        string message;
        cout<<"Type the message"<<endl;
        cin>>message;
        cout<<"Give me a key from 0 to 26"<<endl;
        cin>>key;
        for (int i=0,l=message.size(); i<=l; i++)
        {
            if (isalpha(message[i]))
            {
                if (isupper(message[i]))
                {
                    cout<<(char)('A'+(message[i]-'A'+key)%26);
                }
                if (islower(message[i]))
                {
                    cout<<(char)('a'+(message[i]-'a'+key)%26);
                }
            }
            else
            {
                cout<<message[i];
            }
        }
        return 0;
}

使用

std::getline(std::cin, message);

CIN 只读取到下一个空格,其余的输入保存在流缓冲区中,以便您在第一步中输入的句子的下一个单词作为第二个 CIN 的输入。