2012-02-21 45 views
-5
#include <stdio.h> 
int main(void) 
{ 
    int n,c,value,sum=0; 
    printf ("Enter the no of integers u want to add:"); 
    scanf ("%d",&n); 
    printf ("\nEnter %d integers:",n); 
    for (c=1;c<=n;c++) 
    { 
     scanf ("%d",&value); 
     sum=sum+value; 
    } 
    printf ("\nSum of the integers:%d",sum); 
    getch(); 
} 

該程序給出不同的輸出作爲總和。我無法找出謬誤。幫助將非常感激。程序添加n個不在C中工作的整數

+10

給出的一個例子你得到的結果,並解釋你爲什麼認爲這是錯誤的。 – 2012-02-21 15:47:17

+4

舉一個你正在使用的*輸入*的例子。 – ydroneaud 2012-02-21 15:52:51

+0

另外,你爲什麼最後調用'getch()'而不是將它分配給任何東西? – 2012-02-21 15:55:45

回答

1

您的設置還有其他問題,這裏沒有顯示。

下面的代碼工作正常(一旦你擺脫非標準getch()的憎惡和報酬main值):

#include <stdio.h> 
int main(void) { 
    int n,c,value,sum=0; 
    printf ("Enter the no of integers u want to add:"); 
    scanf ("%d",&n); 
    printf ("Enter %d integers:",n); 
    for (c=1;c<=n;c++) { 
     scanf ("%d",&value); 
     sum=sum+value; 
    } 
    printf ("Sum of the integers:%d\n",sum); 
    return 0; 
} 

的成績單:

pax> ./qq 
Enter the no of integers u want to add:3 
Enter 3 integers:1 2 3 
Sum of the integers:6 

pax> ./qq 
Enter the no of integers u want to add:5 
Enter 5 integers:10 
20 
30 
40 
50 
Sum of the integers:150