loop not exiting

Sep 19, 2019 at 6:06pm
For some reason when I enter -1, my loop is not exiting.

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
 #include <iostream>
using namespace std;

const int N =10;


int main()
{
    int i,j,n;
    float median,a[N],t;
    a[0] = 0.0;
    i = 0;
    while (a[i] != -1)
    {
        cout <<"\nEnter the expenditure record (e.g., $1.95, coffee, enter -1 to end):$" << endl;;
        cin >>  a[i];
        i = i+1;
    }
    n = sizeof(a);
    /* Sorting begins */
    for (i = 1 ; i <= n-1 ; i++) { /* Trip-i begins */
        for (j = 1 ; j <= n-i ; j++) {
            if (a[j] <= a[j+1]) { /* Interchanging values */
                t = a[j];
                a[j] = a[j+1];
                a[j+1] = t; }
            else
                continue ;
        }
    }
    /* sorting ends */
    cout << "\nSorted list of expenditure:" << endl;;
    for (i = 1 ; i <= n ; i++)
        cout << a[i];
    /* calculation of median */
    if ( n % 2 == 0)
        median = (a[n/2] + a[n/2+1])/2.0 ;
    else
        median = a[n/2 + 1];
    cout << "\nThe Median is %f \nBye!"<< median;
}

Sep 19, 2019 at 6:24pm
It doesn't end because you incremented i at the end of the loop.

By the way since you're using arrays you should also insure that i never overflows the bounds of the array.

By the way have you looked at the value of n before your second loop.
Sep 19, 2019 at 6:25pm
You enter a[i], but then you increment i to be i+1 before checking the value of a[i] again.

It's actually a bit more of a problem than just that, as you attempt to check a[1], you are accessing uninitialized data because you never initialized a[1] to a proper value before checking its value.

This would be easiest to solve by changing it to an unconditional loop w/ break

1
2
3
4
5
6
7
while (true)
{
    cin >> a[i];
    if (a[i] == -1)
        break;
    i++;
}


or add the max N as the condition,
1
2
3
4
5
6
7
while (i < N)
{
    cin >> a[i];
    if (a[i] == -1)
        break;
    i++;
}
Last edited on Sep 19, 2019 at 6:27pm
Sep 19, 2019 at 8:46pm
Dang. I misread the topic as “loop not exciting”, but now that I’m here...

May I suggest another method for controlling the loop?
Separate the input and the loop variables.

Get input as a STRING, and convert it to a float afterwards.

1
2
3
#include <iostream>
#include <sstream>
#include <string> 
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
  // Here is the array of expenditures, complete with
  // a maximum and a current count of expenditures
  const int Maximum_Number_of_Expenditures = 10;
  float expenditures[Maximum_Number_of_Expenditures];
  int number_of_expenditures = 0;

  // Our loop will allow us to enter zero to the maximum number of expenditures.
  // (We will deal with other exit conditions inside the loop.)

  // Instructions for the user before we enter the loop:
  cout << "Enter up to " << Maximum_Number_of_Expenditures << " expenditures, one per line, as a dollar amount (e.g., 1.95).\n";
  cout << "Press Enter twice to finish.\n";
  while (number_of_expenditures < Maximum_Number_of_Expenditures)
  {
    // Instruct the user:
    cout << (number_of_expenditures + 1) << ": $";

    // Get the STRING input:
    string s;
    getline( cin, s );

    // Are we done?
    if (s.empty()) break;

    // Try to convert the user’s input to a float.
    float expenditure;
    istringstream ss{ s };
    iss >> expenditure;
    if (!ss)
    {
      cout << "That was not a dollar amount. Try again.\n";
      continue;
    }

    // Add the expenditure to the list
    expenditures[number_of_expenditures++] = expenditure;
  }
  // (We asked the user to press Enter twice to exit, but if he entered the
  // maximum number, we should still accept that additional Enter press.
  if (number_of_expenditures == Maximum_Number_of_Expenditures)
  {
    string s;
    getline( cin, s );
  }

Hope this helps.
Topic archived. No new replies allowed.