# Thread: Ohm's Law Calculator Help (ALMOST COMPLETE)

1. ## Ohm's Law Calculator Help (ALMOST COMPLETE)

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:

1. 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", &current);

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", &currentcheck);

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);
}```

2. Are you using goto or I am getting blind? :O

3. I'm using goto to get to the end of the program so that it doesn't print unnecessary information, if I understand your question correctly.

4. Within 2 % tolerance implies the use of fabs might be a good idea.

if (fabs(1 - N/D) < 0.02)

The correct value is the one I normally use for D. So, I would use the user value for N. In other words, the check value for D; and the user input for N.

FYI: Most graders I have had would take off a large number of points for using a goto.

Tim S.

5. There are many better ways of structuring this program than using goto.

Also this looks very complicated even discounting the horrible goto statements.
Code:
```   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;
}```
Think about using a loop instead of the goto. After all you probably want to re-enter the data if you get improper input.

Jim

6. also you only NEED two inputs of data, ohms law and the power law will give you the other 2

7. My professor suggested i use goto, so I don't know what to tell you (this is an introductory course). Also, I know how Ohm's law works... it's ridiculous that I have to create a program that will handle 1-4 inputs but that is what the assignment calls for.

8. Originally Posted by jrjones0109
My professor suggested i use goto
That is way too hard for me to believe. Are you sure you understood correctly what the prof said? In any case, read again Jim's post.

9. Originally Posted by jrjones0109
My professor suggested i use goto
If that is true I'd probably recommend you drop the course, or try to take it with another professor. The blind leading the blind is no way to learn programming.

10. Originally Posted by Cat
If that is true I'd probably recommend you drop the course, or try to take it with another professor. The blind leading the blind is no way to learn programming.
I fully disagree. You should discuss about what you were told here with your professor, let him say why he said (if he really said) such a thing and then you make a decision

11. Yes I'm sure he said that. goto was not in my original program until he suggested I put it there to get rid of the duplicate print statements I was getting. Using a loop does make sense though. Assuming I implement one of the loop functions that still doesn't solve my problem with that I stated in my original post. Something has to be wrong with my final if statement. I understand using goto is sloppy but for the time being it is working.

12. Originally Posted by jrjones0109
I understand using goto is sloppy but for the time being it is working.
I guess it depends on whether your goal is to pass a programming course or learn to be a programmer. If you just want to get your grade and never look back, that's one thing. If you actually plan to program long term, you should set higher standards for yourself. "Sloppy but working" code is toxic to any project it makes its way into. It's a maintenance nightmare - the time it takes to clean up bad code after it's in production is often exponentially larger than the time it would take to do it right the first time.

Repetition becomes habit. An introductory programming course is the place where you will instill habits - make sure you're instilling the habits you want to have when you're done with school.

13. Here is an example of your data entry that removes the goto:
Code:
```   int repeat;
do
{
repeat = 1;
printf("OHMS LAW CALCULATOR \n");
printf("Enter voltage in volts \n");
scanf("%f", &voltage);

printf("Enter current in amps \n");
scanf("%f", &current);

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");
}
else if ((voltage == -1) && (current == -1) && (power == -1))
{
printf("NOT ENOUGHT DATA");
}
else if ((current == -1) && (resistance == -1) && (power == -1))
{
printf("NOT ENOUGHT DATA");
}
else if ((voltage == -1) && (resistance == -1) && (power == -1))
{
printf("NOT ENOUGHT DATA");
}
else
{
printf("\nTry again\n");
repeat = 0;
}
} while(repeat);```
This just removes the gotos, nothing else. I didn't change any of your logic and it probably isn't an ideal solution but it does eliminate several goto statements.

Also note that comparing a floating point number with the == or != operators is not recommended. Floating point numbers are approximations.

Jim

14. wow EVERY programmer i know, and some prof`s from 5 different schools that i help tutor/teach with/for, simply says "goto is not part of programming, it is only left there because of old habits."

does your programming teacher actually program, or just got thrown into teaching C? lol