Write a program display the following menu:
Ohms Law Calculator
1. Calculate Resistance in Ohms
2. Calculate Current in Amps
3. Calculate Voltage in volts
4. Quit Enter your choice (1-4)
If the user enters 1, the program should ask for voltage in Volts and the current in Amps. Use the following formula:
R= E/i
Where:
E= voltage in volts
I= current in amps
R= resistance in ohms
If the user enters 2 the program should ask for the voltage in Volts and resistance in Ohms. Use the following formula:
I= E/R
Where
I= Current in Amps
E= Voltage in Volts
R= resistance in Ohms
If the user enters 3 the program should ask for resistance in Ohms and current in Amps. Use the following formula:
E= I*R
where;
I= Current in Amps
E= Voltage in volts
R= Resistance in Ohms
If the user enters 4 the program should end
. Input validation: Display an error message if the user enters a number outside the range of 1 through 4 when selecting an item from the menu and exit program Do not accept negative input values for Resistance. If a negative Resistance input value is entered display an error message and exit the program When calculating resistance, if one of inputs is negative and the other positive an error message should be displayed and then exit the program.
C++ Programming
Solution :
#include<iostream>
using namespace std;
void resistance() {
double E, I, R;
cout << "Enter Voltage in Volts: ";
cin >> E;
cout << "Enter Current in Amps: ";
cin >> I;
if(E >= 0 & I > 0)
cout << "Resistance in Ohms:
" << E/I << endl;
else cout << "ERROR! Voltage and Current should
be positive" << endl;
}
void current() {
double E, I, R;
cout << "Enter Voltage in Volts: ";
cin >> E;
cout << "Enter Resistance in Ohms: ";
cin >> R;
if(E >= 0 & R > 0)
cout << "Current in Amps: "
<< E/R <<endl;
else cout << "ERROR! Voltage and Resistance
should be positive" << endl;
}
void voltage() {
double E, I, R;
cout << "Enter Resistance in Ohms: ";
cin >> R;
cout << "Enter Current in Amps: ";
cin >> I;
if(I >= 0 & R >= 0)
cout << "Voltage in Volts: "
<< I*R <<endl;
else cout << "ERROR! Resistance and Current
should be positive" << endl;
}
int main() {
double E, I, R;
int choice;
while(true)
{
cout << "\nOhms Law
Calculator" << endl;
cout << "1. Calculate
Resistance in Ohms\n2. Calculate Current in Amps\n3. Calculate
Voltage in volts\n4. Quit" << endl;
cout << "Enter Your
Choice(1-4): ";
cin >> choice;
if(choice == 1)
resistance();
else if(choice == 2)
current();
else if(choice == 3)
voltage();
else if(choice == 4)
break;
else cout << "Wrong choice,
Plzzz choose correct option\n" << endl;
}
return 0;
}
Screenshot:
Get Answers For Free
Most questions answered within 1 hours.