Hello,

I have a lab assignment in an introductory programming class at the college course. The assignment is to create an ohm's law calculator.

The program should prompt the user for 4 different inputs, Resistance (in ohms), Potential difference (in Volts), Current (in Amps) and Power (in watts). The user must input at least two inputs. Valid inputs include any real number except for -1. If a -1 is inputted, your program should recognize that the variable containing the -1 is the variable that you are solving for. The following possibilities must be dealt with:

If less than two inputs are entered:

Prompt the user “NOT ENOUGH DATA"

2. If two inputs are entered:

Then calculate the other two values

3. If three inputs are entered:

Verify that the three inputs are valid (i.e. if I, V & R are input, validate that I*R = V within 2%). If valid, then calculate the fourth variable. If not valid prompt the user “INVALID DATA” and end the program.

4. If Four inputs are entered:

Verify that all four inputs are valid within 2%. If valid, prompt the user “VALID DATA” if not valid, prompt the user “INVALID DATA”.

I have written all of the program and it all works EXCEPT; I am having trouble getting the calculator to work if 4 inputs are entered. I feel that I have made a minor error but I am having trouble locating it. My code is attached. Help please!

Jason

Code:#include <stdio.h> #include <math.h> int main (void) { float voltage , current , resistance, power, b, voltagecheck = 0, currentcheck = 0, resistancecheck = 0, powercheck = 0; printf("OHMS LAW CALCULATOR \n"); printf("Enter voltage in volts \n"); scanf("%f", &voltage); printf("Enter current in amps \n"); scanf("%f", ¤t); printf("Enter resistance in ohms \n"); scanf("%f", &resistance); printf("Enter power in watts \n"); scanf("%f", &power); if ((voltage == -1) && (current == -1) && (resistance == -1)) { printf("NOT ENOUGH DATA"); goto ENDING; } else if ((voltage == -1) && (current == -1) && (power == -1)) { printf("NOT ENOUGHT DATA"); goto ENDING; } else if ((current == -1) && (resistance == -1) && (power == -1)) { printf("NOT ENOUGHT DATA"); goto ENDING; } else if ((voltage == -1) && (resistance == -1) && (power == -1)) { printf("NOT ENOUGHT DATA"); goto ENDING; } if ((voltage == -1) && (power == -1)) { voltage = current * resistance; power = pow(current, 2) * resistance; printf("voltage: %f. \n current: %f. \n resistance: %f. \n power: %f. \n" ,voltage, current, resistance, power); goto ENDING; } else if ((voltage == -1) && (current == -1)) { voltage = sqrt(power * resistance); current = sqrt(power / resistance); printf("voltage: %f. \n current: %f. \n resistance: %f. \n power: %f. \n" ,voltage, current, resistance, power); goto ENDING; } else if ((voltage == -1) && ( resistance == -1)) { voltage= power / current; resistance = voltage / current; printf("voltage: %f. \n current: %f. \n resistance: %f. \n power: %f. \n" ,voltage, current, resistance, power); goto ENDING; } else if ((current == -1) && (resistance == -1)) { current= power / voltage; resistance= voltage / current; printf("voltage: %f. \n current: %f. \n resistance: %f. \n power: %f. \n" ,voltage, current, resistance, power); goto ENDING; } else if ((current == -1) && (power == -1)) { current= voltage / resistance; power= voltage * current; printf("voltage: %f. \n current: %f. \n resistance: %f. \n power: %f. \n" ,voltage, current, resistance, power); goto ENDING; } else if ((resistance == -1) && (power == -1)) { resistance= voltage / current; power= voltage * current; printf("voltage: %f. \n current: %f. \n resistance: %f. \n power: %f. \n" ,voltage, current, resistance, power); goto ENDING; } if ( voltage == -1) { voltage = current * resistance; printf("voltage: %f. \n current: %f. \n resistance: %f. \n power: %f. \n" ,voltage, current, resistance, power); goto ENDING; } else if (current == -1) { current= voltage / resistance; printf("voltage: %f. \n current: %f. \n resistance: %f. \n power: %f. \n" ,voltage, current, resistance, power); goto ENDING; } else if (resistance == -1) { resistance= voltage / current; printf("voltage: %f. \n current: %f. \n resistance: %f. \n power: %f. \n" ,voltage, current, resistance, power); goto ENDING; } else if (power == -1) { power = voltage * current; printf("voltage: %f. \n current: %f. \n resistance: %f. \n power: %f. \n" ,voltage, current, resistance, power); goto ENDING; } if ((voltage != -1) && (current != -1) && (resistance != -1) && (power != -1)) { voltagecheck = (current * resistance); scanf("%f", &voltagecheck); currentcheck = (voltage / resistance); scanf("%f", ¤tcheck); resistancecheck = (voltage / current); scanf("%f", &resistancecheck); powercheck = (voltage * current); scanf("%f", &powercheck); } if ((voltagecheck < (voltage - (voltage * 0.02))) && (voltagecheck > (voltage + (voltage * 0.02))) && (currentcheck < (current - (current * 0.02))) && (currentcheck > (current + (current * 0.02))) && (resistancecheck < (resistance - (resistance * 0.02))) && (resistancecheck > (resistance + (resistance * 0.02))) && (powercheck < (power - (power * 0.02))) && (powercheck > (power + (power * 0.02)))) { printf("vofltagecheck: %f. \n currentcheck: %f. \n resistancecheck: %f. \n powercheck: %f. \n" ,voltagecheck, currentcheck, resistancecheck, powercheck); printf("VALID DATA"); } else { printf("INVALID DATA"); goto ENDING; } ENDING: scanf("%f",&b); }