Error using sentinel with an Array

Hello,

When I fill the array my program works, but if I use the sentinel to end the loop (in this case -1) the output gets messed. Can anyone see what I'm doing wrong?

#include <iostream> // for console input/output
#include <iomanip> // for setprecision

using namespace std;

int main()
{
const int NUM_ARR =10; //array
int num_ent[NUM_ARR]; // what numbers will be enters in
int count = 0;
int totalsum;
int avg_num, low_num, number;
int new_num[NUM_ARR];


cout<<fixed<<showpoint<<setprecision(5);

cout<<"Enter up to 10 numbers"<<endl;
cout<<"or enter -1 to quit"<<endl;
cout<<"Enter number:";
cin>>number;

while(number != -1 && count < NUM_ARR) //to loop array
{
count++;
num_ent[count -1] =number;
cout<<"Enter number:";
cin>> number;
}
cout<<"The count is:"<<count<<endl;

for(int count = 0; count<NUM_ARR; count++)

new_num[count] = num_ent[count];

low_num = new_num[0];

for(int count = 0; count<NUM_ARR; count++) //to enter numbers into array for lowest number
{
if(new_num[count] < low_num)
low_num=new_num[count];
}


totalsum = 0;
for(int count = 0; count<NUM_ARR; count++) //to enter numbers into array for sum
{
totalsum += num_ent[count];
}


for(int count = 0; count<NUM_ARR; count++) //to enter numbers into array for average
{
avg_num =totalsum/NUM_ARR;
}

cout<<"The lowest number is:"<<low_num<<endl;
cout<<"The sum of all the numbers is:"<< totalsum<<endl;
cout<<"The average of all the numbers is"<< avg_num<<endl;


system ("pause");
return 0;
}
Last edited on
This is my implementation:

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
#include<iostream>
using namespace std;

int main() {

	int array[10] = {0};
	int element = 0;
	int counter = 0;
	int mega = 0;
	int average = 0;

	cout << "Enter up to 10 positive integers, to stop entering integers enter -1" << endl;

	while (true) {
		if ((cin >> element) && (element != -1)) {
			array[counter] = element;
			counter++;}
		else if (counter == 9) {
			break;}
		else {break;}}
	
	for (int i = 0; i < (counter + 1); i++) {
		mega += array[i];}

	average = mega/counter;

	cout << "The amount of elements entered was " << counter << endl;
	cout << "The average was " << average;

	while (true) {}

	return 0;}


This version allows for decimal point averages, I think, lawl :

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
#include<iostream>
using namespace std;

int main() {

	int array[10] = {0};
	double element = 0;
	int counter = 0;
	double mega = 0;
	double average = 0;

	cout << "Enter up to 10 positive numbers, to stop entering numbers enter -1" << endl;

	while (true) {
		if ((cin >> element) && (element != -1)) {
			array[counter] = element;
			counter++;}
		else if (counter == 9) {
			break;}
		else {break;}}
	
	for (int i = 0; i < (counter + 1); i++) {
		mega += array[i];}

	average = mega/counter;

	cout << "The amount of elements entered was " << counter << endl;
	cout << "The average was " << average;

	while (true) {}

	return 0;}
Last edited on
I'm still not seeing what I'm doing wrong. Why are my outputs coming out as they are?
Topic archived. No new replies allowed.