I have an assignment that request I create a simple calculator that can handle user inputted integers for (addition / subtraction / multiplication / division / negative numbers)
I am suppose to start off with provided code and customize from there, but find it very complicated for a rather simple program. Below is the provided code, I wrote a different code that achieves the task which is much easier. What am I missing?
//Left Pad the shorter of the two numbers with Zero
//Send the longer &string, then shorter &string
//Returns an index to the rightmost digit in either string
int AddLeftPadding(const string &LongNum, string &ShortNum)
{
int longLength;
You already have the function for add, so just create functions for the other operations and add some decision handling in main.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
num1 = GetNumberAsString("Num1: ");
sign = GetSign(); //have this function display a menu and return the sign as an int or char (int is easier IMO)
num2 = GetNumberAsString("Num2: ");
//left-pad the shorter string with '0'
/* -- left-pad code -- */
//use if/else statements or a switch here for the sign case
switch(sign) {
case 1:
AddBCD(num1, num2, sum, carry);
break;
case 2:
//function for subtract, etc...
break;
//and fill in rest of the cases
}
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
double result, numOne, numTwo;
int option;
cout << "Please select one of the operations from the list below: " << endl;
cout << "1. Addition\n" << "2. Subtraction\n" << "3. Multiplication\n" << "4. Division" << endl;
cin >> option;
if(option < 2){
cout << "Please enter two numbers: " << endl;
cin >> setw(8) >> numOne >> setw(8) >> numTwo;
result = numOne + numTwo;
cout << "The answer is: " << result << endl;
}
elseif(option > 1 && option < 3){
cout << "Please enter two numbers: " << endl;
cin >> setw(8) >> numOne >> setw(8) >> numTwo;
result = numOne - numTwo;
cout << "The answer is: " << result << endl;
}
elseif(option > 2 && option < 4){
cout << "Please enter two numbers: " << endl;
cin >> setw(8) >> numOne >> setw(8) >> numTwo;
result = numOne * numTwo;
cout << "The answer is: " << result << endl;
}
elseif(option >3 && option < 5){
cout << "Please enter two numbers: " << endl;
cin >> setw(8) >> numOne >> setw(8) >> numTwo;
result = numOne / numTwo;
cout << "The answer is: " << result << endl;
}
return 0;
}
Above is the code I wrote to accomplish the task listed above. So I guess my question is more of, why would my professor request using the given code when it is easily handled with a less complex program?
I am very new to C++ so take my inquiry with a grain of salt.
Professionally, you're more likely to encounter a situation like your professor presented, having to modify existing code, than having to write a program from scratch. And even then if you're working in a team you'll have to deal with the code they write without rewriting it. I mean, this is just a small program so it's easy to rewrite it, but in real life you'll have applications that are thousands of lines of code that you'll be asked to add a little bit of functionality. There's no way it would be acceptable to rewrite that just to add a few more menu options.