By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 810 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

help with ceil function

P: n/a
Uhm..it's my 1st time using this fuction...could someone provide
anexample for me please? thanks

Mar 18 '07 #1
Share this Question
Share on Google+
14 Replies


P: n/a
oh yes one more thing >.< how do i use the sqrt function?

Mar 18 '07 #2

P: n/a
On 18 Mar 2007 07:51:16 -0700 in comp.lang.c++, ya*********@yahoo.com
wrote,
>oh yes one more thing >.< how do i use the sqrt function?
What did you try? Post some code!
How did the result differ from what you wanted/expected?
Mar 18 '07 #3

P: n/a
#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...

Mar 18 '07 #4

P: n/a
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)'

Mar 18 '07 #5

P: n/a
ya*********@yahoo.com wrote:
#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);
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);

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...
Mar 18 '07 #6

P: n/a
ya*********@yahoo.com wrote:
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)'
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.
Mar 18 '07 #7

P: n/a
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.
Could you show me an example please?

Mar 18 '07 #8

P: n/a
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)

Mar 18 '07 #9

P: n/a
ya*********@yahoo.com wrote:
#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...
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.
Mar 18 '07 #10

P: n/a
ya*********@yahoo.com wrote:
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)'
Change "int number" to "double number" so that the compiler
knows which version of sqrt to pick.

HTH,
- J.
Mar 18 '07 #11

P: n/a
ya*********@yahoo.com wrote:
#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;
// NEVER EVER EVER!!!!! Use goto like this. NEVER!!!!!
}
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...
Mar 18 '07 #12

P: n/a
If i enter weight as 5.63kg i should get cost as $7.45...butI'm
getting $7.0615...
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.

Mar 18 '07 #13

P: n/a
Dnia Sun, 18 Mar 2007 09:11:24 -0700, yansong1990 napisał(a):

(24) : error C2296: '%' : illegal, left operand has
type 'float'

seems the problem is lying on this line of code

if ((number%test)== 0)
Use std::fmod() from <cmath>.
Operator % is for integers only.

--
SasQ
Mar 18 '07 #14

P: n/a
On 18 Mar 2007 08:31:47 -0700 in comp.lang.c++, ya*********@yahoo.com
wrote,
> float squareroot;
double squareroot;

You should prefer double ahead of float unless you have
some reason not to.
> squareroot = sqrt(number);
squareroot = sqrt(double(number));

Mar 18 '07 #15

This discussion thread is closed

Replies have been disabled for this discussion.