2015-04-01 58 views
2

這裏是我的代碼:Int Array []打印不好。

#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 . . . 
+0

您在循環中有一個循環,而在第二個循環的主體中有分號。這意味着** all **您輸入的數據最終在陣列的位置「10」處結束。 – dasblinkenlight 2015-04-01 16:27:12

+6

在for後面加上分號(indegs = 0; indeks <10; indeks ++);'**投票以打印錯誤的方式關閉** – dasblinkenlight 2015-04-01 16:27:37

+1

[main()返回一個int](http://isocpp.org/wiki/ faq/newbie#main-returns-int) – NathanOliver 2015-04-01 16:31:09

回答

4

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 
} 
+0

哇,謝謝你。我仍然是新手:D – 2015-04-01 16:32:46

+1

雖然我們在這裏,'j'和'rezultat'未被使用,'indeks'(我假設他是指索引),'n'可以在'for'循環中聲明。 'cin'和'cout'可能會更好用,而且你也可以在第二秒附近刪除'{'和'}以便循環使用 – dwcanillas 2015-04-01 16:33:29

+0

@dwcanillas,對於那個j是哦,並且rezultat ...我用過)我的舊代碼,所以我忘了刪除它,對不起:D。 – 2015-04-01 16:48:05

1

for(indeks = 0; indeks < 10; indeks++); 什麼都不做,除了增加indeks 10倍。 我可以寫出爲您糾正的整個代碼,但您將如何學習?

+0

你必須成爲未來的「C」老師。 +1 lol – MCHAppy 2015-04-01 16:34:05

+0

geez man:D,我幾秒前得到答案,不需要編寫整個代碼。我是新來的C++和整個編程的事情。不管怎麼說,還是要謝謝你。 – 2015-04-01 16:35:43

0

您的代碼似乎有幾個語法錯誤。 天氣風向標已發佈正確的版本,請看他的回答。

#include <iostream> 
#include <stdio.h> 

void main() 
{ 
    const unsigned int A_SIZE(10); 
    int a[ A_SIZE ]; 

    printf("Unesite elemenate niza\n"); 

    for (unsigned int indeks(0); indeks < A_SIZE; ++indeks) 
     scanf("%d", &a[ indeks ]); 

    for (unsigned int indeks(0); indeks < A_SIZE; ++indeks) 
     printf("%d\n", a[ indeks ]); 

    std::cout << "Enter a character to exit: "; char c; std::cin >> c; 
} 
+0

是的,我做了,我查找天氣風向標它一切正常。謝謝你的回答。對不起,我仍然對這件事情不熟悉。 – 2015-04-01 16:42:30