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;
} 

I have tried above code. What I want to do is to add numbers taken from user but I get some other result.. can anyone tell me how to add inputs in above program ? thanks in advance

8 Answers8

3

It's because you don't initialize total. When a local variable is declared, it's value is undefined, and adding something to an undefined value leads to undefined behavior.

In reality, the value of an uninitialized local variable is whatever is in the memory location now occupied by the variable, and will be seemingly random.

You also have the problem that you don't actually add all entries in the array, only the one at index j, which is also an uninitialized variable, leading you to fetch a value from a random location (and most likely from beyond the limits of the array).


You don't actually need the array, just three variables: The loop counter i, the total (which you should initialize to zero) and a general value integer used in the input statement.

Then you do e.g.

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';

Note: You don't need to initialize the value variable in my example, because it's not read, only written to (and therefore initialized) in the input statement.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

Since an explanation for your problem has already been given, here is how your program could be in a shorter form:

#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();
}
David G
  • 94,763
  • 41
  • 167
  • 253
0

Your variable j is not initialized.

You may want to use cin.ignore(10000,'\n') instead of getch().

Also, conio is not standard and not required in your program.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
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;   
}
P0W
  • 46,614
  • 9
  • 72
  • 119
0

Both total and j are not initialized and their since they are local variables their initial value is undetermined. So the result of this line is also undetermined:

total+=Array[j];

Edit

So you edited your question so that the line is now:

total+=Array[i];

but since total is still not initialized the outcome is still undefined. Also it seems like you might have meant to include the line in the for loop, currently it is outside. Which means i will be 5 and you will also be accessing out of bounds. With a few minor edits this should do what you want:

int total = 0 ;
for(i=0; i<5; i++)
{
    cout<<"Enter the Numbers : ";
    cin>>Array[i];
    total+=Array[i];
} 
Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
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];
}
Richard Sitze
  • 8,262
  • 3
  • 36
  • 48
Jiminion
  • 5,080
  • 1
  • 31
  • 54
0

Try this one:

#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;
} 
Aayushi Jain
  • 2,861
  • 2
  • 29
  • 36
0

See the below code and the comments for explaination

#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;
} 
Saksham
  • 9,037
  • 7
  • 45
  • 73