help with loop

my loop is infinite when I try to reach the "CAP" value but it does stop when I enter the sentinel value. Can anyone help me understand why?

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
 

// ------------------------------------------------------------------------
// Function: Read up to CAP integer values into array from keyboard, 
//           stopping when CAP values have been read or when the sentinel
//           value is read.
// ------------------------------------------------------------------------
void LoadArray(int A[], int CAP, int & N, int Sentinel, string Prompt)
{  
 int k; 
  

    
   for(k=0;k<=N;k++)
{
   
 do
{
  cout << Prompt;
   cin >> A[k];

} while(A[k] != Sentinel || A[k] < CAP);
} 

cout << endl;
}
needs to be && instead of ||

By using the OR operator the loop will run as long as one of the booleans evaluates to true, so if there is no Sentinel value entered it will keep going infinitely
I figured out it is not stopping because the way I have it written it is saying keep going WHILE the value of A[k] is under the cap .

what I need it to do is stop once the number of arrays written reaches the cap. How would I do that?

and also do you know how I could write it so that the value of int N is updated to be the number of values the array holds.

here is how I call the function:

1
2
3
4
5
//-| -----------------------------------------------------------
   //-| 3. Load array Num with values from keyboard terminated by
   //-|    sentinel 7777, using prompt "Enter next value (7777 to STOP): ".
   //-| -----------------------------------------------------------
   LoadArray(Num, 10, Num_size, 7777, "Enter next value (7777 to STOP): ");
I think you could just change A[k]<CAP to k<CAP.

And just set N=k within the loop, that way it will become the value of k, which is the number of elements in the array.
I tried your changes and its still not stopping once it reaches the cap.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// ------------------------------------------------------------------------
// Function: Read up to CAP integer values into array from keyboard, 
//           stopping when CAP values have been read or when the sentinel
//           value is read.
// ------------------------------------------------------------------------
void LoadArray(int A[], int CAP, int & N, int Sentinel, string Prompt)
{  
 int k; 
  

    
   for(k=0;k<=N;k++)
{
   
 do
{
  cout << Prompt;
   cin >> A[k];
   N = k;

} while(A[k] != Sentinel && k < CAP );
} 

cout 
i figured out the do while loop is causing it not to work with the cap. Any suggestions on editing it ?
bump
Why is your do/while loop inside a for loop ?

Your code should be
1
2
3
4
5
6
7
for( k = 0; k < CAP; k ++)
{
    cout << Prompt;
    cin >> A[k];
    if( A[k] == Sentinel )
        break;
}

or
1
2
3
4
5
6
k = 0;
do
{
    cout << Prompt;
    cin >> A[k];
}while( A[k] != Sentinel && ++k < CAP );
Topic archived. No new replies allowed.