Populate array with While sentinel loop

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.

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
37
38
39
40
41
 #include <iostream>
#include <cmath>

using namespace std;

const int arraySize = 10;
const int sentinel = 0;


int main()
{   
    int array[arraySize];
    int i;
    int input;
    int count = 0; // starting count will not be greater than arraysize
    
    cout << "Enter up to 10 positive integers, 0 to quit:" << endl;
    cin >> input;
    
    while (input!=sentinel)
    {
        if (input<0)
	  cout << "Positive input only, Enter 0 to quit. " << endl;
        else if (count>arraySize)
	  cout << "10 numbers reached, entry ignored. Enter 0 to quit."
	else
        for (i=0; i<arraySize; i++)
        array[i]=input;
                
        
        
        cin >> input;
    }
   
   for (i=0; i<arraySize; i++)
   cout << array[i];
   
   
   
return 0;
}
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
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <cmath>

using namespace std;

const int arraySize = 10;
const int sentinel = 0;

int main()
{
	int array[arraySize];
	int i;
	int input;
	int count = 0; // starting count will not be greater than arraysize

	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;
			continue;
		}
			

		//else if (count>arraySize)
			//cout << "10 numbers reached, entry ignored. Enter 0 to quit.";
		//else
			//for (i = 0; i<arraySize; i++)
				//array[i] = input;

		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;
	system("pause");
	return 0;
}
Thanks much shadowCODE

Can you please explain why my code did not work?

I am also trying to sort the array as it is being entered. I want to have another while loop to do this nested in the first one. Here is what I have.

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

#include <iostream>
#include <cmath>

using namespace std;

const int arraySize = 10;
const int 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;
}
Last edited on
Thank you shadowCODE.
Can you tell me why my code did not work?

Your edit works, however count increases on a negative input. It is not supposed to.
I think of it like this

1. check for sentinel(while loop)
-check for negative input
-increment count
-sort and populate array

2. print array
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

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <cmath>

using namespace std;
const int arraySize =10;
const int 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";
    else if (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; 
}
Topic archived. No new replies allowed.