Simple Calculator

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?

/*
Add two large BCD numbers
*/

#include <iostream>
#include <string>
#include <regex>

using namespace std;

//Prototypes
string GetNumberAsString(string prompt);
void AddBCD(const string num1, const string num2, string &sum, bool &carry); //unsigned version
void AddBCD(const string num1, const bool sign1, const string num2, const bool sign2, string &sum, bool &carry); //signed
int AddLeftPadding(const string &LongNum, string &ShortNum);
void RemoveLeftPadding(string &Num);

int main()
{
string num1, num2, sum = "", underline = "";
int ix, length, digit = 0;
bool carry = false, sign1, sign2; //true signs = negative, false=positive

num1 = GetNumberAsString("Num1: ");
num2 = GetNumberAsString("Num2: ");

//left-pad the shorter string with '0'
if (num1.length() > num2.length())
length = AddLeftPadding(num1, num2);
else
if (num2.length() > num1.length())
length = AddLeftPadding(num2, num1);
else
length = num1.length() - 1; //same length


AddBCD(num1, num2, sum, carry);

//Display the operands and sum
RemoveLeftPadding(num1);
RemoveLeftPadding(num2);
underline.insert(0, length + 2, '-');
cout << "\n " << num1 << endl
<< "+" << num2 << endl
<< underline << endl
<< (carry?"":" ") << sum << endl;

return 0;
}

//Get user-entered number
//Call with prompt
//Returns string containing a number
string GetNumberAsString(string prompt)
{
string Num;

cout << prompt;
cin >> Num;
while(regex_search(Num.begin(), Num.end(), regex("[^0-9]"))) //regex("^-?\\d+$") allows leading - sign
{
cout << "Enter digits only: ";
cin >> Num;
}

return Num;
}

//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;

longLength = LongNum.length() - 1;
ShortNum.insert(0, LongNum.length() - ShortNum.length(), '0');

return longLength;
}

//Replace leading zeros in a string with spaces
//Send the &string
void RemoveLeftPadding(string &Num)
{
int ix = 0;

while (ix <= Num.length() - 1 && Num[ix] == '0')
Num[ix++] = ' ';
}

void AddBCD(const string num1, const string num2, string &sum, bool &carry)
{
int digit;

for (int ix = num2.length() - 1; ix >= 0; ix--)
{
digit = num1[ix] + num2[ix] + (carry?1:0) - '0' - '0';
sum.insert(0, 1, static_cast<char>(digit % 10 + '0'));
carry = digit>9?true:false;
}
if (carry)
sum.insert(0, 1, '1');
}
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
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <iomanip>

using namespace 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;
    }
    else if(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;
        
    }
    else if(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;
    }
    else if(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.
Topic archived. No new replies allowed.