Reputation: 231
#include<iostream>
#include<string>
#include<iterator>
using namespace std;
int main()
{
string a("hello world");
for(auto it = a.begin(); it != a.end() && !isspace(*it); it++ )
{
*it = toupper(*it);
}
cout<<a;
}
There are two errors I get. One is as mentioned, "auto changes meaning in c++11" and the other is "!= operator not defined." Never had this problem before.
I only used the auto operator because the book suggested.
I'm a beginner, and getting back to learning after about 2 months. Having trouble catching up.
Upvotes: 4
Views: 8405
Reputation: 31
As chris mentioned, using Range-based for loop is much better. It's closer to spirit of C++11 and it's easier to learn for beginners. Consider:
#include <string>
#include <iostream>
int main()
{
std::string s{"hello, world"}; // uniform initialization syntax is better
for (auto& c : s) // remember to use auto&
if (!isspace(c)) c = toupper(c);
cout << s << '\n'; // remember the last newline character
return 0;
}
-- Saeed Amrollahi Boyouki
Upvotes: 3
Reputation: 6326
Your code runs ok when compiled with -std=c++11
, You may check it here.
You can add the option in Setting->Compiler->Have g++ follow the C++11 ISO C++ language standard [-std=C++11]
in CodeBlocks.
Upvotes: 10