Using auto

The included code does not compile on my 12-11 Codebooks. What's wrong?

#include <iostream>
using namespace std;

int main()
{
auto Flag = true;
auto Number = 2500000000000;

cout << "Flag = " << Flag;
cout << " , sizeof(Flag) = " << sizeof(Flag) <<endl;
cout << "Number = " << Number;
cout << " , sizeof(Number) = " << sizeof(Number) << endl;

return 0;
}
error: Flag does not name a type, latter Flag not declared
The same for Number.
Last edited on
What error message do you get?
The code looks fine. If you get any errors, that's not because of the syntax, but probably because of some issues with the compiler. Try using Microsoft Visual Studio 2010 or 2012 Express instead.
> What's wrong?

Enable C++11.

In Settings => Compiler => Flags check -std=c++11

While you are at it, also check these three -Wall -Wextra -pedantic-errors
Last edited on
JL, for me, it says, auto has no initializer, and when I set the variable to 0, it only returns numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int nNumberofArgs,char* pszArgs[])
{
    auto variable;
    cout << "Enter a word or a number: ";
    cin >> variable;

    cout << variable << endl;
    system("PAUSE");
    return 0;
}
This is different than your earlier example but...

The compiler can't determine the type of "variable" (i.e., what type "auto" should be replaced by) since you aren't assigning anything to it.
> and when I set the variable to 0, it only returns numbers.

When we use auto in this manner, we ask the compiler to deduce the type of a variable from the type of its initializer.

So this is an error; there must be an initializer: auto variable;

And with auto variable = 0 ;, the type of variable is deduced to be int
I have been away from my lessons for a few days and now when I run the program it compiles OK. Thank you all for your help.
Topic archived. No new replies allowed.