c++ - Int Array[ ] 打印不好。

标签 c++ c arrays printf

这是我的代码:

#include <stdio.h>
void main()
   {
   int indeks, a[11], j, rezultat[50];
   int n = 0;

   printf("Unesite elemenate niza\n");

   while (n < 10)
   {

     for(indeks = 0; indeks < 10; indeks++);
     scanf("%d", &a[indeks]);
     n++;
   } 
    for (n = 0; n < 10; n++) {
    printf("%d\n", a[n]);
   }

}

您好,我有一个问题,这不会将数组打印为我在 中输入的整数。

它总是打印十次-858993460。

这就是它在 cmd 中的样子。 (抱歉英语不好)

 Unesite elemenate niza: 
 1     /input starts here
 3
 5
 1
 0
 2
 3
 5
 7
 4     /ends here
-858993460  
-858993460
-858993460
-858993460
-858993460
-858993460 
-858993460
-858993460
-858993460
-858993460      /output result
Press any key to continue . . .

最佳答案

for 循环不执行任何操作,因为它以 ; 结尾,并且当 while 循环迭代时,indeks始终为10。我建议如下

#include <stdio.h>
int main()                                  // correct function type
    {
    int indeks, a[11], j, rezultat[50];
    int n = 0;

    printf("Unesite elemenate niza\n");

    //while (n < 10)                        // delete while loop
    //{

    for(indeks = 0; indeks < 10; indeks++)  // remove trailing ;
        scanf("%d", &a[indeks]);

    //n++;                                  // delete unnecessary line
    //} 

    for (n = 0; n < 10; n++) {
        printf("%d\n", a[n]);
    }
   return 0;                                // add return value
}

关于c++ - Int Array[ ] 打印不好。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29395935/

相关文章:

c - Keccak 中的舍入常量

php - RESTful API - 数组到集合 Laravel 5

具有指定模板参数的 C++11 make_pair 无法编译

c++ - 作为宏参数给出的先前定义的常量被视为字符串文字

c++ - 如何为圆环创建索引

将文件扩展名连接到 C 中的基本名称

c - 为什么将我的输入分配给枚举会导致段错误?

c++ - 与计算着色器和 imageStore 的内存一致性

java - 获取 JSON 数组 JAVA 中的第一个实例

arrays - Swift 3.0 : compiler error when calling global func min<T>(T, T) 数组或字典扩展