I'm writing this program right now where the user inputs integers with a limit of 100 integers and entering -999 to denote the end of the list. I'm having some trouble... it seems like the way that I have it written now, the program must read 100 integers no matter what. But I don't know what to change. I tried flipping around my "while" and "for" statements, but then that gave me some kind of internet explorer error and shut down. Anyways, if anyone can take a look and see what I have going wrong here, let me know. I'd really appreciate it.
Code:
#include <iostream>
#include <iomanip>
using namespace std;
const int sentinel = -999;
void selectionSort(int number[], int length);
int main()
{
int number[100];
int index;
cout << "Enter a maximum of 100 positive integers ending with " << sentinel << endl;
for (index = 0; index < 100; index++)
{
while (number[index] != sentinel)
{
cin >> number[index];
}
}
selectionSort(number, 100);
for (index = 0; index < 100; index++)
cout << number[index] << endl;
return 0;
}
void selectionSort(int number[], int length)
{
int index;
int smallestIndex;
int minIndex;
int temp;
for (index = 0; index < length - 1; index++)
{
smallestIndex = index;
for (minIndex = index + 1; minIndex < length; minIndex++)
if (number[minIndex] < number[smallestIndex])
smallestIndex = minIndex;
temp = number[smallestIndex];
number[smallestIndex] = number[index];
number[index] = temp;
}
}