Range based

Hey all this code is actually from the tutorial section, and it's not working... I'm brand brand brand new, I have been manually typing out code to get a feel for the language and re-enforce what I am learning. But in this case I even tried copy paste of the code in my code blocks compiler.

// range-based for loop
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string str {"Hello!"};
for (char c : str)
{
std::cout << "[" << c << "]";
}
std::cout << '\n';
}

This example is in the halfway point of the Statements and flow control, and I'm a getting an error on sting str {"Hello!:}; line it says...

error: in C++ 98 'str' must be initialized by constructor, not by {...}

Also curios as to why the using namespace std; line is used in this code and
std::cout << '\n'; as earlier I read that after using namespace std; you can just use cout << "declaration"; Thanks in advance
This is the clue:

error: in C++ 98 ...


You need to go to the global compiler settings (settings->compiler->compiler settings tab) and make sure to check C++11 (or better) mode (Have g++ follow the C++11 ISO C++ language standard [-std=c++11]), and try again.

Good luck!
Also curios as to why the using namespace std; line is used in this code and std::cout << '\n';

The std::cout rather than cout is prob just habit. Or cut & paste.

Andy
Thank you very much for your prompt answers.
Topic archived. No new replies allowed.