-3

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?

Hardy
  • 1
  • 1
  • 4
    Just an unrelated note: You might use `g++` to compile a file ending with `.cpp`, but there's nothing C++ about the source you show. It might as well be a straight C program. If you want to program in C++, then *program in C++*. You might want to consider [finding a good beginners book about C++ to read](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Some programmer dude Jan 20 '17 at 07:39
  • 2
    As for your problem, first try some [rubber duck debugging](https://en.wikipedia.org/wiki/Rubber_duck_debugging). And if that fails use an actual debugger to step through the file, line by line, while monitoring variables and their values. That last part might be helped if you put statements on different lines, instead of cramming as much as possible into as few lines as possible. – Some programmer dude Jan 20 '17 at 07:40
  • @Someprogrammerdude He's definitely only using C library functions, but I don't think that code is valid for C89 at least since there are variable declarations not at the start of the scope of `main()` – Govind Parmar Jan 20 '17 at 14:20

1 Answers1

2

With while(1), you made an infinite loop.

Instead of else printf("Corrupt\n"); try

else {
 printf("Corrupt\n");
 break ;
}

NB: To clarify, you keep looping and call scanf without any input resulting in i == 0 and numr == 0 and numc == 0

You probably also want to write an exit statement for when you succeed, but this is not the question you asked.

Antzi
  • 12,831
  • 7
  • 48
  • 74
  • The program needs 99*99 input numbers, but I just input 99 numbers. Why the problems doesn't wail or stop but still continue. – Hardy Jan 20 '17 at 07:48
  • You made a loop using `while` but did not wrote any condition to exit the loop. See https://www.tutorialspoint.com/cplusplus/cpp_while_loop.htm – Antzi Jan 20 '17 at 07:51
  • What if I change the program as : while (~scanf("%d", &n) && n){} And delete the scanf("%d", &n); below – Hardy Jan 20 '17 at 07:56
  • 1
  • ` while (~scanf("%d", &n))` sounds better. You don't really need it in the condition do you ? – Antzi Jan 20 '17 at 08:03
  • @MartinBonner I think the point is to catch the `EOF` return from `scanf`. But then an explicit check for that would be better. Or it's like you think and the OP mistakes the two "not" (logical `!` and bitwise `~`) operators. – Some programmer dude Jan 20 '17 at 14:26
  • @Someprogrammerdude: I hadn't confused ! and ~. I had a) had a brain fart and thought scanf returned 0 for success and non-0 for failure; b) thought the OP had confused !/~. – Martin Bonner supports Monica Jan 20 '17 at 14:55