MCS273 Introduction to C++ Programming (January 2013)

A straight-line program to do date format conversion


Overview

In this brief lab you will write a straight-line program to perform date format conversion. Straight-line programs have no flow-control: no conditional statements, loops or recursive function calls. (In fact, you won't see flow control until Chapter 4.) Straight-line programs can make for instructive and challenging puzzles, even though people seldom write them.

Date conversion (2 points)

  1. Create a new project called Date and then write and compile a straight-line program there to perform date format conversions. I'll explain by example. The user will type input in the form 10/05/98 and the computer should output, say, Oct 05, 1998. (See Exercise P2.23 on page 70 for a hint. It's easiest if your program first reads in the month, 10, as an integer, then reads in the rest of the line /05/98, as a string. You should only use stuff you have read about in Chapters 1 and 2 in the book.)

    Be sure that all variable names are carefully chosen, and that the program is indented properly.

    Be sure your program compiles and runs. Perform several tests of the program before getting checked off.
  2. Check-off: Leave the window with your tests open on your screen, as well as the program file.

Improving the program (optional)

You are encouraged to work with a neighbor on this part. If you do work with someone else, you'll be checked off together; you needn't each have your own copy of a working program.

To do this assignment, you'll need a way to convert digits to integers: If str is a string, the expression (str[4] - '0') returns an integer whose value is the digit in position 4 of the string. (Recall that the first position is 0, so this converts the 5th character in str to an integer.)

For example, the following program prints the number 400 followed by a newline:

  #include <iostream>
  #include <string> 
  using namespace std;
  
  int main() {
     string str = "Pi=3.14159";
     cout << 100*(str[6] - '0') << endl;
     return 0;
  }
Improve your program by making the following improvements. Be sure to save your last properly working copy in a backup file before you take on each new challenge. This way, if you get stuck, at least you can go back to something that works.

  1. Improve the format so that a leading zero in front of the date is removed. I.e.,
    • 10/05/98 gets displayed as Oct 5, 1998
    • 10/25/98 gets displayed as Oct 25, 1998
  2. Improve the format so that a leading zero in front of the date is removed. I.e.,
    • 12/18/23 gets displayed as Dec 18, 2023
    • 12/18/50 gets displayed as Dec 18, 1950
  3. Check-off: Leave the window with your tests open on your screen, as well as the Emacs file containing your improved program.
  4. (optional challenge) Make it so that the months are written unabbreviated: December 18, 2023. The challenge here is that there should not be additional leading or trailing spaces, so months use up a variable number of characters depending on how long it is written out in English.