Stuck in a loop

I can't get out of my loop to fill the array.
This array should stop and print after 10 inputs. but it keeps on going.

This is what my code is supposed to do.
Use 0 as sentinel for an array.
Only positive ints will be entered into the array.
A negative int should result in an error message, then input will continue.
The numbers will be inserted into the array in ascending order. Do not insert them then sort the array.


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
 

#include <iostream>
#include <cmath>

using namespace std;
const int arraySize =10;
const int sentinel = 0;

int main ()
{     
  int array[arraySize];
  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
    {    // code to arrange the input without sorting the array.  
	  
      while (!found && pos < count)
      {
	if (array[pos]>num)
	  found = true;
	  else
	  pos++;
	      
	  for (index=count; index > pos; index--)
	    {
	      array[index]=array[index-1];
	      array[pos]=num;  
	      count++;
	    }  	    
       }
      
     }
    
    cin >> num;
  }

cout << array[index] << " " << endl;

  
 return 0; 
}
Last edited on
You never tell the loop to stop after 10 times. You tell it to run untill num = sentinel (which is 0)

So the loop will run forever until the user inputs 0.

Also, this wont work - cout << array[index] << " " << endl;
this code happens outside of the for loop. You probably want it inside so it can print out the entire array. Or make a new for loop that prints all of them out.
Last edited on
Thank you.
Sorry that is what it's supposed to do, since it's a sentinel controlled loop.

The issue is; it should give error in line 25(code below) when more than 10 inputs have been inserted.

And I made a new loop to print the array now.

It prints garbage and not the ints stored in the array, no matter the numbers of ints in the array

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

#include <iostream>
#include <cmath>

using namespace std;
const int arraySize =10;
const int sentinel = 0;

int main ()
{     
  int array[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
    {  // code to arrange the input without sorting the array.  	  
      while (!found && pos < count) 
      {
	if (array[pos]>num)
	  found = true;
	  else
	  pos++;
	      
	  for (index=count; index > pos; index--)
	    {
	      array[index]=array[index-1];
	      array[pos]=num;  
	      count++;
	    }  	    
       }
      
     }
    
    cin >> num;
    

  }
    for (index=0; index<arraySize; index++)
      cout << array[index] << " ";  
  
 return 0; 
}
I might have found the problem. Each loop, you want to add +1 to the variable "count". And you want, when count == 10 that the program quits. What the problem is, is that the count++
is inside this loop -

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 while (!found && pos < count) 
      {
	if (array[pos]>num)
	  found = true;
	  else
	  pos++;
	      
	  for (index=count; index > pos; index--)
	    {
	      array[index]=array[index-1];
	      array[pos]=num;  
	      count++;
	    }  	    
       }


Whats inside the while loops parameters is a problem. The program never enters that while loop becuase, you initialize count to be 0. And you initialize pos to be 0. So when it gets to that while loop, 0 is not bigger than 0, so it wont enter the loop. it skips it and goes directly to cin >> num; Then, count is still 0 because it never entered the loop so the program never uses count++. And pos is still equal to 0.
Last edited on
I think the way you are doing your menu is cool but I was having issues debugging it.

So I rewrote your menu in a way that make more sense to me.
I hope this helps get you going.

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

#include <iostream>
#include <cmath>

using namespace std;
const int ARRAYSIZE = 10;
using namespace std;

int main ()
{
    bool run_menu_1 = true;
    int myArray[ARRAYSIZE];
    int count = 1;
    int input = 0;
    cout << "Please enter a positive interger or 0 to quit" << endl;
    while ( run_menu_1 )
    {
        cin >> input;
        if ( input == 0 )
            run_menu_1 = false;
        if ( count < 10 )
        {
            myArray[count] = input;
            count++;
        }
        else
        {
            cout << "You have 10 digits, exiting";
            run_menu_1 = false;
        }
    }

        // enter code here to do things.... to your array


}
Last edited on
@Bdanielz provided you with a much better and cleaner code.

I advise you to learn how to debug your program. That way you can much easier find problems.
Hello guys thanks for your input.
To explain better this is what I have to do.

1. I have to start with a sentinel control loop
1a- check for negative input
1b- check for entry count so that negative input does not increment count / let user know that when 10 elements are in the array.

2. Nest a while loop that will sort and populate the array. // algorithm provided below

3. Print the array.

//Sort array Algorithm.
inserting a value num into an array maintaining ascending order

find the position pos in the array to insert num
move all values in the array from pos and on, down one location
insert the new value num at the position pos
add 1 to count

Stepwise Refinement

Step 1 - find the position pos in the array to insert num

set pos to 0
set found to false
while not found and pos < count
if numbers[pos] > num
set found to true
else
add 1 to pos


Step 2 - move all values in the array from pos and on, down one location

for i is count downto pos+1
set numbers[i] to numbers[i-1]

Final Algorithm

set pos to 0
set found to false
while not found and pos < count
if numbers[pos] > num
set found to true
else
add 1 to pos
for i is count downto pos+1
set numbers[i] to numbers[i-1]
set numbers[pos] to num
add 1 to count

// hence why is what I came up with
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]; //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
    {  // code to arrange the input without sorting the array.  	  
      while (!found && pos < count) 
      {
	if (array[pos]>num)
	  found = true;
	  else
	  pos++;
	      
	  for (index=count; index > pos; index--)
	    {
	      array[index]=array[index-1];
	      array[pos]=num;  
	      count++;
	    }  	    
       }
      
     }
    
    cin >> num;
    

  }
    for (index=0; index<arraySize; index++)
      cout << array[index] << " ";  
  
 return 0; 
}
You're getting junk because of the way you're trying to insert the item into the sorted array. The loop at lines 36-41 is in the wrong place. You want something like:
1
2
3
4
5
6
7
8
9
10
11
for (pos = 0; pos < count ++pos) {
    if (array[pos] < num) break;
}

// num should go at position pos. Move
// the remaining items up one position
for (int i=count; i > pos; --i) {
    array[i] = array[i-1];
}

array[pos] = num;

i think your error is here
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 while (!found && pos < count) 
      {
	if (array[pos]>num)
	  found = true;
	  else
	  pos++;
	      
	  for (index=count; index > pos; index--)
	    {
	      array[index]=array[index-1];
	      array[pos]=num;  
	      count++;
	    }  	    
       }
      
     }


so i amend as follow


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; 
}
thank you so much. it worked.
@yuzhengtian
Thanks everyone
Last edited on
Topic archived. No new replies allowed.