Adding two angles, need some help

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2008
Posts: 2
Reputation: Jwhispers is an unknown quantity at this point 
Solved Threads: 0
Jwhispers Jwhispers is offline Offline
Newbie Poster

Adding two angles, need some help

 
0
  #1
Dec 5th, 2008
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.
  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 77
Reputation: mahlerfive is an unknown quantity at this point 
Solved Threads: 16
mahlerfive mahlerfive is offline Offline
Junior Poster in Training

Re: Adding two angles, need some help

 
0
  #2
Dec 5th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 338
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 65
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: Adding two angles, need some help

 
0
  #3
Dec 5th, 2008
Like this..
  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.
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 2
Reputation: Jwhispers is an unknown quantity at this point 
Solved Threads: 0
Jwhispers Jwhispers is offline Offline
Newbie Poster

Re: Adding two angles, need some help

 
0
  #4
Dec 5th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC