Input into Array

I am creating a program to create an array of integers, read in numbers inputted by the user until the user enters a certain number.

I would like to output the number of values entered which I think I accomplished below but I also need to sum the number of values entered which is where I am stumped.


const int SIZE = 100;


int count = 0;
int Array[SIZE];

for (int i = 0; i <= SIZE; i++)
{
count++;
cout << "Enter numbers followed by -1 when you are done " << ": ";

cin >> Array[i];
cout<<count-1;

int sum = 0; //THIS PART where I am Stuck
sum = sum + Array[i];
cout << "sum is" << sum;

if (Array[i] == -1)
break;



}



Last edited on
closed account (SECMoG1T)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

const int SIZE = 100;
int count = 0

int main ()
{
  int Array[SIZE], temp;
  cout << "Enter numbers followed by -1 when you are done;
  
  cin>> temp;/// i'll use a temporal variable to check if value  is equal to -1
  while (count <100&& temp!=-1)
    {    
         Array [i]=temp;
        cin>> temp;
    }

    ///use a loop display the  values
}
 
Change:
1
2
3
4
if(Array[i] > 0)
{
    sum = sum + Array[i];
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Example program
#include <iostream>
#include <string>

using namespace std;
int main()
{
const int SIZE = 100;


int count = 0;
int sum = 0;
int Array[SIZE];

for (int i = 0; i <= SIZE; i++)
{
count++;
cout << "Enter numbers followed by -1 when you are done " << ": ";

cin >> Array[i];
if(Array[i] > 0)
{
    sum = sum + Array[i];
}
cout<<count-1 << endl;;

if (Array[i] == -1)
break;



}

    cout << "Total numbers entered is: " << sum << endl;
}



Remember to declare
sum
before using it.

if you do this the result will be incorrect since you add -1 to sum. That's why i used if statement above.
 
sum = sum + Array[i];
Last edited on
closed account (SECMoG1T)
Sorry I missed the point
@fortune500,
Remember to use code tags next time.
Oh it makes sense now!

Thanks Jacob and Andy I will use the code tags henceforth.
Topic archived. No new replies allowed.