Notes Sep 30

chars.cpp

// chars.cpp

#include <iostream>
using namespace std;
int main()
{
    // Arithmetic with chars
    char c = '#';
    c = c * 2;
    cout << c << endl;
    c = c + 32;
    cout << c << endl;
    // Can subtract chars
    int x = 'E' - 'A';
    cout << x << endl;
    // Useful for converting
    // numeric character to corresponding
    // number.
    char k = '8';
    int z = k - '0';
    cout << z << endl;

    return 0;
}

strs.cpp

// strs.cpp

#include <iostream>
using namespace std;
int main()
{
    char initial = 'C';
    string me = "CL";
    cout << me << endl;
    cout << me[1] << endl;
    int lastInitial = me[1];
    cout << lastInitial << endl;
    me[1] = '!';
    cout << me << endl;
    return 0;
}

strinput.cpp

//strinput.cpp

#include <iostream>
using namespace std;
int main()
{
    // This version has a problem when
    // the name contains a space. It
    // stops reading when it hits a space.
    // And then keeps the rest of the input
    // for the next cin.
    string name;
    cout << "Enter your name: ";
    cin >> name;
    cout << "Hello, " << name << "!" << endl;
    cout << "What is your favorite food? ";
    string food;
    cin >> food;
    cout << "I like " << food << " too." << endl;
    return 0;
}

strinput2.cpp

//strinput2.cpp

#include <iostream>
using namespace std;
int main()
{
    string name;
    cout << "Enter your name: ";
    getline(cin, name);
    if(name == "Chris"
       || name == "chris"
       || name == "Sara"
        )
    {
        cout << "Welcome to the program." << endl;
        cout << "You can have ANY food you want!" << endl;
    }
    else {
        cout << "Hello, " << name << "!" << endl;
        cout << "What is your favorite food? ";
        string food;
        getline(cin, food);
        cout << "I like " << food << " too." << endl;
    }
    return 0;
}

ifdemo.cpp

//ifdemo.cpp
#include <iostream>
using namespace std;
int main()
{
    int birthYear;
    cout << "What year were you born? ";
    cin >> birthYear;
    int age = 2015 - birthYear;
    cout << "You are " << age << endl;
    if(age > 35) {
        cout << "You are OLD!!!!!" << endl;
    }
    else {
        cout << "You are young." << endl;
    }
    cout << "Thanks for playing" << endl;
    return 0;
}