【问题标题】:How to read more than one line of input?如何读取多行输入?
【发布时间】:2020-11-20 14:33:44
【问题描述】:

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

ps:我用CodeBlocks

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

这行得通

Hello World

但是我不能输入这个

Hello World
Have a nice day

因为程序退出命令提示符时没有任何错误或消息。

如何阅读多行内容?

【问题讨论】:

  • 行为是随机的吗?如果某些输入始终发生这种情况,请分享该输入和结果输出。
  • 你说你输入了10-15行,但是你的代码只能读到一行。
  • 在输入第一行之后(即在您第一次输入之后)您的代码应该继续,然后在您输入第二行之前完成。不是这样吗?
  • 我们所说的“单行”可以跨越显示的多“行”。 “单行”是文本,直到遇到换行符 '\n'
  • 我冒昧地重新表述了你的问题。请查看它以检查它是否正确描述了您的问题。如果没有,我们可以回滚

标签: c++ arrays for-loop encryption


【解决方案1】:

你可以这样做

#include <iostream>

using namespace std;

int main() {

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

  return 0;
}

【讨论】:

    【解决方案2】:

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

    #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

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

    希望有帮助吗?

    【讨论】:

    • 嘿@CKE 我是初学者(大约 15 天),我想知道这段代码是非常糟糕还是一般,如果你能回复我会很高兴。谢谢
    • @ShivamJha:老实说,我无法判断这一点。我认为您的算法正在运行,但我建议您编写一个解密器以检查您的加密有多好。也许您也可以请朋友解密您的输出;)。
    • @ShivamJha:请随时将此答案标记为已接受(答案左侧的复选标记)。提前致谢。
    猜你喜欢
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 2020-06-13
    • 2015-02-14
    • 2018-10-19
    相关资源
    最近更新 更多