Angles calculator help

Hi everyone this is my first post here on cplusplus.com's forums and I should be visting back here and posting quite often. I am currently taking an intro to C++ programming course and I am in my third week of class and I got an assignment that is due soon and I am pretty stumped on it. I need to make a program that asks the user enter a first angle (degrees, minutes, and seconds) and a second angle (degrees, minutes, and seconds) and then to calculate the sum of the two angles. For example:


20° 31' 19" + 0° 31' 30" = 21° 2' 49"

I have been able to make the past two programs but this one unfortunately is very confusing to me. I am no way asking for someone to write a program for me but if someone is willing to offer help for me on how to start this program and how to input and output this correctly, it would be greatly appreciated. After I receive some advice on how to start this I will post my work and go from there.

P.S. I would ask my teacher for more help on this but it is only a once a week class and is very hard to receive the help I need on this in just one day.

Thanks everyone.

the simplest way to start would just be to convert all numbers to seconds, do your sum, then convert back.

For the conversion this might help you:
http://zonalandeducation.com/mmts/trigonometryRealms/degMinSec/degMinSec.htm
Remember. : 1st Show Your Effort on Coding .. We are not sitting here to do your homework/assignments

Assignments/Homework are given for your own benefit to learn ..
This is what I have so far. I have run into a problem though, whenever it gives the answer all it does is add deg1 and deg2 and not the whole entire set of numbers. How do I fix this so that it adds all of my variables and outputs them all?

Thanks


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
//calculator to add angles
#include <iostream>

using namespace std;

int main()
{
    int deg1, min1, sec1, deg2, min2, sec2, angle;
    
    cout<<"Enter your first angle seperated by spaces: ";
    cin>>deg1;
    cin>>min1;
    cin>>sec1;
    
    cout<<"Enter your second angle seperated by spaces: ";
    cin>>deg2;
    cin>>min2;
    cin>>sec2;
    
    angle= deg1+deg2, min1+min2, sec1+sec2;
    
    cout<<angle<<endl;
    
    
    system("PAUSE");
    
    return 0;
}
I apologize for not adding a code to begin with but this assignment really stumped me. I tried as hard as I could to make a start to this and this is all I could come up with.
Well that's a decent start I suppose, although remember that you need to check that when the numerator of an angle value exceeds it's denominator then you have to +1 (or however many) onto the next step up instead.

I think this is a little unclear with my explanation so here's an example.
1o 47' 24"
3o 12' 39"         +
------------------------
= 3o 59' 63"
= 3o 60' 3"
= 4o 0' 3"


Just a load of ifs could get you through if you setup the arithmetic correctly afterwards
Last edited on
Hi bknick24,

On line 20 the comma's don't do what you think. You need to assign the results of the additions to variables.

As well as that the assignment is a bit trickier - simply adding up deg, min, sec values is not the way to do it. You have to take into account that there are 60 secs in a min & 60 mins in a degree - so what to do when they get exceeded?
Yea I actually just ran into the problem you just mentioned SatsumaBenji. I have corrected my mistake on my addition and outputting all of my variables but I can not figure out how to carry over to the next variable, for example when my seconds adds up to 73, I would need to carry over the +1 to my minutes. How do I go up doing this? I believe my teacher mentioned the "%" symbol can help fix this?

Here is my code now:

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
#include <iostream>

using namespace std;

int main()
{
    int deg1, min1, sec1, deg2, min2, sec2, angle, angle2, angle3;
    
    cout<<"Enter your first angle seperated by spaces: ";
    cin>>deg1;
    cin>>min1;
    cin>>sec1;
    
    cout<<"Enter your second angle seperated by spaces: ";
    cin>>deg2;
    cin>>min2;
    cin>>sec2;
    
    angle= deg1+deg2;
    angle2= min1+min2;
    angle3= sec1+sec2;
    
    cout<<angle<<"°"<<angle2<<"'"<<angle3<<"''"<<endl;
    
    
    system("PAUSE");
    
    return 0;
}
Anyone have any ideas to fix my carry-over problem from seconds-minutes-degrees?

Thank you for the input so far.
Like SatumaBenji said, you want to use some if statements. Do this before you output your angle, of course. Just remember to work from angle3 up.

Post what you think and I'll help your thinking.
Last edited on
I tried reading some if statements in the book from class but we have yet to learn about it in class.

would something like this work:

1
2
if (angle3 >= 60)
   angle2 = angle2 + 1


Please let me know if something like this would work, there is not really a similar example in my book.

Thank you for the reply.
Yes, that is the right train of thought, but you are going to have to reduce angle3 to under 60. Add a line to do that. Use {} to surround your commands after the if statement. Don't forget that angle2 can be higher and need to be carried and reduced.
Actually now that I think about it, I think a while loop would do better, just incase you have really large additions that carry 2 or something...

1
2
3
4
while(angle3 >= 60){
    angle2++;    //short for "angle2 = angle2 + 1"
    angle3 -= 60;    //short for "angle3 = angle3 - 60"
}


A while loop is basically the same thing as an if statement but once it hits the end of the loop it returns to the to the test statement, if the test is still true then what's inside the {curly braces} get executed again, and this keeps going until the test statement returns false (or if it is false when the loop is first come across then none of the content is executed)

EDIT: btw the modulus operator, %, gives you the remainder of a division, so for example 73 % 60 would give you 13, which is the value that should appear instead of 73 but it does not help you figure out how much to add on to the next number if there are very large carries.
Just use the looped tests and +/-.
Last edited on
Thanks Satsuma, that actually helped me figure it out!

Got it complete!
Topic archived. No new replies allowed.