scanf和printf用于循环不同的输出量

时间:2014-10-15 01:05:17

标签: c debugging for-loop printf scanf

我得到的错误是扫描将比打印循环多一个数字。 循环后我有示例代码。谁会知道为什么会这样?

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

        //Throw error is *Argv[] is not an integer
        //assuming it was an integer
        int cnt = (int)(argv[1][0] - '0');
        printf( "number is %d\n", cnt );
        //
        printf("Enter %i integer values to place in tree:\n", cnt);
        for ( i1 = 0; i1 < cnt; ++i1) {
                scanf( "%d\n", &elemArray[i1] );
        }

        //first ouput "INput Values"

         for ( i2 = 0; i2 < cnt; ++i2) {
               printf( "%d\n", elemArray[i2] );
        }

        //Build tree with each element
        return 0;
}

输出是:

Enter 5 integer values to place in tree:
1
2
3
4
5
6
1
2
3
4
5

1 个答案:

答案 0 :(得分:0)

我明白了。它是 scanf( "%d\n", &elemArray[i1] );要更改为 scanf( "%d", &elemArray[i1] );