I use a file as the input for a c++ programs, and the input is not complete. When I output the results to another file, it meets some problems. Because of the uncompleted input, I think the programs will shutdown after the program, but it doesn't. It continue running and drop into a loop that never ends.
Here's the src of the program:
#include <stdio.h>
int main()
{
while(1) {
int n,i,j;
int a[100][100]={0};
int row[100]={0};
int col[100]={0};
scanf("%d",&n);
if (n==0) break;
int numr=0,numc=0,cr=0,cc=0;
for (i=0;i<n;i++) {
for (j=0;j<n;j++) {
scanf("%d",&a[i][j]);
row[i]=row[i]+a[i][j];
}
if (row[i]%2!=0) {numr++; cr=i;}
}
for (j=0;j<n;j++) {
for (i=0;i<n;i++) col[j]=col[j]+a[i][j];
if (col[j]%2!=0) {numc++; cc=j;}
}
if ((numr==0)&&(numc==0)) printf("OK\n");
else if ((numr==1)&&(numc==1)) printf("Change bit (%d,%d)\n",cr+1,cc+1);
else printf("Corrupt\n");
}
}
And I use the g++ commond to compile the file
g++ testid.cpp -o testid
The input file is:
99
0 0 1 1 1 1 0 1 0 0 1 1 0 0 1 1 1 1 1 0 0 0 1 1 1 0 1 1 0 0 0 1 0 0 1 1 0 1 1 0 0 0 1 1 1 0 1 1 0 0 1 0 0 1 0 0 0 1 1 1 1 1 1 0 0 1 0 1 1 0 0 0 0 0 0 0 0 1 1 0 1 0 1 0 0 1 1 1 0 1 0 1 1 0 1 1 0 1 0
And I run the programs:
./testid < sample.in > out
It drops into a never ending loops. The output file look likes:
Corrupt
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
But I wants the result be null for I have not complete my input. What is problem? How does it work?