0

I'm at the beginning of a book called "C++ in one day" with an example code of a simple calculator. After typing the code and trying to run it errors appear and I don't really understand it even it says clearly operation was not declared in this scope.

Thank you in advance!

Here is the whole calculator code:

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int sel = 0;
    cout << "ADVANCE CALCULATOR\n";
    cout << "ENTER THE OPERATION YOU WANT TO CALCULATE\n";
    cout << "[1] Arithmetic\n";
    cout << "[2] Trigonometric\n";
    cout << "[3] Exponential\n";
    cout << "[4] Logarithmic\n";
    cout << "Your choice: ";
    cin >> sel;

    switch (sel) {
    case 1:
        arithmetic();
        break;
    case 2:
        trigonometric();
        break;
    case 3:
        exponential();
        break;
    case 4:
        logarithmic();
        break;
    default:
        cout << "invalid operation";
    }
    return 0;
}
void arithmetic()
{
    int op = 0;
    float A = 0;
    float B = 0;
    cout << "Select operation\n";
    cout << "[1] Addition\n";
    cout << "[2] Substraction\n";
    cout << "[3] Product\n";
    cout << "[4] Division\n";
    cin >> op;
    cout << "Enter first number: ";
    cin >> A;
    cout << "Enter second number: ";
    cin >> B;
    cout << "Result: ";
    switch (op) {
    case 1:
        cout << (A + B);
        break;
    case 2:
        cout << (A - B);
        break;
    case 3:
        cout << (A * B);
        break;
    case 4:
        cout << (A / B);
        break;
    default:
        cout << "Invalid operation";
        break;
    }
    cout << endl;
}
void trigonometric()
{
    int op = 0;
    float val = 0.0;
    cout << "Select\n";
    cout << "[1] Sine\n";
    cout << "[2] Cosine\n";
    cout << "Op: ";
    cin >> op;
    cout << "Enter value: ";
    cin >> val;
    if (op == 1) {
        cout << sin(val);
    }
    else if (op == 2) {
        cout << cos(val);
    }
    else {
        cout << "Invalid operation";
    }
    cout << endl;
}
void exponential()
{
    float base = 0.0;
    float eee = 0.0;
    cout << "Enter base: ";
    cin >> base;
    cout << "Enter expnent: ";
    cin >> eee;
    cout << pow(base, eee) << endl;
}
void logarithmic()
{
    float value = 0.0;
    cout << "Enter value for calculate the log(e): ";
    cin >> value;
    cout << log(value) << endl;
}

This gives the following errors:

<source>: In function 'int main()':
<source>:19:9: error: 'arithmetic' was not declared in this scope
   19 |         arithmetic();
      |         ^~~~~~~~~~
<source>:22:9: error: 'trigonometric' was not declared in this scope
   22 |         trigonometric();
      |         ^~~~~~~~~~~~~
<source>:25:9: error: 'exponential' was not declared in this scope
   25 |         exponential();
      |         ^~~~~~~~~~~
<source>:28:9: error: 'logarithmic' was not declared in this scope
   28 |         logarithmic();
      |         ^~~~~~~~~~~
ASM generation compiler returned: 1
<source>: In function 'int main()':
<source>:19:9: error: 'arithmetic' was not declared in this scope
   19 |         arithmetic();
      |         ^~~~~~~~~~
<source>:22:9: error: 'trigonometric' was not declared in this scope
   22 |         trigonometric();
      |         ^~~~~~~~~~~~~
<source>:25:9: error: 'exponential' was not declared in this scope
   25 |         exponential();
      |         ^~~~~~~~~~~
<source>:28:9: error: 'logarithmic' was not declared in this scope
   28 |         logarithmic();
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
Svet GEoff
  • 85
  • 6

1 Answers1

1

You have functions you are trying to call under the line you are calling them from, in that time they aren't declared yet. Define them before line they are used.

    #include <cmath>
#include <iostream>
using namespace std;
void arithmetic() {
  int op = 0;
  float A = 0;
  float B = 0;
  cout << "Select operation\n";
  cout << "[1] Addition\n";
  cout << "[2] Substraction\n";
  cout << "[3] Product\n";
  cout << "[4] Division\n";
  cin >> op;
  cout << "Enter first number: ";
  cin >> A;
  cout << "Enter second number: ";
  cin >> B;
  cout << "Result: ";
  switch (op) {
    case 1:
      cout << (A + B);
      break;
    case 2:
      cout << (A - B);
      break;
    case 3:
      cout << (A * B);
      break;
    case 4:
      cout << (A / B);
      break;
    default:
      cout << "Invalid operation";
      break;
  }
  cout << endl;
}
void trigonometric() {
  int op = 0;
  float val = 0.0;
  cout << "Select\n";
  cout << "[1] Sine\n";
  cout << "[2] Cosine\n";
  cout << "Op: ";
  cin >> op;
  cout << "Enter value: ";
  cin >> val;
  if (op == 1) {
    cout << sin(val);
  } else if (op == 2) {
    cout << cos(val);
  } else {
    cout << "Invalid operation";
  }
  cout << endl;
}
void exponential() {
  float base = 0.0;
  float eee = 0.0;
  cout << "Enter base: ";
  cin >> base;
  cout << "Enter expnent: ";
  cin >> eee;
  cout << pow(base, eee) << endl;
}
void logarithmic() {
  float value = 0.0;
  cout << "Enter value for calculate the log(e): ";
  cin >> value;
  cout << log(value) << endl;
}
int main() {
  int sel = 0;
  cout << "ADVANCE CALCULATOR\n";
  cout << "ENTER THE OPERATION YOU WANT TO CALCULATE\n";
  cout << "[1] Arithmetic\n";
  cout << "[2] Trigonometric\n";
  cout << "[3] Exponential\n";
  cout << "[4] Logarithmic\n";
  cout << "Your choice: ";
  cin >> sel;

  switch (sel) {
    case 1:
      arithmetic();
      break;
    case 2:
      trigonometric();
      break;
    case 3:
      exponential();
      break;
    case 4:
      logarithmic();
      break;
    default:
      cout << "invalid operation";
  }
  return 0;
}
Tomáš Šturm
  • 489
  • 4
  • 8