auto and user input.

Is there any way that you can store data input from a user of unknown type into
an auto variable? It looks like I must immediately initialize the variable and there does not seem a way to do that without limiting the type of user input.

example:

auto strength;

std::cin >> strength;

this gives an error message:

error: declaration of 'auto strength' has no initializer|

The compiler isn't THAT smart. Another issue is that user input happens at runtime, but auto happens at compile time.

Instead, you can use getline and read input to a string, then look at the string and try and figure out what they entered.

Out of curiosity, why do you need this? Were you just curious if it was possible?
I had a book that was talking about c++0x(c++11) and the auto keyword and the explanation seemed to imply you could input what ever into an auto variable and yet you must immediately initialize the variable. I was wondering if I was missing something and could not find any answer. Thanks for answering.
closed account (Dy7SLyTq)
no autos meant for using iterators in for loops as counters and when using templates. if you want that... look at boost.any
@DTSCode: that is only one of many uses for auto.

@BitRat: can you quote the text you read? You may have misunderstood.
closed account (Dy7SLyTq)
@LB: what are some other uses?
Some functions in the standard have an unspecified return type, meaning you cannot possibly know what the return type is in advance. You must use auto in these cases.

An example of this is std::bind:
http://en.cppreference.com/w/cpp/utility/functional/bind

auto is also useful for when you're lazy and don't want to type the full type out and you know it's safe to use auto. This comes into play with some really crazy nested templates.
Last edited on
closed account (Dy7SLyTq)
like i said, its for templates. bind is a templated function so you would use it when you dont know what types you will be needing
Another use for auto:

1
2
3
4
5
6
7
8
9
10
SomeOtherScope::SomeClass::Type SomeOtherScope::SomeClass::func()
{
  //...
}

// vs

auto SomeOtherScope::SomeClass::func() -> Type
{
}


</slightly off topic>

Basically what they said. auto does not do what you want here. You can't determine the type at runtime... it has to be compile time.
Last edited on
closed account (Dy7SLyTq)
i didnt give an exhaustive list.
auto is also useful for when you're lazy and don't want to type the full type out and you know it's safe to use auto.
thats another one i forgot about.
DTSCode wrote:
like i said, its for templates. bind is a templated function so you would use it when you dont know what types you will be needing
auto has nothing to do with templates.
closed account (Dy7SLyTq)
Sure it is. Let's say I want to make a sort algorithim by passing an stl contsiner to it. I would use a template for the unknown container type and auto for the iterator
Basically it deduces the type from a given value. Ex if you set an auto value equal to a double auto becomes double if you set auto even to a string it becomes a string value same with iterators and lambdas and templated stuff like that.


Basically the format is this

1
2
auto variable_name = value_of_a_certain_type;
//variable_name is now the type of the value 


ex:

1
2
3
4
auto num = 1.2;
//num is now a double
auto str = "Hello";
//str is now a string 
DTSCode wrote:
Sure it is. Let's say I want to make a sort algorithim by passing an stl contsiner to it. I would use a template for the unknown container type and auto for the iterator
You can use auto without templates and templates without auto.
closed account (Dy7SLyTq)
i never said one is neccesary to use with the other. as shown above by your being too lazy to type out the type, and dischs example, templates arent required. i only said that ive typically seen auto used in those cases i mentioned
Ah, thanks for clearing up the misunderstanding.
closed account (Dy7SLyTq)
np
This is the code I was reading in a book that said I must immediately initialize an auto variable:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 1: #include <iostream>
 2:
 3: int main()
 4: {
 5:     // define character values
 6:     auto strength;
 7:     auto accuracy;
 8:     auto dexterity;
 9:
10:     // define constants
11:     const auto maximum = 50;
12:
13:     // get user input
14:     std::cout << "\nEnter strength (1-100): ";
15:     std::cin >> strength;
16:
17:     std::cout << "\nEnter accuracy (1-50): ";
18:     std::cin >> accuracy;
19:
20:     std::cout << "\nEnter dexterity (1-50): ";
21:     std::cin >> dexterity;
22:
23:     // calculate character combat stats
24:     auto attack = strength * (accuracy / maximum);
25:     auto damage = strength * (dexterity / maximum);
26:
27:     std::cout << "\nAttack rating: " << attack << "\n";
28:     std::cout << "Damage rating: " << damage << "\n";
29: }


I was very confused and then I downloaded the sample code from the authors site(http://workbench.cadenhead.org/book/cpp-24-hours/source/chapter21/Combat.cpp) and saw the different contradictory type assignment for lines 6 to 8. So after a bit of frustration and swearing I moved on till I read one of the end of chapter activities:

Write a program with four overloaded square() functions that multiply a number by itself and take int, long, float, and double as their only parameter. Store the result of the function in an auto-typed variable and display it.

This made me wonder what the point of the above exercise was because I had already worked on overloaded functions and it seemed pretty annoying to do so much work just to store it in one auto variable.(What a challenge!) I started to lose confidence that there wasn't more to auto. I thought all you guys would surely be able to point out what I had missed. So far I do not believe I have missed anything and will probably toss the book aside.
This is the code I was reading in a book that said I must immediately initialize an auto variable:


auto uses the type of the initializer to determine the type of the variable to declare. If there is no initializer, there is no information available for it to use to determine the type the variable should be.
Topic archived. No new replies allowed.