【问题标题】:My Program Only Processes One Line From The Input我的程序只处理输入中的一行
【发布时间】:2013-11-14 19:52:32
【问题描述】:

我正在做一个凯撒块密码。解决方案的一般步骤是:

  • 将您的消息读入一个大缓冲区或字符串对象。

  • 是否删除空格和标点符号(这对于 如果你这样做了,你会读到的敌人)。

  • 然后统计消息中的字符数。

  • 选择第一个大于消息长度的完美正方形,
    分配一个该大小的 char 数组。

  • 将消息从左到右读入该大小的方阵中, 从上到下。

  • 将消息从上到下、从左到右书写,您就已经
    加密它。

我的代码:

#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <ctype.h>
#include <cmath>
#include <functional>
#include <numeric>
#include <algorithm>
#include <locale>

using namespace std;

int main(int argc, char *argv[])
{

    int length = 0;

    cout << "Enter a string: ";

    string buffer;
    char buff[1024];

    while (getline(cin, buffer)) 
    {
        buffer.erase(remove_if(buffer.begin(), buffer.end(), not1(ptr_fun(::isalnum))), buffer.end());
        break;
    }

    length = buffer.length();
    int squareNum = ceil(sqrt(length));

    strcpy(buff, buffer.c_str());

    char** block = new char*[squareNum];
    for(int i = 0; i < squareNum; ++i)
    block[i] = new char[squareNum];

    int count = 0 ;

    for (int i = 0 ; i < squareNum ; i++)
    {
        for (int j = 0 ; j < squareNum ; j++)
        {
            block[i][j] = buff[count++];
        }
    }

    for (int i = 0 ; i < squareNum ; i++)
    {
        for (int j = 0 ; j < squareNum ; j++)
        {
            cout.put(block[j][i]) ;
        }
    }

    return 0;

}

在大多数情况下,它是有效的。我遇到的问题是输入多于一行时。

Ex. 1 
this is sample text suitable for a simulation of a diplomatic mission or a spy's instructions

Ex. 2
this is sample text suitable
for a simulation of a diplomatic
mission or a spy's instructions

示例 1 有效,示例 2 无效,因为有多行。我感觉它与 while(getLine) 函数有关,但我不知道具体要更改什么。

【问题讨论】:

    标签: c++


    【解决方案1】:

    “while”循环中的这个“break” - 它在第一次“getline”调用后中断循环。这就是为什么你只得到一条线。

    【讨论】:

      【解决方案2】:

      也许你应该考虑删除后删除中断。

      【讨论】:

        【解决方案3】:

        你在这里做什么:

        while (getline(cin, buffer)) 
        {
            buffer.erase(remove_if(buffer.begin(), buffer.end(), not1(ptr_fun(::isalnum))), buffer.end());
            break;
        }
        

        每次使用 getline 时都会将新行保存到缓冲区。我的意思是,每次有getline() 引起你的buffer 被替换,而不是附加。

        试试这个:

        string buffer = "";
        string buff2;
        
        // You need to provide some way to let the user stop the input
        // e.g. ask him to declare number of lines, or what I recommend
        // check for an empty line given, which is implemented below:
        
        while (getline(cin, buff2)) 
            {
                // now pressing [enter] after the last input line stops the loop
                if (buff2.empty())
                    break;
        
                buff2.erase(remove_if(buffer.begin(), buffer.end(), not1(ptr_fun(::isalnum))), buffer.end());
                buffer += buff2;
            }
        

        【讨论】:

        • 我一开始并没有意识到,问题在于没有选项可以写多行。现在它应该可以工作了。
        猜你喜欢
        • 1970-01-01
        • 2017-10-05
        • 1970-01-01
        • 1970-01-01
        • 2016-05-11
        • 2012-06-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-06
        相关资源
        最近更新 更多