3
#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.

Slay
  • 221
  • 1
  • 5
  • 12
  • 6
    Compile with `-std=c++11`. Also, this is better done by `std::transform` or a ranged for loop. – chris Sep 23 '13 at 00:43
  • The compiler has c++11 enabled. – Slay Sep 23 '13 at 00:45
  • 2
    Then get a newer version I suppose. – chris Sep 23 '13 at 00:47
  • Is "auto changes meaning" really an error or just an informational warning? – AnT stands with Russia Sep 23 '13 at 00:51
  • Can you `Cut` and `Paste` the exact error message. Also the exact command line you are using to compile the file. – Martin York Sep 23 '13 at 00:52
  • What compiler and version is it? By what process are you compiling? Because it sure doesn't sound like you have C++11 enabled, seeing as how this is the exact error message `g++` gives when you *don't* have C++11 enabled. – Benjamin Lindley Sep 23 '13 at 00:53
  • C:\CodeBlocks\second\main.cpp|8|warning: 'auto' changes meaning in C++11; please remove it [-Wc++0x-compat]| – Slay Sep 23 '13 at 00:54
  • C:\CodeBlocks\second\main.cpp||In function 'int main()':| C:\CodeBlocks\second\main.cpp|8|warning: 'auto' changes meaning in C++11; please remove it [-Wc++0x-compat]| C:\CodeBlocks\second\main.cpp|8|error: 'it' does not name a type| C:\CodeBlocks\second\main.cpp|8|error: expected ';' before 'it'| C:\CodeBlocks\second\main.cpp|8|error: 'it' was not declared in this scope| ||=== Build finished: 3 errors, 1 warnings (0 minutes, 0 seconds) ===| – Slay Sep 23 '13 at 00:55
  • I use codeblocks. I'm really comfortable with it. – Slay Sep 23 '13 at 00:58
  • 2
    Add the warning information to the question instead of posting it in the comment. – Yu Hao Sep 23 '13 at 00:59
  • Are you comfortable with adding options for codeblocks to pass to the compiler? Because despite your claim, you in fact do not have C++11 enabled. – Benjamin Lindley Sep 23 '13 at 00:59
  • How do I enable it? If someone could guide me, please. – Slay Sep 23 '13 at 01:10
  • http://stackoverflow.com/a/17203469/440119 – Benjamin Lindley Sep 23 '13 at 01:20
  • A Google search for that error message turns up only this question. Please copy-and-paste the exact error message into your question. (Code::Blocks is an IDE, which has to be configured to use some compiler; unless the message is from your IDE, it's the compiler that's relevant.) – Keith Thompson Sep 23 '13 at 03:27

2 Answers2

10

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.

prehistoricpenguin
  • 6,130
  • 3
  • 25
  • 42
3

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