Any error in my coding?

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 1637 Views - Last Post: 25 August 2014 - 04:46 AM Rate Topic: -----

#1 Deepak Kadam   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 19-August 14

Any error in my coding?

Posted 19 August 2014 - 09:03 AM

Hello. I wanted to know whether there is any fault in the coding below. It is a simple and basic program to get the values of trigonometric ratios. I have clearly stated the conditions to get the required output but still it gives me an error.

I have used the if....else statement in which I have provided the required conditions to get the perfect output.

I have stated that if the value of the denominator in any of the fraction is equal to 0 then display that it is NOT DEFINED. But still it gives me an error i.e. Floating Point: Divide by Zero. I don't want this error in my output. I know that length cannot be zero in a triangle but still for my sake I want to get the required output.

Please do help me to sort out this issue. I am a beginner and don't know much about all the available operators and header files! So, I always create functions to carry out any operations needed.

#include<iostream.h>
#include<conio.h>
void main()
{
	double x, y, r, angle;
	double sinn, cosn, tann, cotn, secn, cosecn;
	cout<<"Enter the measure of angle in degrees: ";
	cin>>angle;
	cout<<"Enter the length of the side opposite to the angle: ";
	cin>>y;
	cout<<"Enter the length of the side adjacent to the angle: ";
	cin>>x;
	cout<<"Enter the length of the hypotenuse: ";
	cin>>r;
	clrscr();
	sinn=y/r;
	cosn=x/r;
	tann=y/x;
	cotn=x/y;
	secn=r/x;
	cosecn=r/y;
	while(0>=(x, y, r)>=0)
	{
		if(r==0)
		{
		  cout<<"sin("<<angle<<")= NOT DEFINED"<<endl<<endl;
		}
		else
		{
		  cout<<"sin("<<angle<<")= "<<sinn<<endl<<endl;
		}
		if(r==0)
		{
		  cout<<"cos("<<angle<<")= NOT DEFINED"<<endl<<endl;
		}
		else
		{
		  cout<<"cos("<<angle<<")= "<<cosn<<endl<<endl;
		}
		if(x==0)
		{
		  cout<<"tan("<<angle<<")= NOT DEFINED"<<endl<<endl;
		}
		else
		{
		  cout<<"tan("<<angle<<")= "<<tann<<endl<<endl;
		}
		if(y==0)
		{
		  cout<<"cot("<<angle<<")= NOT DEFINED"<<endl<<endl;
		}
		else
		{
		  cout<<"cot("<<angle<<")= "<<cotn<<endl<<endl;
		}
		if(x==0)
		{
		  cout<<"sec("<<angle<<")= NOT DEFINED"<<endl<<endl;
		}
		else
		{
		  cout<<"sec("<<angle<<")= "<<secn<<endl<<endl;
		}
		if(y==0)
		{
		  cout<<"cosec("<<angle<<")= NOT DEFINED"<<endl<<endl;
		}
		else
		{
		  cout<<"cosec("<<angle<<")= "<<cosecn<<endl<<endl;
		}
		cout<<"Enter the measure of angle in degrees: ";
		cin>>angle;
		cout<<"Enter the length of the side opposite to the angle: ";
		cin>>y;
		cout<<"Enter the length of the side adjacent to the angle: ";
		cin>>x;
		cout<<"Enter the length of the hypotenuse: ";
		cin>>r;
		clrscr();
		sinn=y/r;
		cosn=x/r;
		tann=y/x;
		cotn=x/y;
		secn=r/x;
		cosecn=r/y;
	}
getch();
} 

Attached image(s)

  • Attached Image
  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Any error in my coding?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14175
  • View blog
  • Posts: 56,782
  • Joined: 12-June 08

Re: Any error in my coding?

Posted 19 August 2014 - 09:06 AM

Quote

I have stated that if the value of the denominator in any of the fraction is equal to 0 then display that it is NOT DEFINED. But still it gives me an error i.e. Floating Point: Divide by Zero. I don't want this error in my output.


I always find it best to check if a denominator is zero before doing division. If I find that then I would output a message indicating what is happening *AND* exit the function/method without actually doing the math.
Was This Post Helpful? 1
  • +
  • -

#3 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6644
  • View blog
  • Posts: 30,937
  • Joined: 10-May 07

Re: Any error in my coding?

Posted 19 August 2014 - 09:07 AM

...& the antique compiler doesn't add any good perspective to the issue.
Was This Post Helpful? 1
  • +
  • -

#4 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Any error in my coding?

Posted 19 August 2014 - 09:35 AM

Tell us what the heck you're trying to do with this line, because I'm absolutely sure it's not what you want:

while(0>=(x, y, r)>=0)

Was This Post Helpful? 0
  • +
  • -

#5 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2170
  • View blog
  • Posts: 4,236
  • Joined: 30-May 10

Re: Any error in my coding?

Posted 19 August 2014 - 09:35 AM

> while(0>=(x, y, r)>=0)
Try to explain what you think this is doing.

