So I seem to have the same problem again with the formula and I have done what the other guy said just now on this forum and made sure that the formulas came after the values were given.
case 1 : cout << "Please enter the voltage: ";
cin >> V;
cout << "Please enter the current: ";
cin >> I;
I=R/V;
cout << "The resistance is " << R << endl;
break;
Where in this code do you set the value of R ?
1 2 3 4 5 6 7
case 3 : cout << "Please enter the resistance: ";
cin >> R;
cout << "Please enter the current: ";
cin >> I;
I=R/V;
cout << "The voltage is " << V << endl;
break;
#include <iostream>
usingnamespace std;
int main()
{
double V{}, I{}, R{}; // <--- ALWAYS initialize all your variables.int calculation{};
cout <<
"Ohms Law\n\n""1. Resistance Calculation.\n""2. Current Calculation.\n" // <--- The space at the end is not needed or ever noticed.
"3. Voltage Calculation.\n\n"" Enter your choice: ";
cin >> calculation;
cout << '\n';
switch (calculation)
{
case 1:
cout << "Please enter the voltage: ";
cin >> V;
cout << "Please enter the current: ";
cin >> I;
I = R / V;
cout << "The resistance is " << R << '\n';
break;
case 2:
cout << "Please enter the voltage: ";
cin >> V;
cout << "Please enter the resistance: ";
cin >> R;
I = R / V;
cout << "The current is " << I << '\n';
break;
case 3:
cout << "Please enter the resistance: ";
cin >> R;
cout << "Please enter the current: ";
cin >> I;
I = R / V;
cout << "The voltage is " << V << '\n';
break;
default:
cout << "\n Error incorrect number choice!\n";
break;
}
return 0; // <--- Not required, but makes a good break point for testing.
}
First help your-self by making it easier to read and understand.
As Repeater pointed out take a good look at lines 24 - 30 and ask your-self the question what the value of "R" is and then use a calculator to see what the answer is.
When it is easier to read the problem jumps out quicker.
So basically my problem was that I used a general formula for the code when I was suppose to have a specific formula for finding the different variables.
I have also learnt to use the formating I'm suppose to use when asking for help