Calculator square root

Hi.
I am new to c++ and started building a simple calculator. I did manage to make it to the easy stuff like plus, minus, multiply and devide. Now I want to add the "Square root". I have managed to make the program to square root, but my problem is that I have to enter 2 numbers (As for plus, minus, multiply and devide). So my question is: How can I make it so when I make input 5 (for square root) it will cout "Enter the number you want to square root: "? and not "Enter num 1" "Enter num 2"?
Here is my code:

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
#include<iostream>
#include<cmath>
using namespace std;

int choice;
double num1, num2, result;


int main()
{
  cout << "CALCULATOR   \nBy Sander Lindberg\n\n";

  while(true)
  {
    cout <<   "1. Add\n" <<
              "2. Subtract\n" <<
              "3. Multiply\n" <<
              "4. Devide\n" <<
              "5. Squareroot\n"
              "6. Exit\n\n" <<
              "What would you like to do? ";

    cin >> choice;

    if(choice == 6)
        break;



    cout << "\nPlease enter the first number: ";
    cin >> num1;
    cout << "Now, please enter the second number: ";
    cin >> num2;

    if(choice == 1)
        result = num1 + num2;
    else if(choice == 2)
        result = num1 - num2;
    else if(choice == 3)
        result = num1 * num2;
    else if(choice == 4)
        result = num1 / num2;
    else if(choice == 5)
        result = sqrt(num1);




    cout << "\nThe answer is: " << result << "\n\n" << endl;


  }
  cout << "\nBye bye!\n\n";


}
You must make the distinction between operations which only require one operand and those that require two.
1
2
3
4
5
6
7
8
9
10
11
12
13
if (choice == 5)
{
    //user chose square root, they only need to provide one number
    cout << "\nPlease enter the number: ";
    cin >> num1;
}
else
{
    cout << "\nPlease enter the first number: ";
    cin >> num1;
    cout << "Now, please enter the second number: ";
    cin >> num2;
}
Thank you! :)
Topic archived. No new replies allowed.