| | |
Adding two angles, need some help
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 2
Reputation:
Solved Threads: 0
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.
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)
#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"); }
Last edited by Ancient Dragon; Dec 5th, 2008 at 9:41 am. Reason: add code tags
•
•
Join Date: Aug 2008
Posts: 77
Reputation:
Solved Threads: 16
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..
C++ Syntax (Toggle Plain Text)
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
Last edited by cikara21; Dec 5th, 2008 at 3:03 am.
![]() |
Other Threads in the C++ Forum
- Previous Thread: I'm getting the wrong output when I run this program?
- Next Thread: Array/Function problems
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





