Having trouble with Division

I was trying to make a very simple application that would help you find the Sine of an angle, but I cant get it to work.

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
/*This program is to help you figureout the Sine of a triangle. It
uses division to find a result.*/

#include <iostream>

int main()

{
using std::cout;
using std::cin;
using std::endl;

cout << "\n";
cout << "  Imagine you have a Right Triangle and you want to find the Sine.\n";
cout << "First you would need to label all of the angles. Lets say the Right\n";
cout << "angle is A, the acute angle is B(Your Sine angle), and the last\n";
cout << "angle is C. Sine = measure of the leg opposite angle B / measure of\n";
cout << "the hypotenuse.\n";

unsigned short MeasureOfLegOpposite;
float MeasureOfTheHypotenuse;
unsigned short Sine = (MeasureOfLegOpposite / MeasureOfTheHypotenuse);

cout << "\nWhat is the measure of the leg opposite your angle? ";
cin >> MeasureOfLegOpposite;

cout << "\nAnd what is the measure of the hypotenuse? ";
cin >> MeasureOfTheHypotenuse;

cout << "\n";

cout << "So the Sine of angle B is: ";
cout << Sine;
cout << "\n\n";
cout << "Thanks!";

int exit;
cin >> exit;

return 0;
}



When I type in my numbers it never works. Its just says the samething every time. The division worked when I substituted the cin >> with pre set numbers. Thats whats confussing.
Last edited on
Move the line:
unsigned short Sine = (MeasureOfLegOpposite / MeasureOfTheHypotenuse);

To after you write :
cin >> MeasureOfTheHypotenuse;

What you are doing is you are trying to find the value of sine before you get the values of LegOpposite and Hypotenuse from the user.
Yes, your sine variable isn't dynamic, it's static meaning that when you create it it's set to what the values you are dividing are at the time. It won't work like that, so move it to after you're finished calculating, personally I think it makes more sense to do it like this:

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
/*This program is to help you figureout the Sine of a triangle. It
uses division to find a result.*/

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{

unsigned short MeasureOfLegOpposite = 0, Sine = 0;
float MeasureOfTheHypotenuse = 0.0;

cout << "\n";
cout << "  Imagine you have a Right Triangle and you want to find the Sine.\n";
cout << "First you would need to label all of the angles. Lets say the Right\n";
cout << "angle is A, the acute angle is B(Your Sine angle), and the last\n";
cout << "angle is C. Sine = measure of the leg opposite angle B / measure of\n";
cout << "the hypotenuse.\n";

cout << "\nWhat is the measure of the leg opposite your angle? ";
cin >> MeasureOfLegOpposite;

cout << "\nAnd what is the measure of the hypotenuse? ";
cin >> MeasureOfTheHypotenuse;

cout << "\n";

Sine = (MeasureOfLegOpposite / MeasureOfTheHypotenuse); // Assign the value to sign 

cout << "So the Sine of angle B is: " << Sine << "\n\nThanks!";

cin.get(); // You don't need to use cin, cin.get() will read until newline/return/enter is pressed

return 0;
}
Topic archived. No new replies allowed.