首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何读取多行输入?

如何读取多行输入?
EN

Stack Overflow用户
提问于 2020-11-20 22:33:44
回答 2查看 89关注 0票数 3

因此,我构建了一个小型的基本数据加密器(仅用于学习目的)。它工作得非常好,但它只读取一行输入。是编辑器的问题还是我的代码有问题。

ps:我使用CodeBlocks

代码语言:javascript
复制
#include <iostream>
#include <ctype.h>

using namespace std;

int main()
{
    std::string str;
    char enc;
    int word;
    cout << "\t\t\t\t\t\t\t\tENCRYPTOR" <<endl;
    cout << "\t\t\t\t\t\t\t\t---------" <<endl;
    cout << "Enter a Word: ";
    getline(cin, str);
    int n = 0;
    cout << "\n\n\t\t\t\t\t\t\t\tENCRYPTED D@T@" <<endl;
    cout << "\t\t\t\t\t\t\t\t--------------\n\n" << endl;
    for(int i = 0; i < str.length(); i++){
        int randomAdd[5] = {5,6,2,3,2};
        int size = sizeof(randomAdd)/sizeof(randomAdd[0]);
//        for(int j = 0; j < 5; j++){
        word = str.at(i);
        if(i%5 == 0){
            n = 0;
        }
        enc = int(word) + randomAdd[n];
        std::cout << char(enc);
        n++;
    }

 return 0;
}

这是可行的

代码语言:javascript
复制
Hello World

但是我不能输入这个

代码语言:javascript
复制
Hello World
Have a nice day

因为之后程序会退出命令提示符,而不会出现任何错误或消息。

我怎么能读多行呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-11-26 23:16:16

您可以这样做

代码语言:javascript
复制
#include <iostream>

using namespace std;

int main() {

  string str;
  
  while (getline(cin, str)) {
    cout << str << endl;
  }

  return 0;
}
票数 0
EN

Stack Overflow用户

发布于 2020-11-25 15:34:10

此代码示例允许您从命令行/shell以交互方式输入多行

代码语言:javascript
复制
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    string str;
    char enc;
    int word;
    vector<string> myInput;
    cout << "\t\t\t\t\t\t\t\tENCRYPTOR" <<endl;
    cout << "\t\t\t\t\t\t\t\t---------" <<endl;
    while (str != "Enigma")
    {
        cout << "Enter a line (Write Enigma to exit input): ";
        getline(cin, str);
        myInput.push_back(str);
    }
    int n = 0;
    cout << "\n\n\t\t\t\t\t\t\t\tENCRYPTED D@T@" <<endl;
    cout << "\t\t\t\t\t\t\t\t--------------\n\n" << endl;
    for(auto & myInputLine : myInput)
    {
        str = myInputLine;
        for (size_t i = 0; i < str.length(); i++) {
            int randomAdd[5] = { 5,6,2,3,2 };
            int size = sizeof(randomAdd) / sizeof(randomAdd[0]);
            word = str.at(i);
            if (i % 5 == 0) {
                n = 0;
            }
            enc = int(word) + randomAdd[n];
            std::cout << char(enc);
            n++;
        }
    }
    return 0;
}

如果写入了Enigma,则输入结束。

所有输入都存储在STL的vector容器中,请参阅vector

之后,所有的行都会被你的算法加密。

希望它能帮上忙?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64931337

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档