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.

#include<iostream>
#include<cstdlib>
#include<cmath>
using namespace std;

int main()

{
      //input variables
      int degrees1, 
          degrees2, 
          minutes1, 
          minutes2, 
          seconds1, 
          seconds2; 
          
      //new angle variables       
      int degrees3,
          minutes3,
          seconds3;
      
      //holds degrees and minutes converted to seconds
      long dsec1, 
           dsec2, 
           dsec3, 
           minsec1, 
           minsec2, 
           minsec3;  
          
      
      begin_loop:
      cout << "Enter the first angle in degrees (d), then minutes (m), then seconds (s) \n";
      
      cin >> degrees1 >> minutes1 >> seconds1;
      
      if ((degrees1 <= 360) && (minutes1 < 60) && (seconds1 < 60))
         {
           cout << "The first angle entered is " << degrees1 << "d " << minutes1 << "m " << seconds1 << "s \n";
         }
              
      else
      
         {
           if ((degrees1 >= 360) || (minutes1 >=60) || (seconds1 >=60))
                 {
                   cout << "Degrees must be 0 to 360, minutes must be 0 to 60, and seconds must be 0 to 60 \n";
                   cout << endl;
                   goto begin_loop;
                 }
         }

      begin_loop2: 
      cout << "Enter the second angle in degrees (d), then minutes (m), then seconds (m) \n";
      
      cin >> degrees2 >> minutes2 >> seconds2;
      
      if ((degrees2 <= 360) && (minutes2 <= 60) && (seconds2 <= 60))
         {
           cout << "The second angle entered is " << degrees2 << "d " << minutes2 << "m " << seconds2 << "s \n";
         }
              
      else
      
         {
           if ((degrees2 >= 360) || (minutes2 >=60) || (seconds2 >=60))
                 {
                   cout << endl;
                   cout << "Degrees must be 0 to 360, minutes must be 0 to 60, and seconds must be 0 to 60 \n";
                   cout << endl;
                   goto begin_loop2;
                 }
         }
      
      cout << endl;
      
       //converting degrees and minutes to seconds
      dsec1 = degrees1*3600;
      dsec2 = degrees2*3600;
      
      minsec1 = minutes1*60;
      minsec2 = minutes2*60;
      
      //new angle calc
      dsec3 = (dsec1 + dsec2); 
      minsec3 = (minsec1 + minsec2);
      seconds3 = (seconds1 + seconds2);
      
      //Converting new angle from seconds to degrees, minutes, and seconds
      degrees3 = (dsec3/3600);
      minutes3 = (minsec3/60);
      seconds3 = seconds3;
      
      cout << endl;
      
      //display output
      cout << degrees1 << "(d) " << minutes1 << "(m) " << seconds1 << "(s) " << "+ ";
      cout << degrees2 << "(d) " << minutes2 << "(m) " << seconds2 << "(s) " << "= ";
      cout << degrees3 << "(d) " << minutes3 << "(m) " << seconds3 << "(s)\n";
      
      
      
      
      
      system("pause");
      
}

Recommended Answers

All 3 Replies

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.

Like this..

current_deg
current_min
current_sec
 
if(current_sec >= 60) then
begin
   current_min++
   current_sec=(current_sec-60)
end

if(current_min>=60) then
begin
   current_deg++
   current_min=(current_min-60)
end

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.