0
#include <iostream>
#include<conio.h>
using namespace std;

int main()
{
    int Array[5];
    int i;
    int total;
    int j;

    for(i=0; i<5; i++)
    {
        cout<<"Enter the Numbers : ";
        cin>>Array[i];
    } 


    total+=Array[i];
    cout<<total<<endl;

    getch();
    return 0;
} 

我试过上面的代码。我想要做的是添加从用户那里获取的数字,但我得到了一些其他结果.. 谁能告诉我如何在上面的程序中添加输入?提前致谢

4

8 回答 8

3

这是因为你没有初始化total. 声明局部变量时,它的值是未定义的,向未定义的值添加某些内容会导致未定义的行为。

实际上,未初始化的局部变量的值是该变量现在占用的内存位置中的任何值,并且看起来是随机的。

您还有一个问题,您实际上并没有在数组中添加所有条目,只有 index 处的条目j,这也是一个未初始化的变量,导致您从随机位置获取值(并且很可能来自超出数组)。


您实际上不需要数组,只需要三个变量:循环计数器i、 (您应该将其初始化为零)和输入语句中使用total的通用整数。value

然后你做例如

int total = 0;

for (int i = 0; i < 5; ++i)
{
    std::cout << "Enter a number: ";

    int value;
    std::cin >> value;
    total += value;
}

std::cout << "Total is " << total << '\n';

注意:您不需要value在我的示例中初始化变量,因为它不是被读取的,只是在输入语句中写入(并因此被初始化)。

于 2013-07-27T17:18:29.680 回答
2

由于已经给出了对您的问题的解释,因此您的程序可以采用更短的形式:

#include <iterator>
#include <algorithm>
#include <iostream>

int main()
{
    std::cout << "The total is ";
    std::cout << std::accumulate(std::istream_iterator<int>(std::cin),
                                 std::istream_iterator<int>(), 0);

    std::cin.get();
}
于 2013-07-27T17:27:53.103 回答
0

请参阅下面的代码和注释以进行解释

#include <iostream>
//#include<conio.h>   we dont need this and it is obsolete
using namespace std;

int main()
{
    int Array[5];
    int i;
    int total;
    int j;

    for(i=0; i<5; i++)
    {
        cout<<"Enter the Number : ";
        cin>>Array[i];
        total+=Array[i];   //this should come inside the loop so that each element gets added to the total
    } 



    cout<<total<<endl;
    cin.ignore(); //use this over getchar() and cin.get() as it doesn't leave the input on the stream
    return 0;
} 
于 2013-07-27T17:27:06.783 回答
0

您的变量j未初始化。

您可能想要使用cin.ignore(10000,'\n')而不是getch().

此外,conio这不是标准的,在您的程序中也不需要。

于 2013-07-27T17:20:18.673 回答
0
int main()
{
    int Array[5];
    int i;
    int total =0; //Initialize this variable
    //int j; // Not Needed

    for(i=0; i<5; i++)
    {
        cout<<"Enter the Numbers : ";
        cin>>Array[i];
        total+=Array[i]; //<--- Note this correction for totaling
    }

    cout<<total<<endl;
    getch();
    return 0;   
}
于 2013-07-27T17:21:49.623 回答
0

两者totalj都没有被初始化,因为它们是局部变量,所以它们的初始值是不确定的。所以这条线的结果也是不确定的:

total+=Array[j];

编辑

所以你编辑了你的问题,现在该行是:

total+=Array[i];

但由于total仍未初始化,因此结果仍未定义。此外,您似乎可能打算在for循环中包含该行,目前它在外面。这意味着i将是5,您也将越界访问。通过一些小的编辑,这应该可以满足您的需求:

int total = 0 ;
for(i=0; i<5; i++)
{
    cout<<"Enter the Numbers : ";
    cin>>Array[i];
    total+=Array[i];
} 
于 2013-07-27T17:21:52.333 回答
0
int total = 0; 

//  Add second loop for total (or better, add it in the first one)
for(i=0; i<5; i++)
{
    total += array[i];
}
于 2013-07-27T17:21:56.427 回答
0

试试这个:

#include <iostream>
using namespace std;

int main()
{
int Array[5], i, total=0;

for(i=0; i<5; i++)
{
    cout<<"Enter the Numbers : ";
    cin>>Array[i];
    total = total + Array[i];
}

cout<<"The sum is: "<<total<<endl;

return 0;
} 
于 2013-07-27T17:22:21.443 回答