Lab 6: Mines Dating Diary
Concepts
Focus on two main concepts in this assignment: properly designing a main() subroutine that calls other well-named functions; and implementing reusable functions.
Basic Program Design
While functions provide modularity, reuse and encapsulation, they also provide us a mechanism for creating programs that are more readable. Consider the following code:
The code is correct. But does it read well? What if there was a function called "prompt" that displayed a message to the screen?
Now it's a little more readable. Simple, yes? Now consider the following.
cout << "Enter an integer: ";
cin >> breakups;
The code is correct, and accomplishes capturing user input. But think for a moment. Every time you wish to capture user input, what do you do? You declare a variable. You write an input prompt. You write a cin statement. Can't we simplify these tasks by encapsulating steps in a function?
breakups = captureInt("Enter an integer: ");
Assuming that a function called captureInt exists and it prompts the user, captures what they type and returns an int, then we can trust that the variable breakups will be assigned the value returned by captureInt.
Consider a more complex interaction, where we verify a user enters a number within a range. You might implement something like this:
while (breakups < 1 || breakups > 100) {
cout << "Enter an integer (1 to 100): ";
cin >> breakups;
}
Wouldn't it be great if there was a function that handled this for you?
breakups = captureInt("Enter an integer", 1, 100);
Notice how these functions "hide" the work that they need to do, freeing us to think about more important things. As long as captureInt does it's job, we can trust that breakups will be assigned a value between 1 and 100, inclusively.
ABCUF
Always Be Using Functions. Let that be a rule of thumb for the remainder of the semester. When working on your programs, try to reach an end result where main doesn't do much "low level stuff" but rather leverages functions in order to do what your program needs to do.
For example, if you were to write a pizza-making program, don't write one super-long, hard-to-read, scare-your-date-away implementation of main. Use functions:
createShoppingList();
buyIngredients();
gatherIngredientsInKitchen();
pourYourselfGlassOfFineChianti();
makeDough();
makaDaSauca();
//...
cookPizza();
return 0;
}
Instructions
We've heard that dating is such a major activity (right?) here at Mines it seems that many folks might be overwhelmed by keeping track of their dating history. What was that person's name? What was his/her favorite food? What did I think of them?
Your goal is to write a program that allows the user to enter in some basic information about a person and writes the data to an external file. Take a look at a typical interaction:
Date of date: 9/13/2011
Date's name: Uh, I never asked.
Date's age (18 to 25): 36
Date's age (18 to 25): 21
Date's favorite food: Beer
Rate your date (1 to 5): 2
Your date has been recorded.
Notice how the user was prompted twice for the age since the user typed a number outside the specified range.
When the user sees the line "Your date has been recorded," we should assume that their date information has been saved to an external file called dates.txt. The contents of the file should look like this:
Name: Pat
Age: 23
Favorite Food: Il Pastaio Spaghetti and Meatballs
Rating: 5
Date: 9/13/2011
Name: Uh, I never asked.
Age: 21
Favorite Food: Beer
Rating: 2
For this assignment, you must use functions to capture strings and integers, and you must use a function that takes care of saving the captured data to a file. Be sure to note the requirements below, under "Requirements and Rubtic" for more information.
Hints
General Design Tips
Focus on one feature at a time.
Our solution uses constants inside main for a FILENAME string, and four ints (MIN_AGE, MAX_AGE, MIN_RATING and MAX_RATING).
Our main routine merely declares variables and uses functions to assign values to those variables. It never explicitly uses cin or cout directly. Our main also does not explicitly open a file for saving -- it uses a function to do so. Our main implementation makes two calls to prompt, three calls to captureString, two calls to captureInt and one call to save. All of these functions are described below.
Our solution includes the definition of four functions: one for simply writing a message to the screen, another for capturing integers within a range, another for capturing strings, and another for saving information to a file.
An Important Tip about your captureInt function
Due to the way the >> and cin behaves, you need to do one specific thing in your captureInt implementation. Before your return statement, be sure to call cin.ignore().
return answer;
}
An Important Tip about your captureString function
Remember our brief fling with getline() a few assignments ago? Now that you understand the idea of functions, we'll be using getline() a lot more. Remember, when capturing a string from user input, you need to use getline(). Here's an example of using getline() to capture a string from the console.
getline(cin, input);
How to append new data to the output file, rather than always overwriting the file
This is a dating diary, so every time we go on a date and then run the program, we want the new information to be appended to the file rather than always overwriting the file. To do this, your save function should probably start with the following lines:
ofstream file;
file.open(filename.c_str(), ios_base::app);
Your file will always be overwritten if you do this:
ofstream file(filename.c_str());
You definitely don't want to lose your precious dating data, so be sure to open the file as shown in green above.
Requirements and Rubric
Your program must save the date data to a file called dates.txt.
Your program must include the following function implementations:
- A function called
promptthat returns "nothing," and accepts onestringparameter. It causes the string to be displayed on the screen. - A function called
captureIntthat returns anintand accepts three parameters: onestring, and twoints. It should display the string parameter's value as a prompt on the screen, capture the user's integer input, and return the user's input. It should reprompt the user for input as long as the number the user enters is not between the twointparameters. - A function called
captureStringthat returns astringand accepts onestringparameter. It should display the string parameter's value as a prompt, capture the user's input, and return the user's input. - A function called
savethat returns "nothing" and accepts six parameters:
- a
stringparameter calledfilename - a
stringparameter calleddate - a
stringparameter calledname - an
intparameter calledage - a
stringparamter calledfavoriteFood - an
intparameter calledrating
filename) and write the values of each of the parameters to the file.
It must not overwrite existing data in the file (see the hint above), and the data written should be properly formatted (as described in "Instructions" above). - a
Your main routine should only declare variables and use functions to assign those variables values; call the save function; and return 0.
A friendly message from The Terminator, our grading program
*bzzzt* Hasta la vista baby! Head to the choppah! It's not a tum *bzzt* Excuse me, malfunction.
I will check for the following:
Your program must terminate after capturing information for one date and saving the data to the output file.
Your program must save the date data to a file called dates.txt.
Your program must capture only five criteria: the date of the date, your date's name, age, favorite food, and rating.
The function names you use must match the specifications above.
I'll be *bzzzt* I'll be *bzzt* I'll be *bzzt* back!
This work is worth 120 points.
| Requirement | Points | Notes |
|---|---|---|
| Place your name in the comment header in main.cpp | 5 | |
| Correct submission of src directory as a .zip file. | 5 | |
| Brief comment above each function definition. | 20 | |
Correct implementation of prompt. |
10 | |
Correct implementation of captureInt. |
20 | |
Correct implementation of captureString. |
10 | |
Correct implementation of save. |
20 | |
| Correctly appends data to output file. | 5 | |
Date data saved in file dates.txt. |
10 | |
| Proper style and formatting | 10 | you read the style guide, yes? |
main leverages functions correctly |
5 |
Concepts Exercised: functions, abstraction, readability, style, declaring facts, repeating tasks, program design, fun, I/O, using libraries.
© 2011 Yong Joseph Bakos.