0

I know this question has been asked before, but the answers there don't seem to be related to the issue I'm having.

Here's my code

#include <iostream>

int main()
{
    double E;
    double R;
    double t;
    double C;
    
    std::cout << "This program will calculate the current flowing through an RC curcuit!\n\n";
    std::cout << "Please enter the power source voltage value in Volts: ";
    std::cin >> E;
    std::cout << "\n\n";
    std::cout << "Please enter the total resistance value in Ohms: ";
    std::cin >> R;
    std::cout << "\n\n";
    std::cout << "Please enter the time elapsed after the switch closed Seconds: ";
    std::cin >> t;
    std::cout << "\n\n";
    std::cout << "Please enter the total capacitance value in Farads: ";
    std::cin >> C;
    std::cout << "\n\n";
    double RC = R * C;
    double ER = E / R;
    double pow = -t / RC;
    double expo = 2.71828 ** pow; //this line is the problem...

    double I = ER * expo;
    std::cout << "The current flowing through this circuit is: " << I;

    return 0;
}

I really don't know what this means, despite looking it up on Google... Can someone please explain, generally, how to avoid this type of error and not just solve this particular instance of it?

  • 4
    C++ doesn't have a `**` operator. You need the [`std::pow`](https://en.cppreference.com/w/cpp/numeric/math/pow) function to do floating-point exponentiation. Note this means `pow` is not a great choice for your variable name. – Nate Eldredge Oct 13 '21 at 00:45
  • So that's what it was... thanks, @NateEldredge !! – Emran BinJumaan Oct 13 '21 at 00:46
  • 2
    The reason for the error is that since there is no `**` operator, it gets parsed as `2.71828 * *pow`, where `*pow` appears to be trying to apply the unary `*` pointer dereference operator to `pow`. This then errors because `pow` isn't a pointer. – Nate Eldredge Oct 13 '21 at 00:47
  • 4
    Actually, if you're trying to raise `e` to a power, use `[std::exp(p)](https://en.cppreference.com/w/cpp/numeric/math/exp)` instead; it is more accurate than `std::pow(2.71828, p)`. – Nate Eldredge Oct 13 '21 at 00:50
  • 3
    Good man for avoiding `using namespace std;`. This makes it less troublesome to use the `std::pow` function in a function with a variable named `pow`. – user4581301 Oct 13 '21 at 00:50
  • 5
    @user4581301 No `namespace std` and no `bits/stdc++.h`. OP is already leagues above lots of C++ coders. – Silvio Mayolo Oct 13 '21 at 00:51

1 Answers1

3

C++ doesn't have a ** operator like some languages do. You need to use the std::pow function to do exponents, or std::exp for the particular case of raising the mathematical constant e to a power.

#include <cmath>

...

double expo = std::exp(pow);
Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
  • yes, thank you and Nate for your input... I was wondering why it said '\*' instead of "\**" lmao – Emran BinJumaan Oct 13 '21 at 00:48
  • 2
    Yeah, C++ is parsing that as `2.71828 * (*pow);`, so it expects `pow` to be a pointer. It's not the most intuitive thing, but it's the only way the language could make sense of it unfortunately. – Silvio Mayolo Oct 13 '21 at 00:49