Welcome to Lesson 6!


Learning Objetives

By the end of today's class, you should know...
  • What is ASCII?
  • What is a string?
  • What is the difference between a char and a string?
  • How do you declare a string variable and assign it a value?
  • How do you output a string to the console?
  • How do you read in a string from the console and store it in a variable?
  • What is the problem you may run into when using cin with strings?
  • What is string concatenation?

Announcements

  • Quiz 2 after our break!
  • Lab 3 due Friday!

Review of Assigning Variables

With a partner, determine what gets printed to the console in parts 1 and 2:

Part 1:

//assume below statement are part of a main function

int oranges = 12;
int apples = 4;
oranges = apples;
int numFruit = apples + oranges;
apples = apples - 2;
cout << "Apples: " << apples << endl;
cout << "Oranges: " << oranges << endl;
cout << "Total Fruit: " << numFruit << endl;

Part 2:

1. cout << 7 / 2.0;

2. cout << 7 / 2;

3. cout << 7 % 2;

4. int result = 7.0 / 2.0;

   cout << result;

5. cout << 9 + 3 * 5 / 2 - 3;

Part 3:

  • Write a complete user interaction (3 lines of code) to prompt a user to enter a weight, read in the weight from the user and store the result in a variable.


Review of Char

  • In addition to numbers, computers can manipulate text and other non-numerical types
  • Values of type char (short for character) are a single letter, number or special symbol
  • You specify a character by enclosing it in single quotes (')
    • The quote marks are not part of the data
  • For example:
    'a'   'b'   'Z'   '3'   'q'   '$'   '*'
  • When you use a char data type, you store the character using an ASCII code
  • ASCII is a coding method that assigns a number to every character
  • You can see the codes in an ASCII Table


Char Variables - Declaring and Assigning

  • As with other data types, you must declare char variables before use:
    char letter;
  • You assign values to a char variable using the equals sign:
    letter = 'A';
  • Just like numerical types, you can combine declaration and assignment into one statement:
    char letter = 'A';
  • Also like numerical types, you can declare multiple variables on one line:
    char letter, letterA = 'A', letterB = 'B';


User I/O with Type char

  • Like numbers, you can output type char using cout <<
    char letter = 'A';
    cout << letter << 'B' << endl;
    
  • Also, you can input type char using cin >>
    cin >> letter;
    cout << letter  << endl;
    


Activity 6.1: My Initials (10 pts)

  • Here is an ASCII conversion chart (also depicted below). Each letter of the alphabet and all symbols on your keyboard (and some that aren't) are represented as numerical values in ASCII.

ascii

  • Let's test the ASCII conversion codes and make sure that they really do work, by writing a program to print out your name.
  • Open a new C++ source file named initials.cpp
  • Copy and paste the example code below into your program.
  • Alter the values for your own initials.
/**
* Jennifer Parrish
* CIS 22A
* M/W 9:30-11:20am
*/

#include<iostream>
using namespace std;

int main(){
    char first = 74;
    char second = 90;
    char third = 80;

    cout << "My initials are: " << first << second << third << endl;

    int initial1 = 'J';
    int initial2 = 'Z';
    int initial3 = 'P';

    cout << "The ASCII values of my initials are:\n";
    cout << initial1 << endl << initial2 << endl << initial3 << endl;
    return 0;
}
 

  • The take away: You can use ASCII numbers and chars interchangeably.
  • Also try the reverse: declare 3 integer variables and assign your initials as chars to each of the variables as in the example above.
  • Will your code compile? What happens when you run it?
  • When you are finished upload your initials.cpp file to Canvas.


Introduction to Strings

  • In addition to single characters, computers can work with text strings
  • For example, in the following the characters between the double quotes are displayed as text:
    cout << "Hello World!" << endl;
  • Programmers refer to text like this as a string because it is composed of a sequence of characters that we string together
  • C++ provides the string type so we can work with text
  • To work with the string type we may need to include the string library:
    #include <string>
    using namespace std;
    
  • Strings are enclosed in double quotes, which are not part of the string
  • For example:
    "Hello"  "b"  "3.14159"  "$3.95"  "My name is Fred"
  • Notice that the string "3.14159" could be expressed as a double by removing the quotes
  • However, a computer stores these two values very differently and we must use them in different ways
  • For instance, we cannot multiply the "3.14159" by 2, but we can when it is expressed as a double:
    cout << "3.14159" * 2; // NO!
    cout << 3.14159 * 2; // allowed 


String Variables and Simple I/O

  • We declare and assign values to string variables like numeric types
  • For example:
    string firstName;             // declaration
    firstName = "Jennifer";       // assignment
    string lastName = "Parrish";  // declaration + assignment
    cout << firstName << " " << lastName << endl;
    

Simple I/O with Strings

  • Like numbers, you can output type string using cout <<
  • Also, you can input a string using cin >> stringName
  • For example:
    string name;
    cout << "Enter your name: ";
    cin >> name;
    cout << "You entered: " << name  << endl;
    
  • The cin statement assigns the user input to the string variable name
  • Note that only a single word can be entered using cin >> name
  • This is because cin >> name works as follows:
    1. Skips whitespace
    2. Reads non-whitespace characters into the variable
    3. Stops reading when whitespace is found


Joining Strings (Concatenation)

  • You can join two strings together using the '+' operator
  • The join operation is called concatenation
  • For example:
    string s1 = "Hello", s2 = "World!";
    string s3 = s1 + s2;
    cout << s3 << endl;
    
  • The string s3 now has the contents of both s1 and s2
  • You can also mix string variables and literal strings:
    string s1 = "Hello", s2 = "World!";
    string s3 = s1 + ", " + s2;
    cout << s3 << endl;
    
  • One or both strings surrounding the + must be a string variable
  • For instance, the following will NOT work:
    string greeting = "Hello" + " " + "World!"; // No!
    
  • However, this is not usually a problem because we can just make one long literal string:
    string greeting = "Hello World!";
    
  • In addition, we can concatenate strings and characters:
    char letter = 'A';
    string s1 = "BC";
    s1 = letter + s1 + 'D';

Activity 6.2: My Name (10 pts)

  • Find a partner for pair programming, then open a new C++ project in Eclipse called Name
  • Save the file as name.cpp
  • We will create a program to greet the user by name.
  • Add a block comment at the top of your program with your name and section information.
/*
* Name of partner 1
* Name of partner 2
* Section info
*/
  • Now add a statement to include the iostream library.
  • Don't forget to use the standard namespace.
  • Create your main function and leave extra space to write your program statements.
  • Your program should now look like this:


  • At the top of the main function, let's declare our first string variable - to store the first name of our user:
string first_name;
  • Declare a second string variable to store the last name of our user in the same _ style.
  • Declare a third string variable to store the user's full name:
string full_name;
  • Welcome the user with a cout statement like this one:
cout << "Hi! I want to learn your name!" << endl;
  • Now, prompt the user to enter their first name.
cout << "Please enter your first name: ";
  • Follow this prompt with a cin statement to capture the user input.
cin >> first_name;
  • Now prompt the user to enter his or her last name and store the input in the last_name variable.
  • We want to assign the full_name variable the value of the user's first and last name combined. Let's do so now using concatenation:
full_name = first_name + " " + last_name;
  • Finally, let's greet the user by his or her full name.
cout << "Nice to meet you " << full_name << "!" << endl;

  • Run your program to ensure it is giving you the output you expect.
  • When you are finished upload your name.cpp file to Canvas


Wrap up

With a partner, answer the following questions:
  • What will the following statements output to the console:
char letter1 = 67;
char letter2 = 111;
char letter3 = 108;
char punctuation = 33;
cout << letter1 << letter2 << letter2 << letter3 << punctuation;
  • Is the following statement legal? Why or why not?
cout << "39" + 2;

Now, alter the above statement to make it legal (two possible changes you could make).
  • Using string concatenation and the string variables below, declare a fourth string variable that contains the following: Thank you for your order, Shinshin! (Note: pay careful attention to the two punctuation marks in the previous sentence)

string grateful = "Thank you for your order";

string name = "Shinshin";

string punctuation = "!";


Assignment 6 due Tuesday at 9:20am
Lab 3 due Friday at Midnight
~Have a Great Weekend!~