-1

After inputting the following code, I would get an error.

const int quantity;
cout << "How much spacing do you want in-between the frames? " ;
cin >> quantity;

error: uninitialised const 'quantity'[-fpermissive]

error: ambiguous overload for 'operator>>'

This does not happen if I just use the type int

int quantity;
cout << "How much spacing do you want in-between the frames? " ;
cin >> quantity;

Which compiles without a problem. I'm new to C++ so I'd just like to know why this is.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
James
  • 41
  • 7
  • `const` means constant. You can't assign a value to the constant except during its initialization. It's the `variable` that could be assigned a value anytime during the program life. – Rahul Verma Oct 03 '17 at 18:53
  • This thread says why: https://stackoverflow.com/questions/12279601/are-there-any-tricks-to-use-stdcin-to-initialize-a-const-variable – user3750325 Oct 03 '17 at 18:54
  • So there is no way to assign a value to a constant through user input? – James Oct 03 '17 at 18:54
  • 2
    No It's not possible. – Rahul Verma Oct 03 '17 at 18:55
  • You cannot (directly at least), but you could expose a `const` reference or a copy using a `get` function to whatever code you might be worried about modifying `quantity`. – George Oct 03 '17 at 18:56
  • read an int : `int dummy; cin >> dummy;` then initialize your constant with it `const int myConstant = dummy`. – Jean-Baptiste Yunès Oct 03 '17 at 18:57
  • Or you could say `int input; cin >> input; const int quantity = input;`. The word `const` doesn’t mean you need to know at compile time, just that you need to know when you create the variable. – Daniel H Oct 03 '17 at 18:57
  • @batMan there are two of us who left answers showing exactly how it's possible. – Mark Ransom Oct 03 '17 at 18:58
  • @MarkRansom: That's tricky. It's not possible directly using Istream. That's what I mean. – Rahul Verma Oct 03 '17 at 19:02
  • @batMan then say it explicitly. – Jean-Baptiste Yunès Oct 03 '17 at 19:03
  • This question was closed as a duplicate, but I don't think that the linked question (what in general does `const` mean?) applies here. I think this is a specific misunderstanding about `const` and how it interacts with streams, with the nuance being "`const` means the value can never change after initialization" versus "`const` means that the value can never change after it's first assigned." – templatetypedef Oct 03 '17 at 19:04
  • @templatetypedef: I was the closer (as you probably knew). I think the answers in the other thread are the same as for this one; you cannot modify a variable marked as `const` (without a `const_cast`). You're right, though, what OP wants here is to simultaneously declare and initialize a `const` variable from a stream, which is not possible. I'm cool with keeping it open. – AndyG Oct 03 '17 at 19:09

5 Answers5

7

If you define the variable as

const int quantity;

you're saying "I'd like an int called quantity, and under no circumstances do I want its value to ever change." As a result, if you then write

cin >> quantity;

the compiler says something to the effect of "wait - you want me to change the value of quantity by replacing it with whatever the user entered, but earlier you said that you never wanted me to change it!"

My sense is that you wanted to make it so that after you give an initial value to quantity that value never changes, but with const variables that initial value needs to be set when the variable is created. You could therefore try something like this:

const int quantity = readValue();

for some function readValue() that reads and returns an int value. That way, the compiler sees that quantity is given a fixed value, it knows that the value never changes, and you never try to directly cin into the value of quantity.

For a more technical perspective on the errors you got: when the compiler read

const int quantity;

without any value assigned to it, it reported an error because it's unusual to create a constant without giving it a value. (I can see from your code that you meant to give it a value, but the way you did it wasn't legal and the compiler didn't piece the two things together). The second error about operator >> resulted because none of the different ways that you can read something from cin (read a string, read an int, read a char, etc.) applied, since each of them assumed that they can get a mutable (modifiable) view of the value in question. Again, both of these issues stem from the fact that the compiler saw your code as two separate independent errors rather than one large "oops, that's not how const works" error.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
2

You can initialize a const local variable only once, at the moment it's declared. Your example looks like it couldn't possibly work, but it's simple if you add a level of indirection.

int ReadAnInt()
{
    int temp;
    cin >> temp;
    return temp;
}

const int quantity = ReadAnInt();
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • 2
    This problem of constant input is, I think, one of the bigger problems with the iostreams design. – Daniel H Oct 03 '17 at 18:59
  • @DanielH There are easy workarounds though: `const int a = *std::istream_iterator(std::cin);` Horrible, yes. Impossible without defining another function, no. – Rakete1111 Oct 03 '17 at 19:12
  • @Rakete1111 I didn’t say it was impossible, but if that’s the best workaround available it is still a problem with the API. – Daniel H Oct 03 '17 at 20:34
0

The const qualifier means that the variable is immutable and you cannot change its value. The first error is telling you that the variable is uninitialize.

cin allows you to assign a value to a variable, which immediately contradicts your const qualifier.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
0

You must initialise quantity the moment you declare it. Furthermore, you can't assign a value to it later on; after all, it is constant.

frslm
  • 2,969
  • 3
  • 13
  • 26
0

First you put the same code in both of the boxes. I admit you put the const symbol in the first source. Second a const must be initialized at declaration in C++. Thus you should put the code as following

 int value;
 cin >> value;
 const int my_num = value;
Brighter side
  • 392
  • 2
  • 14