Notes for Oct 14

alpha-order.cpp

Some experiments in string comparisons – which use ASCII order, not exactly alphabetical order.

// alpha-order.cpp
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    string s1, s2, l1, l2;
    cout << "Enter one word: ";
    getline(cin, s1);
    cout << "Enter another word: ";
    getline(cin, s2);

    l1 = s1;
    l2 = s2; // lower-case versions
    transform(l1.begin(), l1.end(),
              l1.begin(), ::tolower);
    transform(l2.begin(), l2.end(),
              l2.begin(), ::tolower);

    cout << "Alphabetical order:" << endl;
    if(l1 < l2) // compare the lower-case versions
    {
        cout << s1 << endl << s2 << endl;
    }
    else
    {
        cout << s2 << endl << s1 << endl;
    }

    return 0;
}

range-check.cpp

Range-checking using Booleans. Remember DeMorgan’s Laws! The opposite of > is <=.

#include <iostream>
using namespace std;
int main()
{
    int number;
    cout << "Enter a number (1-10): ";
    cin >> number;
    if(number >= 1 && number <= 10) {
        cout << "GOOD" << endl;
    }
    else {
        cout << "BAD\n";
    }
    return 0;
}