Please this is driving me crazy. I just want to make a sentinel controlled loop that will fill an array and print the array afterwards.
My error check is working but my I do not get an array printed out afterwards.
I have tried debugging on Eclipse but I do not know how to enter in more than one enrty while stepping through.
#include <iostream>
#include <cmath>
usingnamespace std;
constint arraySize = 10;
constint sentinel = 0;
int main()
{
int array[arraySize];
int i;
int input;
int count = 0; // starting count will not be greater than arraysize
int pos =0;
cout << "Enter up to 10 positive integers, 0 to quit:" << endl;
cin >> input;
while (input != sentinel && count < arraySize)
{
if (input < 0)
{
cout << "Positive input only, Enter 0 to quit. " << endl;
}
bool found = true;
while (!found && pos < count)
{ // this will determine if an input should be moved further down
if (array[i]>input) // if array element is greater than input
found = true;
else
pos++;
for (i=count; i>pos; i--) // when true iteration loop to sort the location of the input
{
array[i]=array[i-1];
array[count++]=input;
}
}
cin >> input;
}
if (count >= arraySize)
cout << "10 numbers reached, entry ignored." << endl;
for (i = 0; i < count; i++)
cout << array[i] << " ";
cout << endl;
return 0;
}
Thanks to everyone for their contribution.
Here is a code that works without just right without adding extra keywords or changing the initial structure of my code.
Thanks to @yuzhengtian
#include <iostream>
#include <cmath>
usingnamespace std;
constint arraySize =10;
constint sentinel = 0;
int main ()
{
int arr[arraySize]; //array as array index
int num;
int count = 0;
int index;
double average = 0;
int pos = 0;
bool found = false;
cout << "Enter up to 10 positive integers, 0 to quit: \n";
cin >> num;
while(num != sentinel)
{
if (count == arraySize)
cout << "You have 10 digits, more entries will be ignored. Enter 0 to quit";
elseif (num<0)
cout << "Please enter a positive interger or 0 to quit" << endl;
else
{ // find the insert position.
while(!found&&pos<count)
{
if(arr[pos]<=num)
{
pos++;
}
else
{
found=true;
}
}
found=false;//reset the found
//move arr[pos]~arr[count-1]to arr[pos-1]~arr[count]
int i=count;
while(i>pos)
{
arr[i]=arr[i-1];
i--;
}
arr[pos]=num;//insert the positive integer
pos=0;//reset the pos;
count++;//the number of input add one
}
cin >> num;
}
for (index=0; index<arraySize; index++)
cout << arr[index] << " ";
return 0;
}