-3

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");
}
cigien
  • 57,834
  • 11
  • 73
  • 112
bc345
  • 11
  • 2
  • What code have you tried writing to solve this problem? – Chris Sep 30 '21 at 19:15
  • 1
    How can we help you with an error when you've shared neither your code nor the associated error? – Cory Kramer Sep 30 '21 at 19:16
  • @blosFk I know! Just do not trust the user. Enter the values yourself! – Vlad from Moscow Sep 30 '21 at 19:17
  • My code wasn't able to upload with the question. Trying to upload it now – bc345 Sep 30 '21 at 19:20
  • uploaded the code now. thanks @fabian – bc345 Sep 30 '21 at 19:25
  • *my program does not compile and I'm not sure why* You appear to be in need of a [good C++ book](https://stackoverflow.com/a/388282/4641116). – Eljay Sep 30 '21 at 19:27
  • you need to understand that `=` in code has little to do with `=` in maths. `V / I = R` makes no sense, because you cannot assign something to `V / I` – 463035818_is_not_an_ai Sep 30 '21 at 19:27
  • 3
    You don't write equations in C++. Everything on the left hand side of the `=` operator is the target of the assignment, everything to the right gets evaluated to determine the value to assign to this target. You'll need to swap the sides of your "equation". (The error tells you that you cannot assign something to the temporary used to store the result of evaluating `V/I`) – fabian Sep 30 '21 at 19:28
  • 1
    anyhow why read all three values from the user? When the user tells you all you dont need the computation – 463035818_is_not_an_ai Sep 30 '21 at 19:28
  • @463035818_is_not_a_number i'm only trying to read V and I from the user. And then I want to take those values and computer R with them. – bc345 Sep 30 '21 at 19:32
  • @fabian Ok I swapped both sides of my equation it complies but once I try to enter values for the current or "I" it doesn't allow me to type and gives me an error saying "unhandled exception" – bc345 Sep 30 '21 at 19:34
  • Handy reading that will probably make more sense later: [What are rvalues, lvalues, xvalues, glvalues, and prvalues?](https://stackoverflow.com/questions/3601602/what-are-rvalues-lvalues-xvalues-glvalues-and-prvalues) – user4581301 Sep 30 '21 at 19:35
  • what do you expect `cin >> R;` to do then? – 463035818_is_not_an_ai Sep 30 '21 at 19:35
  • The exception is probably the result of the division by zero. Furthermore note that division with 2 integral operands will always result in the truncated result of the division (with the exception of the division by zero). – fabian Sep 30 '21 at 19:38
  • The programmer's secret weapon is the debugger. A debugger allows you to run the program at your speed and watch what the computer does with the program as it does it. Typical usage is to step through the problem area in the code line by line keeping an eye out for the computer doing something you didn't expect, but in this case you've already seen something you don't expect, so you should step through the program to get an understanding of what lead up to the unhandled exception, like `I` being zero or similar. – user4581301 Sep 30 '21 at 19:38

1 Answers1

1

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.

Jeffrey
  • 11,063
  • 1
  • 21
  • 42