Monday, November 19, 2012

CPP 04 - Taking User's Input



Let's modify our previous program converting feet to meters by allowing a user to pick any number they want.

Pic. 4.1 - Feet to Meter Converter with User's Input.

Compile and run the code. It should give you something similar to mine:

Pic. 4.2 - Code Results.

Explanation

Take a look at the line 17 in the code above. It contains the keyword 'cin >>' followed by the variable name. This command stops the program until user provides the number. It then, is stored in the variable number_of_feet. The rest should be self explanatory.

NOTICE!
The keyword 'cin' stands for: console input (from your keyboard). It is followed by '>>' and the variable name (kind of like saying send it to ...).
The keyword 'cout' stands for: console output (to your screen). It is followed by '<<' (kind of like saying whatever follows send to the screen).

Homework

Exercise 4.1
Write a program that converts the meters to feet. User should be prompted for the number of meters to convert.