# How to correctly use if/else if?????? Please Help

Printable View

• 04-27-2011
ambellina
How to correctly use if/else if?????? Please Help
Here is the assignment:
The current I drawn by an electrical appliance of power P watts from a supply voltage V volts is given by the formula: I = P/V amps. An electrical supplier stocks three types of cable, suitable for currents of up to 5 amps, 13 amps, and 30 amps, respectively. Write a program that asks the user for the power rating and supply voltage of an appliance, and displays the most suitable cable, or a warning if the appliance cannot be safely used with any of the cables in stock.
------------------------------------------------------------------------
I have everything set up to the best of my ability:
Code:

```#include <iostream> #include <cmath> using namespace std; int main() { float P,V; cout << "Enter the power rating of the appliance: " << endl; cin >> P; cout << "Enter the supply voltage of the appliance: " << endl; cin >> V; if (0 < P/V <= 5) { cout << "This current is suitable for the 5 amp cable" << endl; } else if (5 < P/V <= 13) cout << "This current is suitable for the 13 amp cable" << endl; else if (13 < P/V <= 30) cout << "This current is suitable for the 30 amp cable" << endl; else if (P/V > 30) cout << "This current can not be safely used with any of the cables in stock" << endl; return 0; }```
------------------------------------------------------------------------
But when I try using my program, I get the same answer for every current (That the 5 amp cable is appropriate):

Enter the power rating of the appliance:
90
Enter the supply voltage of the appliance:
2
This current is suitable for the 5 amp cable

How do I properly use if, else if so that my program runs correctly?
• 04-27-2011
jimblumberg
In C++ you can not check multiple conditions against a single value:
Code:

`if (0 < P/V <= 5)`
This should be
Code:

`if(0 < (P/V) && (P/V) < = 5)`
See this link for information on control statements.

Jim