简体   繁体   English

使用For循环编程C打印一维数组

[英]Printing One Dimensional Array with For Loop Programming C

I am currently following the K&R book on c programming and I have encountered a problem using the printf() function with an array. 我目前正在阅读有关C编程的K&R书,并且在将printf()函数与数组一起使用时遇到了问题。 Everything the array is supposed to hold is correct as I have tested printing each element within the initial while loop, but when I try to print outside of the while loop and within the main() function I don't get the desired answer. 我测试了在初始while循环内打印每个元素时,该数组应该保存的所有内容都是正确的,但是当我尝试在while循环外以及main()函数内进行打印时,没有得到所需的答案。 Here is the code. 这是代码。

for(i=0;i<10;++i){
    ndigits[i]=0;
}
while((c=getchar())!=EOF){

    if(c>='0'&&c<='9'){
        ++ndigits[c-'0'];
    }
    else if(c==' '||c=='\t'||c=='\n'){
        ++nblank;
    }
    else{
        ++nother;
    }
}

for(i=0;i<10;++i){
    printf("%d ", ndigits[i]);
}
printf(" blank space: %d other: %d\n", nblank, nother); 

This program is supposed to count white space, other characters and store the number of occurrences of each digit in the array. 该程序应该计算空格和其他字符,并在数组中存储每个数字的出现次数。 Counting white space and other characters works fine when I have commented out the for loop for printing the array. 当我注释掉用于打印数组的for循环时,对空格和其他字符进行计数会很好。 However, when i run the program without commenting out printing the array it prints only the first element in the array, which is the number of zeros, with ^C next to it and the doesn't even reach the last line where the number white space and other characters are printed. 但是,当我运行程序时未注释掉打印数组时,它仅打印数组中的第一个元素,即零的数量,其旁边是^ C,甚至没有到达最后一行,而白色空格和其他字符被打印。

It is user input through keyboard btw and I am using notepad++ and MinGW as my compiler. 它是通过键盘顺时针进行的用户输入,我使用的是notepad ++和MinGW作为我的编译器。

ex. 恩。

Input: 1353466113 udahkdakdjf
Output: 0 ^C

Input: 000000
Output: 6 ^C

I have tested my program with placing a printf() function within the while loop to check whether the array held the right values and it does. 我已经通过在while循环中放置一个printf()函数来检查程序,以检查该数组是否具有正确的值,并且是否正确。 All help is much appreciated. 非常感谢所有帮助。

  1. Using Ctrl C to signal end of input does more than that, it signaleds program termination. 使用Ctrl C发出信号结束输入的作用还不止于此,它表示程序终止。 Depending on your console, Ctrl D or Ctrl Z may do the trick. 根据您的控制台, Ctrl DCtrl Z可能会解决问题。

  2. Insure ch is an int and not a char . 确保ch是一个int而不是一个char

  3. Your test for white-space is good, but incomplete. 您对空白的测试很好,但是不完整。 Suggest 建议

     #include <ctype.h> ... // else if(c==' '||c=='\\t'||c=='\\n'){ else if (isspace(c)) { 

@user3191564 : I just tested out your code on my laptop and it gives me the same output as you put it. @ user3191564:我刚刚在笔记本电脑上测试了您的代码,它给我的输出与您输入的相同。 I think the EOF flag is what's giving you the weird output. 我认为EOF标志是给您奇怪输出的原因。

Everything else in the program looks to be right logically. 程序中的其他所有内容在逻辑上看起来都是正确的。 Take a critical loop at that while loop, the getchar, and EOF. 在while循环,getchar和EOF中进行关键循环。

And I don't know if you are using the getchar() quite rightly. 而且我不知道您是否正确使用了getchar()。 For more info . 有关更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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