Home > Software design >  Why does computing a formula with multiple unknowns not working?
Why does computing a formula with multiple unknowns not working?

Time:10-01

I'm trying to compute the formula "V / I = R" in my program by asking the user to input values for V and I. Unfortunately my program does not compile and I'm not sure why.


#include <iostream>

using namespace std;

int main()
{
    int V, I, R;
    cout << "Please enter the voltage: " << endl;
    cin >> V;
    cout << "Please enter the curruent: " << endl;
    cin >> I;

    V / I = R;
    cin >> R;
    cout << "The value of the resistor is: " << R << endl;

    system("PAUSE");
}

CodePudding user response:

C is not a system to solve linear equation, you must tell it what to do.

You need to replace:

V / I = R;
cin >> R;

with

R = V / I;

The knowledge you used, to flip the equation around, stays with you. The compiler needs instructions. Also note the = is not the symbol for equality. It is the symbol for assignment. It evaluates the right-hand side and assigns it to the variable on the left-hand side. Some other language write it := to make this clearer.

You should also use float instead of int if you want to be able to get fractional answers, like 0.5.

  •  Tags:  
  • c
  • Related