I have an assignment where I enter a maximum of 100 values. I need to be able to cancel the input when I want.

#include <iostream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
void main()
{
	const int value=100;
	double middle, highest, lowest, sum = 0;
	double counter[value + 1];
	int i, val;


	for (i=1; i<=value; i++)


	{	
		cout << "Enter some values: ";
	while (cin >> value)
		cin >> counter[i];
		sum+= counter[i];
	}
	
  middle = sum / value;
  



system("cls");
  cout << "1.  highest" << endl;
  cout << "2.  lowest" << endl;
  cout << "3.  middle" << endl;
  cout << "4.  Lista" << endl;
  cout << "5.  end" << endl;
  cout << endl << "Välj: ";
  cin >> val;

}

Recommended Answers

All 2 Replies

One, use code tags:

Two, what's the question?

Three, don't use void main () even if your compiler lets you get away with it. Use int main ().

for (i=1; i<=value; i++)
{
    cout << "Enter some values: ";
    while (cin >> value)
         cin >> counter[i];

    sum+= counter[i];
}

middle = sum / value;

Is this really what you mean to do? It doesn't make any sense.

Did this even compile for you? (I don't think so - cin >> value??)
If it did, what you're doing is reading in (perhaps many) numbers till ctrl-z, adding the only the last read value to sum. Move to next array position, do it again. At the end, when you've gone through all hundred iterations (where you could have entered numbers infinite number of times) you find the average of whatever where the last read numbers in each cycle.

Perhaps you really need just the while loop, which ends either upon ctrl-z or the index counter reaching the max index. Be sure to increment that as you go along.

Also, is there a particular reason you allocate 101 elements to array and only use indexes 1-100? More common method would be

int arr[100];
   for( int i = 0; i < 100; i++ ) //note test is strictly less than size
      //do something to arr[i]
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.