Could really use some help

This is a discussion on Could really use some help within the C++ Programming forums, part of the General Programming Boards category; Hey everyone, I could really do with some help. I need to write a program that calculates the sum of ...

1. Could really use some help

Hey everyone,

I could really do with some help. I need to write a program that calculates the sum of two angles, in degrees, minutes & seconds. (60 secs to a minute, 60 mins to a degree, 360 degrees to a circle... I think I'm getting a headache...)

I'm really freaking out, I have no idea how to do this. Any help would be greatly appreciated.

Tarls.

2. i'll try, no promises though. what do you have so far?

3. HINT:

Create an angle struct that holds the data.

Overload the add operator to increment the next highest value when it reaches 60 ie if seconds ==60 ++minutes seconds=0

4. 104

5. If both angles are in degrees you can use integer divison and modulous

int deg,minutes,rdeg;
deg = angle1+angle2;
minutes=deg/60;
rdeg=deg % 60;
sec=minutes/60;
rminuts=minutes % 60;

I think somthing like this would work

6. why would a struct be better than a bunch of floats

7. Wow, that was quick.

<<Overload the add operator to increment the next highest value when it reaches 60 ie if seconds ==60 ++minutes seconds=0>>

Okay, I think I get that...

I've just started learning C++ so it takes a little while to process all this code! Is that the only way? For some reason I was trying to figure out how to do it with the modulus operator.

<<i'll try, no promises though. what do you have so far?>>
Ha, so far? A great big headache. lol. I can't figure out how to make it add right. ie if the user enters 74 29 13 as the first angle (in degrees minutes seconds) and 105 8 16 as the second, how in the name of god do I code it so that 74d 29' 13" + 105d 8' 16" = 179d 37' 29"?

Am I making any sense at all...?

Tarls.

8. <<If both angles are in degrees you can use integer divison and modulous >>

Ah, that's what I was trying to figure out!!

9. am i on the right track?

Code:
sum_deg=deg1+deg2;
sum_min=min1+min2;
if(sum_min>60)
{
sum_deg++;
sum_min=sum_min-60;
}
sum_sec=sec1+sec2;
if(sum_sec>60)
{
sum_min++;
sum_sec=sum_sec-60;
}

10. Hey, looks good to me! So that's a 'struct'?

11. this is what I got, I don't know if the math is right though

Code:
#include <iostream.h>

main()
{
float deg1, min1, sec1, deg2, min2, sec2, sum_deg, sum_min, sum_sec;
cout<<"what is degree1? ";
cin>>deg1;
cout<<endl<<"min1? ";
cin>>min1;
cout<<endl<<"sec1? ";
cin>>sec1;
cout<<endl<<"deg2";
cin>>deg2;
cout<<endl<<"min2";
cin>>min2;
cout<<endl<<"sec2";
cin>>sec2;
sum_deg=deg1+deg2;
sum_min=min1+min2;
if(sum_min>60)
{
sum_deg++;
sum_min=sum_min-60;
}
sum_sec=sec1+sec2;
if(sum_sec>60)
{
sum_min++;
sum_sec=sum_sec-60;
}

cout<<endl<<"answer= "<<sum_deg<<" degrees "<<sum_min<<" minutes "<<sum_sec<<" seconds ";
return(0);
}

12. no it's not a struct. I'm a nube myself and I don't know overloading (in fact I have a post up now about it). but i think this will work.

13. i think blight2c your program is ok except that the sec arithmetic should be done before minutes.

14. The non-bulletproofed input for degrees, minutes, and
seconds would be something like:

char inputString[9];

cout << "Input degrees, minutes, seconds, separated by
commas ";
gets(inputString);

int degTens, degOnes, degrees;

degTens = atoi(inputString(0)) * 10;
degOnes = atoi(inputString(1));
degrees = degTens + degOnes;

int minTens, minOnes, minutes;
minTens = atoi(inputString(3)) * 10;
minOnes = atoi(inputString(4));
minutes = minTens + minOnes;

int secTens, secOnes, seconds;
secTens = atoi(inputString(6)) * 10;
secOnes = atoi(inputString(7));
seconds = secTens + secOnes;

Obviously, this code is just off the top of my head. It
can be cleaned up significantly by reusing the "Tens" and
"Ones" variables, perhaps in a function, and you
definitely should use variables instead of numeric
constants in the calculations. I also forget which
standard header file you need to #include in order to make
use of the gets() function. And of course, this input
routine is not bulletproofed.

Miki
ustuzou@sympatico.ca

15. in C++ structs are the same as classes except the members are all declared public by default. Here's a sample struct that might mimic what you want to do:

Code:
//declare the user defined type
struct time
{
//data variables
int hours;
int min;
int sec;
//default constructor
time();
time & operator+(const time &);
};

//definitions for default constructor and overloaded + operator
time::time() : hours(0), min(0), sec(0)
{}

time & time::operator+(const time & rhs)
{
time result;
if(sec + rhs.sec > 59)
{
result.sec = 60 - (sec + rhs.sec);
result.min++;
}
else
result.sec = sec + rhs.sec;

if(min + rhs.min > 59)
{
result.min = 60 - (min + rhs.min);
result.hour++;
}
else
result.min = min + rhs.min;

result.hour = hour + result.hour;

return result;
}

int main()
{
//declare instances of user defined type
//data variables will be initialized using the default constructor
time time1;
time time2;
time time3;

//hard coded values for time variables used here
time1.sec = 36;
time1.min = 49;
time1.hour = 1;
time2.sec = 44;
time2.min = 0;
time2.hour = 90;

//call + operator on time1 passing time2 and assigning returnv
//value to time3
time3 = time1 + time2;

//show that this did something useful, no matter how trivial
cout << "result = " << time3.hour << ':' << time3.min << ':' <<
time3.sec;

return 0;
}
This can be improved in any number of ways. You should validate all user input, if any. You could overload the << operator. You would need to list approriate header files, etc. But it gives an idea of how structs/classes with overloaded operators might be used to solve the problem.

Page 1 of 2 12 Last