< Previous | Contents | Next >
Another way to assign a value to a variable is through user input. So next, I assign the value of a new variable, fuel, based on what the user enters. To do so I use the following line:
cin >> fuel;
Just like cout, cin is an object defined in iostream, which lives in the std namespace. To store a value in the variable, I use cin followed by >> (the extraction operator), followed by the variable name. You can use cin and the extraction operator to get user input into variables of other fundamental types too. To prove that everything worked, I display fuel to the user.