formula is not working 2

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>

using namespace std;

int main()
{
    double V, I, R;

    int calculation;
    cout << "Ohms Law" << endl << endl;
    cout << "1. Resistance Calculation. " << endl;
    cout << "2. Current Calculation. " << endl;
    cout << "3. Voltage Calculation. " << endl << endl;
    cout << "Enter your choice: ";
    cin >> calculation;
    cout << endl;

    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 << endl;
                 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 << endl;
                 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 << endl;
                 break;

        default : cout << "Error incorrect number choice ";
                  break;
    }





    return 0;
}

Last edited on
1
2
3
4
5
6
7
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;

Where in this code do you set the value of V ?
Hello teacoder,

If you put some blank lines in your code this is much easier to read:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>

using namespace 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.

Andy
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

Thanks to everyone who has helped
Last edited on
Topic archived. No new replies allowed.