简体   繁体   中英

Why is cycle stopping?

I have a program with bool function issquare, which code don't need for you for my question. I have iterative variable T, which is response for how many times I will put squarsized's time string. But after first input of strings programs stop, at T=1, and don't go to next iteration. Why?

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

Instead of

std::cin>>line;

Try

getline(std::cin, line);

operator>> doesn't read an entire line, only until the first whitespace.

Instead of j you are comparing and incrementing i, which is also used by the outer loop:

for(int j=0; i<squaresize; i++){
    std::cin>>line;
    for(int a=0; a<squaresize; a++){
        if (line[a]=='#'){
            arr[j][a]=1;
        }
    }
}   

In the future (and with possibly more complex programs), learning how to use of a debugger can really help you to locate such bugs. Your problem was that the outmost loop executed less than T times:

for(int i=0; i<T; i++){

Since the value of T is constant, something must be modifying i inside the loop. An easy way to debug this would be using a debugger to find out where the variable is changed. In Visual Studio this can be done by breaking and adding a data breakpoint from Debug -> New Breakpoint -> New Data Breakpoint -> Address: &i

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM