Any error in my coding?

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

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#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();
} 
You divide by r on lines 16,17... and divide by x,y a few lines below that. This is before you check to see if they are zero. So at this point, they might be zero.

Furthermore... in your loop.... yes, you check to see if r is zero and will print "NOT DEFINED" if it is zero (on line 26)... but that will not stop you from dividing by it (which you do on line 81)... which will happen regardless of whether or not r is zero.


Also:

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

This does not do what you think. General rule of thumb is to never use commas in conditionals.

In fact, this condition is the same as while(r != 0). The values of x,y are completely ignored.



EDIT:

Also... main() should return an int... not a void.

Also, it should be <iostream>, not <iostream.h> (though you'll probably need to add using namespace std;)
Last edited on
So, will changing while(0>(x, y, r)>0) to while(r!=0) solve my problem? I'll try. But this kind of thing worked in my other programs.

Also, I am using Borland C++ 4.5
In this, all my other programs work without the namespace, int main(), iostream, etc. kinds of stuffs that you have mentioned in your reply.

Thanks for the quick reply, Disch :)

[ BTW, I did not get what you said in first two paras :( ]
Last edited on
So, will changing while(0>(x, y, r)>0) to while(r!=0) solve my problem?


No. I was saying those two statements are identical.

See the rest of my post -- I explain where you are dividing by zero.

In this, all my other programs work without the namespace, int main(), iostream, etc. kinds of stuffs that you have mentioned in your reply.


I'm just telling you how the language works. main() must return an int, <iostream> is the standard header, and cout, etc are in the std namespace.

Whether or not your version of your compiler is standards compliant is another issue.

My advice is learn to do things the right way... that way it'll always work. The code you wrote won't compile on some newer compilers which more closely follow the standard.


[ BTW, I did not get what you said in first two paras :( ]


Take a look at line 16:

 
sinn=y/r;


You are dividing by 'r' here. If r is zero... then you are dividing by zero. There is no if statement which prevents the division if r is zero.


Look at line 81:

 
sinn=y/r;


Again... you are dividing by r. If r is zero, then you are dividing by zero. Your if statement on line 24 will print a different message... but it does not stop this division because this division is outside the {braces} of that if statement.



It's also worth noting that dividing by zero (when zero is a floating point value) is not illegal and should not produce an error. This + your outdated headers tells me you probably should update your compiler. Borland 4.5 is quite literally 20 years old. That's a dinosaur.

Take a look at line 16:


sinn=y/r;


You are dividing by 'r' here. If r is zero... then you are dividing by zero. There is no if statement which prevents the division if r is zero.


Look at line 81:


sinn=y/r;


Again... you are dividing by r. If r is zero, then you are dividing by zero. Your if statement on line 24 will print a different message... but it does not stop this division because this division is outside the {braces} of that if statement.


OK. So what's the latest compiler? Is it Borland C++ Compiler v5.5?

So, how can I prevent the function being divided by zero?
Is it possible to state a condition after declaring a formula? ex. tann=y/x;

Thank you.
OK. So what's the latest compiler? Is it Borland C++ Compiler v5.5?


I don't know about Borland, but the 3 main compilers available on Windows are:
- Microsoft Visual Studio
- GCC
- Clang

All 3 of them are free and available from a google search.

Is it possible to state a condition after declaring a formula?


You don't declare formulas. So no. When you put in a formula... that is where the program will actually do the computation.

Don't actually put in the formula until you want to actually do the division.

If you want to do the division only if the denominator is nonzero.... then put it in an if statement:

1
2
3
4
5
6
if(r != 0)
{
    sinn = y/r; // <- will only be done if r is not zero
}

// <- at this point, it doesn't matter whether or not r is zero anymore. 
Last edited on
Thank you very much Disch! I'll give it a try and report back if any progress takes place!

And I'm thinking of buying Visual Studio 2013. Is it a good idea?
And I'm thinking of buying Visual Studio 2013. Is it a good idea?


No.

Not unless you have money to throw away. Get the free version. Odds are you are not going to need any features the pay version offers.
K. Well, is it possible to write such basic C++ programs in Visual Studio? I mean using the main(), namespace std, return 0, etc.
Yes.

Just make a normal "Empty C++ Project" and don't use any of the premade templates VS throws in your face.
There's a lot of duplicated code in your program. Lines 7-21 appear to be the same as lines 72-86. This is always a indication that you should restructure the code or move the common code to a function.

The same is true of your output. You could put that in a function with parameters for the bits that change:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void doOutput(const char *fname, double angle, double value, double checkVal)
{
	cout<< fname << "("<<angle<<")=";
	if(checkval==0.0) {
		 cout << "NOT DEFINED";
	} else {
		cout<< value;
	}
	cout << endl << endl;
}

...
doOutput("sin", angle, sinn, r);
doOutput("cos", angle, cosn, r);
doOutput("tan", angle, tann, x);
doOutput("cot", angle, cotn, y);
doOutput("sec", angle, secn, x);
doOutput("cosec", angle, cosecn, y);


Lastly, note that sometimes these values are infinite and sometimes they are undefined. If y is positive and x is zero then the tangent is infinity, not undefined. The floating point system handles these cases and it can return infinity, negative infinity or NaN (not a number). The latter is for things like 0/0.
What is doOutput? I haven't seen such coding anywhere and I have not been taught any such thing.

I have re-written the same formulas (lines) to maintain a loop in my program.
If I don't then my program won't ask for next values once it has executed the previous one or it'll become inactive thereafter.

Still, thanks. I'll try to understand it.
What is doOutput? I haven't seen such coding anywhere and I have not been taught any such thing.

Just to be clear - are you saying that you've literally never seen functions before?
Just to be clear - are you saying that you've literally never seen functions before?


I am beginner - keeping this in mind anyone can guess how much do I know!
Just one month has passed since I have started learning about C++. We are taught only about the basic operators at least for now. The real and important topics are yet to start. In fact, I have written the above program on my own with the help of various articles available on the net. My friends get surprised even when they see such not-so-long programs!

Anyway, I have lots of other programs to write rather than getting stuck on the same!

The fact is that "length can never be ZERO or -ve in a triangle." Hence, the question of entering a value ≤ 0 does not arise! This program works well if no such values are entered! Yo may test it on your own.
Last edited on
Yippeeeee!!!! I've got my program to work even when I enter '0' for any of the variable!!
This is the code:-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include<iostream.h>
#include<conio.h>
void main()
{
	double x, y, r, angle;
	double sin, cos, tan, cot, sec, cosec;
	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();
	if(r!=0)
	{
	   sin=y/r;
	}
	if(r!=0)
	{
	   cos=x/r;
	}
	if(x!=0)
	{
	   tan=y/x;
	}
	if(y!=0)
	{
	   cot=x/y;
	}
	if(x!=0)
	{
	   sec=r/x;
	}
	if(y!=0)
	{
	   cosec=r/y;
	}
	while(0>=(x, y, r)>=0)
	{
		if(r==0)
		{
		  cout<<"sin("<<angle<<")= NOT DEFINED"<<endl<<endl;
		}
		else
		{
		  cout<<"sin("<<angle<<")= "<<sin<<endl<<endl;
		}
		if(r==0)
		{
		  cout<<"cos("<<angle<<")= NOT DEFINED"<<endl<<endl;
		}
		else
		{
		  cout<<"cos("<<angle<<")= "<<cos<<endl<<endl;
		}
		if(x==0)
		{
		  cout<<"tan("<<angle<<")= NOT DEFINED"<<endl<<endl;
		}
		else
		{
		  cout<<"tan("<<angle<<")= "<<tan<<endl<<endl;
		}
		if(y==0)
		{
		  cout<<"cot("<<angle<<")= NOT DEFINED"<<endl<<endl;
		}
		else
		{
		  cout<<"cot("<<angle<<")= "<<cot<<endl<<endl;
		}
		if(x==0)
		{
		  cout<<"sec("<<angle<<")= NOT DEFINED"<<endl<<endl;
		}
		else
		{
		  cout<<"sec("<<angle<<")= "<<sec<<endl<<endl;
		}
		if(y==0)
		{
		  cout<<"cosec("<<angle<<")= NOT DEFINED"<<endl<<endl;
		}
		else
		{
		  cout<<"cosec("<<angle<<")= "<<cosec<<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();
		if(r!=0)
	        {
		   sin=y/r;
	        }
	        if(r!=0)
	        {
		   cos=x/r;
	        }
	        if(x!=0)
	        {
		   tan=y/x;
	        }
	        if(y!=0)
	        {
		   cot=x/y;
	        }
	        if(x!=0)
	        {
		   sec=r/x;
	        }
	        if(y!=0)
	        {
		   cosec=r/y;
	        }
	}
getch();
}


Thank Disch!!! Very much! I have re-written this code according to your advice i.e.

You don't declare formulas. So no. When you put in a formula... that is where the program will actually do the computation.

Don't actually put in the formula until you want to actually do the division.

If you want to do the division only if the denominator is nonzero.... then put it in an if statement:
1
2
3
4
5
6
if(r != 0)
{
    sinn = y/r; // <- will only be done if r is not zero
}

// <- at this point, it doesn't matter whether or not r is zero anymore.  



Thank you once again Disch!! :D
Topic archived. No new replies allowed.