• char data type
    • This is a character.
    • The reserved word char
    • The domain is a single alphanumeric character
      • A letter: 'a', 'b', 'X', 'Y', 'Z'
      • Or a digit: '0', '1', ... '9'
      • Or a special character: '+', '-', ..
      • Anything you can type on the keyboard
    • Operations
      • You can add, subtract, multiply, divide two characters, but don't do that yet.
      • You can compare characters, but more on that later.
      • Output: cout << 'x';
      • Input: cin >> charVar
        • This will skip white space
        • then read in the next non white space character and store it in the variable.
    • We think of characters as enclosed in single quotes
      • This is a misnomer but frequently used
      • We actually mean apostrophes
  • Variables
    • Variables are a place a programmer can store data.
    • They are declared (see below) by a programmer
    • They are associated with an identifier
      • A name
      • How you get access to them
      • You need to worry about this.
    • They are associated with a location in memory
      • This is where the data is actually stored.
      • You don't need to worry about this (yet)
    • You grab a variable when you need one.
      • Each should serve a unique purpose.
      • But can be reused when it makes sense.
      • This is tough, pay attention to how it is done.
  • Declarations
    • In the previous set of notes, we declared main, a function
    • We would also like to declare variables and constants
    • In C/C++ we must declare something before we use it.
    • For a variable
      • The declaration takes place inside of the code block for the routine in which it will be used.
      • It is at the top of the code block.
      • It is in one of the following forms
        • datatype identifier;
        • datatype identifier, identifier ...;
        • datatype identifier=value;
        • datatype identifier = value, identifier, identifier=value... ;
      • Some Comments
        • I like one variable declaration per line
          int age,
              shoeSize, 
              speedLimit;
          		 
        • I don't like comments following variables
          int age,         // the age
              shoeSize,    // the shoe size
              speedLimit;  // the speed limit
          		 
        • I do like initializing variables at declaration
          int age = 0,
              shoeSize = 5,
              speedLimit = 70;
          		 
        • I would throw a fit at this however
          int age = 0, shoeSize = 5, speedLimit = 70;
          		 
    • Other declaration sytax to follow.
  • Syntax and Naming Conventions for identifiers
    • Syntax:
      • Identifiers must start with either the underscore _ or a letter
      • They may contain only letters, digits or the underscore
      • They may not be a reserved word.
    • Are the following valid/invalid identifiers? Why/Why not?
      • bob
      • 4bobs
      • _A_WORD_
      • c++
      • b4TheLoop
      • char
    • We have conventions for constructing variable identifiers
      • Think speed limit, not speed of light
      • Start with a lower case letter
      • Each new word starts with an upper case letter
      • The identifier should have some meaning.
    • Which of the following follow the convention? Why/Why not?
      • aBigWord
      • TAX_RATE
      • TheFirstInitial
      • cars4Sale
      • x4b9x
      • a
  • A simple program.
    • Write a program which reads in a users three initials and prints them out.
    • Algorithm:
      Prompt the user for the first initial
      Read in the first initial
      Prompt for the second initial
      Read the second initial
      Prompt for the third initial
      Read the third initial
      Print some blank lines
      Print the monogram
      	 
    • Questions:
      • Will there be any users that don't have three names?
      • Will there be any users with names with letters that are not part of the ASCII character set?
    • One version of the program.
  • string data type
    • Range
      • Strings are a collection of characters
      • Think enclosed in double quotes
        • This is a misnomer
        • They are actually inside quotation marks.
      • Examples: "hello" , "Hello World", "3245", "a", ""
        • By placing a single character inside double quotes, it becomes a string and not a character.
        • Two quotation marks next to each other make an empty string
      • An unterminated string will cause a syntax error
      • Strings may be longer than a single line
      • But may contain a new line character (\n)
    • Operations
      • Output: as with characters
      • Input: cin >> stringVar
        • Skip all whitespace
        • Read in nonwhitespace characters and add them to the string
        • Stop when a whitespace character is encountered
      • Input: getline(stream, string)
        • cin is the only stream you know.
        • Read all characters into the string until a newline is encountered.
      • String Concatenation: string1 + string2
        • Appends the contents of string2 to string 1
        • And returns a string
        • We will use this in assignment statements in a bit.
      • Comparison, searching and other functions to come.
  • Constants
    • Constants are another user declared object.
    • They represent a value which does not change in the program.
    • In general we use constants to
      • Make the program more readable
      • Make the program easy to modify.
    • It is easy to get the use of constants confused with material presented in chapter 2, so watch how they are used in example program.
    • Declarations
      • const datatype identifier = constant expression
      • The datatype (int, string)
      • constant identifier conventions
        • Must have meaning
        • ALL_UPPER_CASE
        • With the underscore between words.
      • const string DAY = "Monday";
    • Constants are declared before the main routine.
      • After using namespace std;
      • Before int main() {
  • Expressions
    • A constant: 8
    • A variable: shoeSize
    • A computation: shoeSize*2 + age
  • Assignment Statements
    • Copy the value from the right hand side to the left hand side
    • variable = expression
    • The expression is evaluated and the value is stored into the variable.
  • A program
    • Description: Write a program that will print an invitation to a birthday part. Read in the guest's name and address then print the letter. Assume that you will reuse the program for different people at different ages, so make this easy to change.
    • Algorithm:
      Prompt for name
      Prompt for Street address 
      Prompt for City/State/Zip
      
      Print letter
      	
    • The Age, Name, and Date of the birthday celebrant will change over the years
      • You probably don't want to enter this data every time you run the program.
      • these should be constants.
    • Use getline as there may be spaces in the guest's name.
    • One version of the code is here.