1

I am pretty much new in C++. I started with "Programming principles and practice using C++" book. It has been great so far but I have problem with some of the commands compilation. I wrote a simple code as follows:

int main()
{
    vector<double> temps;
    for (double temp; cin >> temp;) {
        temps.push_back(temp);
    }
    double sum = 0;

    for (int x:temps) sum += x;
    cout << "\nAverage temperature is: " << sum / temps.size() << endl;

    sort(temps);
    cout << "Median temperature is: " << temps[temps.size()/2] << endl;

}

Unfortunately, I am receiving compilation error on the for-loop as: error: range-based 'for' loops are not allowed in C++98 mode.

I have downloaded the newest version of CodeBlocks with new GCC compiler, but still gives me the same error. I have also tried other compilers in CodeBlocks but they didn't resolve the issue.

I had the same experience with "constexpre" command as well which I could not resolve it. By the way, examples and exercises of the book sometimes need a ".h" file which I have it on the folder which the this file sits. Any advise?

Theolodis
  • 4,977
  • 3
  • 34
  • 53
zero point
  • 1,290
  • 3
  • 10
  • 15
  • 2
    Even if your compiler is new enough to support C++11, it isn't on by default. See [How can I add C++11 support to code::blocks compiler?](http://stackoverflow.com/questions/18174988/how-can-i-add-c11-support-to-codeblocks-compiler) – HostileFork says dont trust SE Jul 29 '14 at 16:31
  • thanks for your response, I followed the setting suggested in that post, but it did not resolve the issue. Now I get a new error and compiler opens a new file "locale_facets_nonio.h": error: template-id 'do_get<>' for 'String std::messages::do_get(std::messages_base::catalog ) ... – zero point Jul 30 '14 at 14:53
  • 1
    Sounds like it *did* resolve the issue you asked about. You just have a new question. It's better to ask new questions as new questions instead of in comments; there's room to edit and add important stuff like line numbers. That said...I notice you don't have namespaces on your `cout` or `endl`. You need to either change those to `std::cout` and `std::endl` or put in `using namespace std;` And of course you have to `#include `. – HostileFork says dont trust SE Jul 30 '14 at 15:04

1 Answers1

1

The problem is:

for (int x:temps) sum += x;

It is, as the compiler tells you, a ranged based for loop and has come with c++11. If you want to keep the loop you do need to compile with either the GCC compiler option -c++0x or -c++11 depending on your actual compiler version.

A work around would be to use a standard for loop:

for ( std::vector<int>::iterator it = temps.begin() ; it != temps.end(); ++it) sum+=*it;
Theolodis
  • 4,977
  • 3
  • 34
  • 53
  • well, it may work as your workaround suggest, but I want to have range-based for-loop capability. – zero point Jul 30 '14 at 14:50
  • @zero-point that's why it is a work around. The solution is, as stated in my answer, to add the `-c++11` compile flag (refer to the question yours is marked as duplicate of) – Theolodis Jul 30 '14 at 15:18