Thread: Could really use some help

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    12

    Unhappy 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.

    Thanks in advance,
    Tarls.

  2. #2
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    i'll try, no promises though. what do you have so far?

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    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. #4
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    104

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    52
    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. #6
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    why would a struct be better than a bunch of floats

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    12
    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. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    12
    <<If both angles are in degrees you can use integer divison and modulous >>

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

  9. #9
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    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. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    12
    Hey, looks good to me! So that's a 'struct'?

  11. #11
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    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. #12
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    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. #13
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    i think blight2c your program is ok except that the sec arithmetic should be done before minutes.
    -

  14. #14
    Registered User
    Join Date
    Feb 2002
    Posts
    16
    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
    [email protected]

  15. #15
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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();
      //overloaded addition operator
      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.

Popular pages Recent additions subscribe to a feed