how to terminate while loop

Hi this is an example from Programming principles and practice book from Bjarne Stroustrup.


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
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    vector<double>temps;  //creates an empty vector temps
    double temp;
    while(cin >> temp){
              temps.push_back(temp);  //put input elements into vector
    }
    
    double sum = 0;
    for(int i = 0;i < temps.size(); i = i + 1)
            sum = sum + temps[i];
            
    //mean temperature
    cout  << "Average temperature: " << sum / temps.size() << endl;
    
    //compute median temperature
    sort(temps.begin(), temps.end());   //sort temps from beggining to end
    cout << "Median temperature: " << temps[temps.size()/2] << endl;  //[temps.size()/2] picks element in middle
    
    cin.ignore();
    cin.get();
}



How to terminate this loop:
1
2
3
  while(cin >> temp){
              temps.push_back(temp);  //put input elements into vector
    }



And, what is the difference between ++i and i++

thanx
The while loop will terminate when an invalid data type (something that isn't a double) or an EOF (end-of-file) is entered.
If you input a character, the loop will exit. In the console, if you type ctrl+z (an eof), the loop will exit.

i++ is the post-increment operator, meaning it will increment the object after it is used.
Example:
1
2
int i = 5;
cout << i++ << ", " << i << endl;

would output:
5, 6


++i is the pre-increment operator, which will increment the object before it is used.
Example:
1
2
int i = 5;
cout << ++i << ", " << i << endl;

would output:
6, 6


In general, there are advantages to using the pre-increment operator over the post-increment operator (speed and memory use), unless the use of post-increment is being taken advantage of, as in the first example.
The way I think of it is, pre-increment might be faster than post-increment, but post-increment will never be faster than pre-increment.
So I generally recommend using post-increment only when you are taking advantage of it's functionality; use pre-increment everywhere else.

If you were to write functions for these two, they would look something like this:
1
2
3
4
5
6
7
8
9
10
11
12
int postIncrement(int number)
{
    int temp = number; //additional step involved in postIncrement.
    number = number + 1;
    return temp;
}

int preIncrement(int number)
{
    number = number + 1;
    return number;
}


EDIT: Tiny addition and spelling.
Last edited on
@Thumper
Be careful with your examples. Modifying a variable and using it within the same statement is not always defined, so I recommend against teaching anything that way.
@L B
I was only intending to show the difference between the two. That is the most clear way imo to show it.
Thank you for mentioning, though. Perhaps better would be
1
2
3
4
int x = 5, y;
y = x++;

cout << x << ", " << y << endl;\
6, 5
thanx for info :D
Topic archived. No new replies allowed.