Notes week of Oct 20

Muddiest points & want to learn

Challenge HW 3.8.3: Using find()

There was some question about this. You want to do userInput.find("darn") in the if condition; however, it is finicky. Here is one way in which it does work – but you have to store the result of the find in an integer variable. It returns -1 if not found, so then you test >= 0 in the if:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string userInput;
    //userInput = "That darn cat.";
    userInput = "Dang, that was scary!";
    int r = userInput.find("darn");
    cout << "find returned " << r << endl;
    if(r >= 0) {
        cout << "Censored" << endl;
    }
    else {
        cout << userInput << endl;
    }
    return 0;
}

Challenge HW 3.9.2: Name song

This program uses erase() and begin(), which were not previously covered. However, the line including both of those is given to you, and the comment says what it does: “remove first char from userName.”

So then all you need to do is substitute the userName for the "(name)" in the verse. Here was my solution:

string name = "(Name)";
int i = secondVerse.find(name);
secondVerse.replace(i, name.length(), userName);

I also covered some other ways that erase() can be used to remove characters from a string:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s = "Hello world";
    cout << "BEFORE: " << s << endl;
    //s.erase(0, 4);    // o world
    //s.erase(4, 2);    // Hellworld
    //s.erase(4);       // Hell
    s.erase(s.begin()); // ello world
    cout << "AFTER: " << s << endl;
    return 0;
}

Introduction to loops

This ‘countdown’ program illustrates a while loop. For variety, we added some calculations and a switch within the loop.

#include <iostream>
using namespace std;
int main()
{
    int start = 15;
    while(start > 0) {
        switch(start) {
            case 5: cout << "five" << endl; break;
            case 4: cout << "four" << endl; break;
            case 3: cout << "three" << endl; break;
            case 2: cout << "two" << endl; break;
            case 1: cout << "one" << endl; break;
            default: cout << start
                        << "  (" << start*start
                        << ")" << endl;
        }
        start--;
    }
    cout << "Blast off!";
    return 0;
}

Solution to Project 5

Here is my solution to Project 5, that I demonstrated in class. If you haven’t submitted one yet, your solution should be sufficiently different. I recommend not looking until after you submit!

#include <iostream>
using namespace std;
int main()
{
    int hour;
    cout << "Enter hour (0-23): ";
    cin >> hour;
    if(hour > 23 || hour < 0) {
        cout << "Error: hour out of range!";
        return __LINE__;
    }

    int minute;
    cout << "Enter minute (0-59): ";
    cin >> minute;
    if(minute > 59 || minute < 0) {
        cout << "Error: minute out of range!";
        return __LINE__;
    }

    // Print out time as given
    if(hour < 10) {
        cout << "0";
    }
    cout << hour << ":";
    if(minute < 10) {
        cout << "0";
    }
    cout << minute << " is ";

    // Print 12-hour time
    if(hour == 0) {
        cout << 12;
    }
    else if(hour > 12) {
        cout << hour-12;
    }
    else { // hour must be in range 1 to 12
        cout << hour;
    }
    cout << ":";
    if(minute < 10) {
        cout << "0";
    }
    cout << minute << " ";

    // print AM/PM
    if(hour >= 12) {
        cout << "PM";
    }
    else {
        cout << "AM";
    }
    cout << endl;

    // Print the approx minutes (quarter past, half past)
    if(minute >= 9 && minute <= 23) { // your choice
        cout << "quarter past";
    } else if(minute >= 24 && minute <= 38) {
        cout << "half past";
    } else if(minute >= 39 && minute <= 53) {
        cout << "quarter til";
        hour = hour + 1;
    }
    cout << " ";

    // Print the hour in English
    switch(hour) {
    case 1:  case 13: cout << "one";    break;
    case 2:  case 14: cout << "two";    break;
    case 3:  case 15: cout << "three";  break;
    case 4:  case 16: cout << "four";   break;
    case 5:  case 17: cout << "five";   break;
    case 6:  case 18: cout << "six";    break;
    case 7:  case 19: cout << "seven";  break;
    case 8:  case 20: cout << "eight";  break;
    case 9:  case 21: cout << "nine";   break;
    case 10: case 22: cout << "ten";    break;
    case 11: case 23: cout << "eleven"; break;
    case 12: cout << "noon";   break;
    case  0: case 24: cout << "midnight"; break;
    }

    // morning, afternoon, or evening
    if(!(hour == 0 || hour == 12 || hour == 24)) {
        cout << " in the ";
        if(hour > 0 && hour < 12) {
            cout << "morning";
        }
        else if(hour > 12 && hour < 18) {
            cout << "afternoon";
        }
        else if(hour > 18) {
            cout << "evening";
        }
    }
    cout << endl;

    return 0;
}