help with ceil function

Discussion in 'C++' started by yansong1990, Mar 18, 2007.

  1. yansong1990

    yansong1990 Guest

    Uhm..it's my 1st time using this fuction...could someone provide
    anexample for me please? thanks
     
    yansong1990, Mar 18, 2007
    #1
    1. Advertisements

  2. yansong1990

    yansong1990 Guest

    oh yes one more thing >.< how do i use the sqrt function?
     
    yansong1990, Mar 18, 2007
    #2
    1. Advertisements

  3. yansong1990

    David Harmon Guest

    On 18 Mar 2007 07:51:16 -0700 in comp.lang.c++,
    wrote,
    What did you try? Post some code!
    How did the result differ from what you wanted/expected?
     
    David Harmon, Mar 18, 2007
    #3
  4. yansong1990

    yansong1990 Guest

    #include <iostream>
    #include <cstdlib>
    #include <math.h>
    using namespace std;

    int main()
    {
    float weight;

    cout<<"Enter parcel weight ";
    cin>>weight;

    if (weight <= 2)
    {
    cout<<"Cost is $3.25"
    <<endl;
    goto end;
    }
    else
    {
    weight = weight - 2;
    ceil(weight);
    cout<<"Cost is $"
    <<weight*1.05+3.25
    <<endl;
    }

    end:
    system("pause");
    return 0;
    }


    If i enter weight as 5.63kg i should get cost as $7.45...butI'm
    getting $7.0615...
     
    yansong1990, Mar 18, 2007
    #4
  5. yansong1990

    yansong1990 Guest

    As for the sqrt...

    #include <iostream>
    #include <math.h>
    #include <cstdlib>
    using namespace std;

    int main()
    {
    int number, test;
    float squareroot;

    cout<<"Input number: ";
    cin>>number;
    test = 2;
    squareroot = sqrt(number);

    I will get this error-

    1>.\assg3q6.cpp(20) : error C2668: 'sqrt' : ambiguous call to
    overloaded function
    1> C:\Program Files\Microsoft Visual Studio 8\VC\include
    \math.h(581): could be 'long double sqrt(long double)'
    1> C:\Program Files\Microsoft Visual Studio 8\VC\include
    \math.h(533): or 'float sqrt(float)'
    1> C:\Program Files\Microsoft Visual Studio 8\VC\include
    \math.h(128): or 'double sqrt(double)'
     
    yansong1990, Mar 18, 2007
    #5
  6. yansong1990

    Rolf Magnus Guest

    Well, ceil() returns the next bigger whole number. Since you're ignoring the
    return value, it has no effect at all. You probably want

    weight = ceil(weight);
     
    Rolf Magnus, Mar 18, 2007
    #6
  7. yansong1990

    Rolf Magnus Guest

    That's because you're giving an int as parameter to sqrt. Since there are
    only the overloads that the compiler is talking about, the value must be
    converted, but all three conversions are equally well, so the compiler
    can't decide which one to use. You have to cast the value to the type you
    want.
     
    Rolf Magnus, Mar 18, 2007
    #7
  8. yansong1990

    yansong1990 Guest

    Could you show me an example please?
     
    yansong1990, Mar 18, 2007
    #8
  9. yansong1990

    yansong1990 Guest

    Okay i tried to make them all be float...but later in the code i will
    get this

    1>.\assg3q6.cpp(24) : error C2296: '%' : illegal, left operand has
    type 'float'

    seems the problem is lying on this line of code

    if ((number%test)== 0)
     
    yansong1990, Mar 18, 2007
    #9
  10. You don't store the result of the ceil function anywhere.
    Contrary to what you expect, it doesn't overwrite the value
    you supply, but rather returns the ceil'ed value.

    HTH,
    - J.
     
    Jacek Dziedzic, Mar 18, 2007
    #10
  11. Change "int number" to "double number" so that the compiler
    knows which version of sqrt to pick.

    HTH,
    - J.
     
    Jacek Dziedzic, Mar 18, 2007
    #11
  12. yansong1990

    red floyd Guest

    // NEVER EVER EVER!!!!! Use goto like this. NEVER!!!!!
     
    red floyd, Mar 18, 2007
    #12
  13. yansong1990

    alex Guest

    If i enter weight as 5.63kg i should get cost as $7.45...butI'm
    Of course you are, that's what you're asking for, what did you expect?
    3.63 * 1.05 + 3.25 = 7.0165

    Seriously though, without taking it personally, this is some of the most
    appalling code I've seen since the late 70s, in the bad old days of
    FORTRAN. It made me shudder - literally!

    May I ask if you have used another comp lang before? And was it
    FORTRAN, TI-Basic, or similar?

    Right, I'll try and be constructive...

    a) as "yansong1990" said, why the "goto" statement? It's not needed and
    is bad programming. Only the most experienced programmers writing
    awfully convoluted software ever have to use it, and even then, that's
    debatable. If your "if" branch is true, the "else" will be skipped
    anyway.

    b) What's with the "ceil(weight);" line? You are making a call to the
    ceil() function, but you are not storing the result anywhere. Are you
    somehow expecting the call to change the value of "weight"? OMFG!

    c) Why on earth are you requesting a system pause?

    Try this:-
    if (weight <= 2.0)
    { cout<<"Cost is $3.25" << endl; }
    else
    { cout<<"Cost is $" << ceil(weight-2.0)*1.05+3.25 << endl;}

    Any better?

    And get yourself a proper C++ book PRONTO, if you intend taking this
    further.
    Moo & Koenig - "Accelerated C++" is my recommendation.
     
    alex, Mar 18, 2007
    #13
  14. yansong1990

    SasQ Guest

    Dnia Sun, 18 Mar 2007 09:11:24 -0700, yansong1990 napisa³(a):

    (24) : error C2296: '%' : illegal, left operand has
    Use std::fmod() from <cmath>.
    Operator % is for integers only.
     
    SasQ, Mar 18, 2007
    #14
  15. yansong1990

    David Harmon Guest

    On 18 Mar 2007 08:31:47 -0700 in comp.lang.c++,
    wrote,
    double squareroot;

    You should prefer double ahead of float unless you have
    some reason not to.
    squareroot = sqrt(double(number));
     
    David Harmon, Mar 18, 2007
    #15
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.