Lab 11

Don't forget to finish the quiz first.

Lab assignment: Introduction to C/C++ - Program a calculator (again)

Getting started: You may use any editing environment you like.  XCode is the default editing environment for C and C++ code on the Mac.  I personally use a text editor and the command line, as Professor Reed demonstrated in class, so I will be able to provide the most assistance if you are in a similar environment, but I am here to help if you are trying a different environment, though my help may be more limited.  For example, I do not know how to set up Eclipse for C++.  I can try to help you parse any errors you may encounter while trying to set it up, but I don't know the process so my guess might not be much better than yours.  Choose an environment which is most comfortable for you.

In this week's lab you must write a C/C++ program to simulate a simple calculator.

This calculator will perform up to 4 functions on two inputs.

For the first point, make a C (.c) or C++ (.cpp) file which compiles, and when run, displays the menu as below, inputting the numbers to variables, then outputting each of them at the end.  The numbers should be input as ints, and the operand as a single char value.

For the second point, write 4 functions, one for each of the basic arithmetic operations (+,-,*,/).  Each function should take 2 integers as arguments, and return a single integer which is the result of the operation.  If you wish to use a floating point number as the return type, that is acceptable as well.

Employ these functions after your menu to successfully perform calculations.  Note that your functions (or at least function declarations) should appear before the main method in your code.

For a third point, wrap the whole thing up in a loop, and add the option of using the result from the previous step as the first number in the new operation.  To clarify, the program should ask the user whether they want to use a new number or the previous result, if they ask for a new number then input that number, then either way ask for the operand and second number as normal.  The memory value should be initialized to 0 at the beginning of the program.  This program should loop forever.  If you ran it at the command line, use Ctrl - c to exit.

The execution of the program should look similar to the following:

Welcome to the calculator.

Enter the first number: 33

Enter an operand (+,-,* or /): +

Enter the second number: 5

You entered 33, +, 5.

Exiting program...

OR

Welcome to the calculator.

Enter the first number: 33

Enter an operand (+,-,* or /): +

Enter the second number: 5

Result from 33 + 5 is: 38

Exiting program...

OR

Welcome to the calculator.

Use memory value (Y/N): N

Enter the first number: 3

Enter an operand (+,-,* or /): +

Enter the second number: 5

Result from 3 + 5 is: 8

Use memory value (Y/N): Y

Enter an operand (+,-,* or /): -

Enter the second number: 10

Result from 8 - 10 is: -2

Exiting program...

Don't worry about comments, coding conventions, or error checking of the input for this submission, though if you have free time it does make the code nicer.

Do put the names and netIDs of both partners in a comment at the top, as well as the lab section you are in.

Be sure to take turns coding!  It's easy to get wrapped up in the coding and forget to give your partner their turn on the keyboard, and it's too easy to sit back and discuss the problem without writing the code yourself.

Submission:

1. You should work with a partner for this lab.  Only one of you needs to submit the program to Blackboard.  Both names must be present in the program you submit.

2. You have until noon on 3/29 (tomorrow) to submit the lab to Blackboard.  Plan your time wisely, and ask questions if you are working but can not progress.

3. You may use either C or C++ to complete this assignment, but be sure you use the appropriate extension for your submission.  Also, executables will not be accepted, so please do not submit any a.outs or their like.