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

This is a discussion on How to correctly use if/else if?????? Please Help within the C++ Programming forums, part of the General Programming Boards category; Here is the assignment: The current I drawn by an electrical appliance of power P watts from a supply voltage ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    4

    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?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,699
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. man -k (how to use correctly)
    By cus in forum Linux Programming
    Replies: 2
    Last Post: 01-14-2009, 10:00 AM
  2. Am I doing these correctly?
    By newbcore in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 12:39 AM
  3. Is this used correctly?
    By MuzicMedia in forum C++ Programming
    Replies: 7
    Last Post: 03-08-2008, 09:07 PM
  4. Using cin correctly
    By kwikness in forum C++ Programming
    Replies: 4
    Last Post: 10-21-2006, 01:39 PM
  5. Did I do this correctly?
    By phosphorousgobu in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2004, 09:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21