Variables and input

A variable is a container inside the computer that can contain a value such as

Four definitions:

  1. variable1.C: define, declare, and initialize a variable all in one statement. Then output the contents of the variable.
    What goes wrong if you try to output the content of the variable (i.e., the number 10) this way?
    	cout << "The variable contains price.\n";
    
  2. variable2.C: Assign a new value to the variable.
    That’s why it’s called a “variable”—its content can vary.
    Information always travels from right to left across an equal sign.
  3. variable3.C: Old-fashioned (pre-2011) way to define a variable.
  4. variable4.C: What’s the biggest number this type of variable can hold?
    maxInt is “camel case”, EXIT_SUCCESS and numeric_limits are “snake case”.
    Note that the number 2,147,483,647 isn’t arbitrary:
    231 = 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 = 2,147,483,648
    (pronounced “2 billion, 147 million, 483 thousand, and 648”).
    Check this with bc, the Linux “binary calculator”. The keystroke control-d tells bc that you’re not planning to type in any more arithmetic problems for it.
    jsmith@storm:~$ bc -l
    2^31
    2147483648
    control-d
    jsmith@storm:~$ 
    
    See Why do the possible values for an int go from -2,147,483,648 to +2,147,483,647?
  5. variable5.C: input a value from the outside world and store it into a variable. Check that the input was successful.
    jsmith@storm:~$ cd
    jsmith@storm:~$ pwd
    jsmith@storm:~$ ls -l
    
    jsmith@storm:~$ wget https://markmeretzky.com/fordham/1600/src/variable/variable5.C
    jsmith@storm:~$ c++ variable5.C
    jsmith@storm:~$ ls -l
    
    jsmith@storm:~$ ./a.out
    Please type the price and press RETURN.
    13
    The variable contains 13.
    jsmith@storm:~$ echo $?
    0                  (EXIT_SUCCESS stands for this number.)
    
    jsmith@storm:~$ ./a.out
    Please type the price and press RETURN.
    hello
    Sorry, that wasn't an acceptable number.
    jsmith@storm:~$ echo $?
    1                  (EXIT_FAILURE stands for this number.)
    
    Exercise. What happens if you try to input an out-of-bounds value such as 2147483648 or -2147483649 into the variable? What is the invisible exit status number?
  6. Feed input into the C++ program (variable5.C) from an input file (thirteen.txt), instead of from the keyboard. First create a very small file containing the two digits 13 and a newline, a total of just three characters. We could create this little file with vi, but it’s easier to create it with echo.
    jsmith@storm:~$ cd
    jsmith@storm:~$ pwd
    jsmith@storm:~$ ls -l
    
    jsmith@storm:~$ echo 13        (echo is the Linux "print" command.)
    13
    
    jsmith@storm:~$ echo 13 > thirteen.txt
    jsmith@storm:~$ ls -l          (There should be a new file named "thirteen.txt".)
    
    jsmith@storm:~$ cat thirteen.txt
    13
    
    jsmith@storm:~$ ./a.out < thirteen.txt
    Please type the price and press RETURN.
    The variable contains 13.
    
    jsmith@storm:~$ ./a.out < thirteen.txt > variable5.txt
    jsmith@storm:~$ ls -l
    
    jsmith@storm:~$ cat variable5.txt
    Please type the price and press RETURN.
    The variable contains 13.