Creating a more advanced calculator

I have this calculator
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
63
64
65
66
67
68
69
70
#include <iostream>

using namespace std;

int main()
{
      int a;
      int b;
      int sum;
      string userResponse;
      string userinput;
      cout << "Please enter a number: \n";
    cin >> a;

    cout << "Now choose what you want to do: +, -, *, %.\n";
    cin >> userinput;
        if(userinput == "+")
        {
            cout << "Now choose another number: \n";
            cin >> b;
            sum = a + b;
            cout << "The total of those numbers are: " << sum << endl;
        }
        if(userinput == "-")
        {
            cout << "Now choose another number: \n";
            cin >> b;
            sum = a - b;
            cout << "The total of those numbers are: " << sum << endl;
        }
        if(userinput == "*")
        {
            cout << "Now choose another number: \n";
            cin >> b;
            sum = a * b;
            cout << "The total of those numbers are: " << sum << endl;
        }
        if(userinput == "%")
        {
            cout << "Now choose another number: \n";
            cin >> b;
            sum = a % b;
            cout << "The total of those numbers are: " << sum << endl;
        }

    if(sum > 20){
      cout << "\nHa, your number is greater than 20 \n";
      }
       if(sum < 20){
       cout << "\nCan't catch me out, your number is less than 20 \n";
       }
       if(sum == 20){
       cout << "\nYour number is equal to 20 \n";
       }

       cout << "\nDoes this calculator work correctly? (Yes/No) \n";
       cin >> userResponse;

       if(userResponse == "Yes"){
       cout << "Thanks for your honesty \n";
       }
       if(userResponse == "No"){
       cout << "I know it works so stop lying";
       }
      
}




But as you can see I have 9 if statements and to me that is a bit excessive, so I was wondering is there anyway to shorten it down by using less if statements. And if so, how?
You need to write the code for every possibility, so in that sense the answer is no.

However, there are more efficient ways to do what you want. The first is to take the code that you're using for every "if", and move it outside. This way you don't have to write it over and over again. Another thing to do is to replace those "if"s with "else if"s. This makes it so when the program finds what it's looking for, it doesn't keep looking. Another approach is to make userInput a char and use a switch case, which I find easier to read (and are faster than a bunch of "if/else if" for the computer).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
cout << "Now choose another number: \n";
cin >> b;

if(userinput == "+")
{
  sum = a + b;
}
else if(userinput == "-")
{
  sum = a - b;
}
else if(userinput == "*")
{
  sum = a * b;
}
else if(userinput == "%")
{
  sum = a % b;
}

cout << "The total of those numbers are: " << sum << endl;
Or even

1
2
3
4
5
6
7
8
9
10
11
12
13
cout << "Now choose another number: \n";
cin >> b;

switch(userinput[0])
{
  case '+' : sum = a + b; break;
  case '-' : sum = a - b; break;
  case '*' : sum = a * b; break;
  case '/' : sum = a / b; break;
  default: reportError(); // to do
}

cout << "The total of those numbers are: " << sum << endl;
Last edited on
Thanks both of you guys for the help :D Now the user only needs to enter a calculation, and its also not limited to one calculation like how it could only add or multiply at once.
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
#include <iostream>

using namespace std;

int main()
{
      int a;
      int b;
      int sum;
      string userResponse;
      char userinput;
      cout << "\t / = Divide, * = Multiply";
      cout << "\n\nEnter a calculation: ";
      cin >> a;
      cin >> userinput;
      cin >> b;


    switch(userinput)
    {
        case '+' : sum = a + b;
        break;
        case '-' : sum = a - b;
        break;
        case '*' : sum = a * b;
        break;
        case '/' : sum = a / b;


    }

    cout << "\nThe total of those numbers are: " << sum << endl;
    if(sum > 20){
      cout << "\nHa, your number is greater than 20 \n";
      }
       if(sum < 20){
       cout << "\nCan't catch me out, your number is less than 20 \n";
       }
       if(sum == 20){
       cout << "\nYour number is equal to 20 \n";
       }

       cout << "\nDoes this calculator work correctly? (Yes/No) \n";
       cin >> userResponse;

       if(userResponse == "Yes"){
       cout << "Thanks for your honesty \n";
       }
       if(userResponse == "No"){
       cout << "I know it works so stop lying";
       }



}
An after thought I just had, say instead of having
1
2
cout << "\nDoes this calculator work correctly? (Yes/No) \n";
       cin >> userResponse;

I have
1
2
cout << "\nDo you want use this again? (Yes/No) \n";
       cin >> userResponse;]

How would would I make so that if the user types in "Yes", the program will run again and if the user typed "No" it would close down. Is there some sort of loop for that? I thought about doing if statements but that's not exactly efficient, just ctrl+c and ctrl+v"ing" over and over. I think I remember watching a tutorial on something similar but I can't remember what is was.
Is there some sort of loop for that?

Yep! I'd prob. use a do-while loop in this case.

See the "The do-while loop" section on this page:

Control Structures
http://www.cplusplus.com/doc/tutorial/control/

Andy

PS Is this a set exercise? Or something you are working on for yourself?
Last edited on
Thanks Andy :D Very simple and easy to put into my program :) And I'm working on it myself
Last edited on
Topic archived. No new replies allowed.