c++ - 为什么循环停止?

标签 c++

我有一个带有 bool 函数 issquare 的程序,对于我的问题,您不需要该代码。我有迭代变量 T,这是对我将 squarsized 的时间字符串放置多少次的响应。但是在第一次输入字符串后,程序在 T=1 处停止,并且不进行下一次迭代。为什么?

int main(){
    int T;
    std::string line;
    std::cin>>T;
    int squaresize;
    int** arr=new int*[30];
    for(int d=0; d<30; d++){
        arr[d]=new int[30];
    }
    for(int i=0; i<T; i++){
        for (int d=0; d<30; d++){
            for(int d1=0; d<30; d++){
                arr[d][d1]=0;
            }
        }
        std::cin>>squaresize;
        for(int j=0; i<squaresize; i++){
            std::cin>>line;
            for(int a=0; a<squaresize; a++){
                if (line[a]=='#'){
                    arr[j][a]=1;
                }
            }
        }   
        if (issquare(arr, squaresize)==true){
            std::cout<<"Case #"<<i+1<<": YES";
        }
        else{
            std::cout<<"Case #"<<i+1<<": NO";
        }
        std::cout<<T;
    }
    return 0;
}

最佳答案

代替

std::cin>>line;

尝试

getline(std::cin, line);

operator>> 不读取整行,直到第一个空格。

关于c++ - 为什么循环停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20167293/

相关文章:

c++ - 无法访问 C++ 中全局变量的构造函数中的静态(非原始)成员

c++ - 按值传递与 const & 和 && 重载

c++ - 如何包含 draco_dec 库?

c++ - 控制台输出线的宽度限制

c++ - 在 Qt 中注册元类型的模式

c++ - 使用套接字从嵌入式设备播放 RTP 流

c++ - native 代码 : cannot use typeid with -fno-rtti

c++ - 使用 ODBC、C++ 时绑定(bind)表值参数时出错

尝试访问对象的方法时出现 C++ Qt 段错误

c++ - 在结构中初始化一对结构