Compare with say
while ( x >= 0 && y >= 0 && r >= 0 )
Was This Post Helpful? 0
  • +
  • -

#6 Deepak Kadam   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 19-August 14

Re: Any error in my coding?

Posted 19 August 2014 - 10:10 AM

View PostSalem_c, on 19 August 2014 - 10:05 PM, said:

> while(0>=(x, y, r)>=0)
Try to explain what you think this is doing.

Compare with say
while ( x >= 0 && y >= 0 && r >= 0 )


My statement means that x, y and r may be positive, negative or equal to zero while entering their respective values through keyboard. All this has worked in my other successful programs too!

Your statement means that x, y and r may only be positive or zero and not negative - that I don't want!
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14175
  • View blog
  • Posts: 56,782
  • Joined: 12-June 08

Re: Any error in my coding?

Posted 19 August 2014 - 10:18 AM

Quote

My statement means that x, y and r may be positive, negative or equal to zero while entering their respective values through keyboard. All this has worked in my other successful programs too!

What is the point of the condition if there is no boundaries on it?
Was This Post Helpful? 0
  • +
  • -

#8 Deepak Kadam   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 19-August 14

Re: Any error in my coding?

Posted 19 August 2014 - 10:19 AM

View PostJackOfAllTrades, on 19 August 2014 - 10:05 PM, said:

Tell us what the heck you're trying to do with this line, because I'm absolutely sure it's not what you want:

while(0>=(x, y, r)>=0)


It means that x, y and r may be positive, negative or equal to zero while entering their respective values through keyboard.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6293
  • View blog
  • Posts: 21,623
  • Joined: 05-May 12

Re: Any error in my coding?

Posted 19 August 2014 - 10:24 AM

You'll have to test each of those conditions separately. C and C++ don't follow mathematical notation/shorthand.

On the other side though, if a number can be positive, negative, or zero then you don't need to check for any conditions at all, since that is always true for any integer value stored in a variable, and generally true for most floating point variables -- excluding the case of NaN.
Was This Post Helpful? 0
  • +
  • -

#10 Deepak Kadam   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 19-August 14

Re: Any error in my coding?

Posted 19 August 2014 - 10:25 AM

View Postmodi123_1, on 19 August 2014 - 10:48 PM, said:

Quote

My statement means that x, y and r may be positive, negative or equal to zero while entering their respective values through keyboard. All this has worked in my other successful programs too!

What is the point of the condition if there is no boundaries on it?


Hey, I've already stated that I'm a beginner! Not even 1 month has passed since I have started writing such basic C++ programs.

I really don't know what are boundaries in C++. Would you please help me understand them?
And could you replace my statement with yours without changing its meaning?
I want x, y and r to be both +ve, -ve as well as zero.
Thank you.
Was This Post Helpful? 0
  • +
  • -

#11 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14175
  • View blog
  • Posts: 56,782
  • Joined: 12-June 08

Re: Any error in my coding?

Posted 19 August 2014 - 10:26 AM

Quote

And could you replace my statement with yours without changing its meaning?

Please be cognizant of the rules regarding asking people to do your work for you.
Was This Post Helpful? 0
  • +
  • -

#12 Deepak Kadam   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 19-August 14

Re: Any error in my coding?

Posted 19 August 2014 - 10:27 AM

View PostSkydiver, on 19 August 2014 - 10:54 PM, said:

On the other side though, if a number can be positive, negative, or zero then you don't need to check for any conditions at all, since that is always true for any integer value stored in a variable, and generally true for most floating point variables -- excluding the case of NaN.


I want my program to be in a loop i.e. never ending. Hence, the conditions!
Was This Post Helpful? 0
  • +
  • -

#13 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14175
  • View blog
  • Posts: 56,782
  • Joined: 12-June 08

Re: Any error in my coding?

Posted 19 August 2014 - 10:29 AM

You could easily just do 'while 1 does not equal 0'.
Was This Post Helpful? 0
  • +
  • -

#14 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6644
  • View blog
  • Posts: 30,937
  • Joined: 10-May 07

Re: Any error in my coding?

Posted 19 August 2014 - 10:30 AM

View PostDeepak Kadam, on 19 August 2014 - 01:27 PM, said:

I want my program to be in a loop i.e. never ending.

x=1;
while(x!=0) {
  cout<<"This is the loop that never ends...";
  //don't modify x
}


Was This Post Helpful? 1
  • +
  • -

#15 Deepak Kadam   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 19-August 14

Re: Any error in my coding?

Posted 19 August 2014 - 10:36 AM

View Postno2pencil, on 19 August 2014 - 11:00 PM, said:

View PostDeepak Kadam, on 19 August 2014 - 01:27 PM, said:

I want my program to be in a loop i.e. never ending.

x=1;
while(x!=0) {
  cout<<"This is the loop that never ends...";
  //don't modify x
}



Ohhkk. Got it.
Thanks.

[quote name='modi123_1' date='19 August 2014 - 10:56 PM' timestamp='1408469163' post='2043029']

Quote

Please be cognizant of the rules regarding asking people to do your work for you.


Hmm...
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2