Spring 2025 CS 31

Programming Assignment 2
It's Tariffic!

Time due: 11:00 PM Thursday, April 17

Before you ask a question about this specification, see if it has already been addressed in the spec or in the Project 2 FAQ. And read the FAQ before you turn in this project, to be sure you didn't misinterpret anything.

(Be sure you also do the homework accompanying this project.)

The mercurial King of Heard Island, Aptenodytes Patagonicus, has suddenly decided to protect the island's only domestic industry, guano production, by imposing tariffs on guano imports. The job of writing a program that determines the amount of import duty imposed on each shipment of guano has come to you.

Your program must accept as input the name of the country from which the guano is imported, the number of kilograms of guano, and the name of the importer. The output will tell how much import duty the importer will have to pay.

Here is an example of a dialog with the program (user input is shown here in boldface):

	Country of origin: Ireland
	Weight in kilograms: 23
	Importer: Mumble
	---
	The import duty for Mumble is 1349.6 lantern fish.

(Lantern fish are the island's currency unit.) According to the import duty schedule:

As an example, Mumble above would be assessed 57.6 lantern fish for the first 6 kilograms of guano, plus 1292 lantern fish for the next 17 kilograms (76 lantern fish per kilogram for the next 17 kilograms instead of 30.3, since the country of origin is Ireland), for a total of 1349.6 lantern fish.

Here's another example:

	Country of origin: Peru
	Weight in kilograms: 3.3
	Importer: Tux
	---
	The import duty for Tux is 31.7 lantern fish.

You can test your understanding of the duty schedule by experimenting with this import duty calculator we found at the Heard Island Trade Ministry's web site.

Your program must collect the information for one importer in the manner indicated by the examples, and then write to cout a line with three hyphens only (no spaces or other characters), followed by exactly one line in a format required below. Our grading tool will judge the correctness of your program by examining only the line following the line with three hyphens (and verifying that there are no additional lines after that line). That one line you write must be in one of the following four forms; the text must be identical to what is shown, except that italicized items are replaced as appropriate:

In the last case, importer must be the importer the user entered, and amount must be the correct duty amount, shown as a number with at least one digit to the left and exactly one digit to the right of the decimal point. The lines you write must not start with any spaces. If you are not a good speller or typist, or if English is not your first language, be especially careful about duplicating the messages exactly. Here are some foolish mistakes that may cause you to get very few points for correctness on this project, no matter how much time you put into it, because the mistake will cause your program to fail most or all of the test cases we run:

Your program must gather the country of origin, the weight in kilograms, and the importer in in that order. However, if you detect an error in an item, you do not have to request or get the remaining items if you don't want to; just be sure you write to cout the line of three hyphens, the required message and nothing more after that. If instead you choose to gather all input first before checking for errors, and you detect more than one error, then after writing the line of three hyphens, write only the error message for the earliest erroneous item.

You will not write any loops in this program. This means that each time you run the program, it handles only one importer. It also means that in the case of bad input, you must not keep prompting the user until you get something acceptable; our grading tool will not recognize that you're doing that.

A string with no characters in it is the empty string. A string with at least one character in it is not the empty string, even if the only characters in it are things like spaces or tabs. Although realistically it would be silly to have an importer consisting of seventeen spaces and nothing more, treat that as you would any other non-empty string: as a valid importer. (Since you don't yet know how to check for that kind of situation anyway, we're not requiring you to.)

The one kind of input error that your program does not have to deal with (because you don't yet know enough to know how to do this nicely), is not finding a number in the input where the weight is expected. We promise that our grading tool will not, for example, supply the text tons and tons when your program requests the weight. Notice that the weight need not be an integer.

The correctness of your program must not depend on undefined program behavior. Your program could not, for example, assume anything about n's value at the point indicated:

	int main()
	{
	    int n;
	    int m = 42 * n;  // n's value is undefined
	    …

Your program must build successfully under both g31 and either Visual C++ or clang++.

What you will turn in for this assignment is a zip file containing these three files and nothing more:

  1. A text file named duty.cpp that contains the source code for your C++ program. Your source code should have helpful comments that tell the purpose of the major program segments and explain any tricky code.
  2. A file named report.docx (in Microsoft Word format) or report.txt (an ordinary text file) that contains:
    1. A brief description of notable obstacles you overcame. (In Project 1, for example, some people had the problem of figuring out how to work with more than one version of a program in Visual C++.)
    2. A list of the test data that could be used to thoroughly test your program, along with the reason for each test. You don't have to include the results of the tests, but you must note which test cases your program does not handle in the way this spec requires. (This could happen if you didn't have time to write a complete solution, or if you ran out of time while still debugging a supposedly complete solution.) For Project 1, for example, such a list, had it been required, might have started off like this:
      More surveyed than the total approving and disapproving (1000, 413, 382 )
      Fewer surveyed than the total approving and disapproving (500, 413, 382 )
      Zero total surveyed (0, 100, 100)
      Data giving a non-integer percentage (1000, 413, 382)
      More approving than disapproving (1000, 413, 382)
      Equal numbers approving and disapproving (1000, 500, 500)
  3. A file named hw.docx (in Microsoft Word format) or hw.txt (an ordinary text file) that contains your solution to the homework accompanying Project 2.

By April 16, there will be links on the class webpage that will enable you to turn in your zip file electronically. Turn in the file by the due time above. Give yourself enough time to be sure you can turn something in, because we will not accept excuses like "My computer died the afternoon of the day the assignment was due." There's a lot to be said for turning in a preliminary version of your program, report, and homework early (You can always overwrite it with a later submission). That way you have something submitted in case there's a problem later. Notice that most of the test data portion of your report can be written from the requirements in this specification, before you even start designing your program.

The writeup Some Things about Strings tells you what you need to know about strings for this project.

As you develop your program, periodically try it out under another compiler (g31 on cs31.seas.ucla.edu if you're doing your primary development using Visual C++ or Xcode). Sometimes one compiler will warn you about something that another is silent about, so you may be able to find and fix more errors sooner. If running your program under both environments with the same input gives you different results, your program is probably relying on undefined behavior (such as using the value of an uninitialized variable), which we prohibit.