help with ceil function

Y

yansong1990

Uhm..it's my 1st time using this fuction...could someone provide
anexample for me please? thanks
 
D

David Harmon

On 18 Mar 2007 07:51:16 -0700 in comp.lang.c++, (e-mail address removed)
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?
 
Y

yansong1990

#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...
 
Y

yansong1990

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)'
 
R

Rolf Magnus

#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);
 
R

Rolf Magnus

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.
 
Y

yansong1990

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?
 
Y

yansong1990

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)
 
J

Jacek Dziedzic

#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.
 
J

Jacek Dziedzic

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.
 
R

red floyd

#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!!!!!
 
A

alex

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.
 
S

SasQ

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.
 
D

David Harmon

On 18 Mar 2007 08:31:47 -0700 in comp.lang.c++, (e-mail address removed)
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));
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top