943,869 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 634
  • C++ RSS
Dec 5th, 2008
0

Adding two angles, need some help

Expand Post »
I had to write a program to add two angles together. I've got the meat done, but i'm having a hard time figuring out how to code the program when i add the angles together so that when say Seconds is more than 60 it adds 1 to minutes.

example: 7d 14m 55s + 5d 24m 55s = 12d 39m 50s

The way i have it coded now my answer comes out as 12d 38m 110s. I'll need to be able to code minutes the same way if the total runs over 60m.
C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<cmath>
  4. using namespace std;
  5.  
  6. int main()
  7.  
  8. {
  9. //input variables
  10. int degrees1,
  11. degrees2,
  12. minutes1,
  13. minutes2,
  14. seconds1,
  15. seconds2;
  16.  
  17. //new angle variables
  18. int degrees3,
  19. minutes3,
  20. seconds3;
  21.  
  22. //holds degrees and minutes converted to seconds
  23. long dsec1,
  24. dsec2,
  25. dsec3,
  26. minsec1,
  27. minsec2,
  28. minsec3;
  29.  
  30.  
  31. begin_loop:
  32. cout << "Enter the first angle in degrees (d), then minutes (m), then seconds (s) \n";
  33.  
  34. cin >> degrees1 >> minutes1 >> seconds1;
  35.  
  36. if ((degrees1 <= 360) && (minutes1 < 60) && (seconds1 < 60))
  37. {
  38. cout << "The first angle entered is " << degrees1 << "d " << minutes1 << "m " << seconds1 << "s \n";
  39. }
  40.  
  41. else
  42.  
  43. {
  44. if ((degrees1 >= 360) || (minutes1 >=60) || (seconds1 >=60))
  45. {
  46. cout << "Degrees must be 0 to 360, minutes must be 0 to 60, and seconds must be 0 to 60 \n";
  47. cout << endl;
  48. goto begin_loop;
  49. }
  50. }
  51.  
  52. begin_loop2:
  53. cout << "Enter the second angle in degrees (d), then minutes (m), then seconds (m) \n";
  54.  
  55. cin >> degrees2 >> minutes2 >> seconds2;
  56.  
  57. if ((degrees2 <= 360) && (minutes2 <= 60) && (seconds2 <= 60))
  58. {
  59. cout << "The second angle entered is " << degrees2 << "d " << minutes2 << "m " << seconds2 << "s \n";
  60. }
  61.  
  62. else
  63.  
  64. {
  65. if ((degrees2 >= 360) || (minutes2 >=60) || (seconds2 >=60))
  66. {
  67. cout << endl;
  68. cout << "Degrees must be 0 to 360, minutes must be 0 to 60, and seconds must be 0 to 60 \n";
  69. cout << endl;
  70. goto begin_loop2;
  71. }
  72. }
  73.  
  74. cout << endl;
  75.  
  76. //converting degrees and minutes to seconds
  77. dsec1 = degrees1*3600;
  78. dsec2 = degrees2*3600;
  79.  
  80. minsec1 = minutes1*60;
  81. minsec2 = minutes2*60;
  82.  
  83. //new angle calc
  84. dsec3 = (dsec1 + dsec2);
  85. minsec3 = (minsec1 + minsec2);
  86. seconds3 = (seconds1 + seconds2);
  87.  
  88. //Converting new angle from seconds to degrees, minutes, and seconds
  89. degrees3 = (dsec3/3600);
  90. minutes3 = (minsec3/60);
  91. seconds3 = seconds3;
  92.  
  93. cout << endl;
  94.  
  95. //display output
  96. cout << degrees1 << "(d) " << minutes1 << "(m) " << seconds1 << "(s) " << "+ ";
  97. cout << degrees2 << "(d) " << minutes2 << "(m) " << seconds2 << "(s) " << "= ";
  98. cout << degrees3 << "(d) " << minutes3 << "(m) " << seconds3 << "(s)\n";
  99.  
  100.  
  101.  
  102.  
  103.  
  104. system("pause");
  105.  
  106. }
Last edited by Ancient Dragon; Dec 5th, 2008 at 9:41 am. Reason: add code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Jwhispers is offline Offline
2 posts
since Dec 2008
Dec 5th, 2008
0

Re: Adding two angles, need some help

To add two times as you have listed, first add the seconds together. If the seconds > 59 you will have to use modulus (%) to find the remainder of the seconds. You can then use this information to figure out what the actual seconds are and that a minute carries over. Now repeat with minutes and days.
Reputation Points: 33
Solved Threads: 18
Junior Poster in Training
mahlerfive is offline Offline
77 posts
since Aug 2008
Dec 5th, 2008
0

Re: Adding two angles, need some help

Like this..
C++ Syntax (Toggle Plain Text)
  1. current_deg
  2. current_min
  3. current_sec
  4.  
  5. if(current_sec >= 60) then
  6. begin
  7. current_min++
  8. current_sec=(current_sec-60)
  9. end
  10.  
  11. if(current_min>=60) then
  12. begin
  13. current_deg++
  14. current_min=(current_min-60)
  15. end
Last edited by cikara21; Dec 5th, 2008 at 3:03 am.
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Dec 5th, 2008
0

Re: Adding two angles, need some help

Ah, ok thank you.

Edit:

I adapted that to a while loop,

while (seconds3 >= 60)
{
minutes3++;
seconds3 = (seconds3-60);
}

while(minutes3 >=60)
{
degrees3++;
minutes3 = (minutes3-60);
}

thanks again for your time
Last edited by Jwhispers; Dec 5th, 2008 at 3:29 am. Reason: show my results
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Jwhispers is offline Offline
2 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: I'm getting the wrong output when I run this program?
Next Thread in C++ Forum Timeline: Array/Function problems





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC