if else

This is a discussion on if else within the C++ Programming forums, part of the General Programming Boards category; im trying to write a program that contains if , else. when i compile it there are no errors, but ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    2

    Question if else

    im trying to write a program that contains if , else. when i compile it there are no errors, but when you run it it will only give you the result of what is in the IF statement. Can anyone please help me figure out what im doing wrong? I copied and pasted what i have so far ...

    Code:
    #include  <iostream>
    #include  <iomanip>
    
    using namespace std;
    const double Connect_Fee = 1.99;
    const double one_to_three_min = 2.00;
    const double additional_min = 0.45;
    
     int main ()
    {
        int numOfMinUsed;
        double amountDue;
          
        cout << endl;
        cout << fixed << showpoint;
        cout << setprecision(2);
        
        cout << "Enter the number of minutes the call lasted from "
             << "New York to New Delhi." << endl;
        cin >> numOfMinUsed;
        cout << endl;    
        
        if (one_to_three_min<= 3)
           amountDue = Connect_Fee
                         +one_to_three_min;    
        
        else 
           amountDue = Connect_Fee
                         + one_to_three_min
                         + (numOfMinUsed -3) * 
                         + additional_min;
        
        cout << "Amount due: $"
             << amountDue << endl;
      
        
        system("pause");
        return 0;
    }
    Last edited by moe47; 09-15-2006 at 12:19 PM.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Use code tags.

    Then think about the if condition.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,910
    Please place your code in [code] and [/code] forum bbcode tags.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,663
    > if (one_to_three_min<= 3)
    Because you declared this as a const, you basically said
    if (2.0<= 3)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    2
    thank you, I didnt even catch that. I have now fixed the problem.

Popular pages Recent additions subscribe to a feed

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