Thread: Program doesn't always do whats expected... any ideas? (newbie)

  1. #1
    photoresistor
    Guest

    Program doesn't always do whats expected... any ideas? (newbie)

    Alright, I'm new to the programming world... I've started learning by reading some online tutorials and the book 'C++ Primer'.

    I've just gone over some sections on functions and thought I'd make a little program of my own to test out and try out some ideas and get used to functions.

    As I'm in Trigonometry right now I thought it'd be fun to have a program convert angle measures in degrees (like 42.35 degrees) into degrees and minutes and seconds (42 degrees 21').

    I did it and for most problems it seems to work but for this problem it does not:

    42.35 degrees

    instead of the 42 degrees and 21 minutes it should solve to it comes up w/ 42 degrees 20 minutes and 59 seconds. ??

    Here is the code:

    #include <stdlib.h>
    #include <iostream>
    using namespace std;

    int degr(float input);
    int minu(float input);
    int seco(float input);

    int degr(float input)
    {
    int degrees = input;
    return degrees;
    }

    int minu(float input)
    {
    int in = input;
    float deci = input - in;
    int minutes = deci*60;
    return minutes;
    }

    int seco(float input)
    {
    int in = input;
    float deci = input - in;
    float semi = deci*60;
    int minutes = semi;
    float b = semi - minutes;
    int seconds = b*60;
    return seconds;
    }

    int main()
    {

    float input = 0;
    int degrees = 0;
    int minutes = 0;
    int seconds = 0;

    cout << "Enter measure of angle in degrees for conversion to minutes and seconds: ";
    cin >> input;

    degrees = degr(input);
    minutes = minu(input);
    seconds = seco(input);

    cout << "\n\nMeasure of angle in minutes and seconds: " << degrees << " degrees " << minutes << " minutes " << seconds << " seconds" << endl << endl;


    system("PAUSE");
    return 0;
    }

    I know its pretty messy now and that a lot of my methods are probably bad but thats all i know how to do right now.

    any help is appreciated.

    if it matters im running XP using bloodshed's dev-c++ compiler.

    oh and after reading the thread at the top i thought id better say that this is not homework or anything... im learning it on my free time and this is just a little program of mine i thought id make. like trying to apply the stuff ive learned you know?

    thanks!

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    3
    *tried to edit post but i wasn't registered before and wont let me edit it now*

    Added comments for clarification:

    #include <stdlib.h>
    #include <iostream>
    using namespace std;

    int degr(float input);
    int minu(float input);
    int seco(float input);

    int degr(float input)
    {
    int degrees = input; // to drop the decimels
    return degrees;
    }

    int minu(float input) //equation to get minutes, using int to drop rest of decimels
    {
    int in = input;
    float deci = input - in;
    int minutes = deci*60;
    return minutes;
    }

    int seco(float input) /* same thing but for seconds... bad thing for this is i dont know how to round to closest integer in programming so it just truncates*/
    {
    int in = input;
    float deci = input - in;
    float semi = deci*60;
    int minutes = semi;
    float b = semi - minutes;
    int seconds = b*60;
    return seconds;
    }

    int main()
    {

    float input = 0;
    int degrees = 0;
    int minutes = 0;
    int seconds = 0;

    cout << "Enter measure of angle in degrees for conversion to minutes and seconds: ";
    cin >> input;

    degrees = degr(input);
    minutes = minu(input);
    seconds = seco(input);

    cout << "\n\nMeasure of angle in minutes and seconds: " << degrees << " degrees " << minutes << " minutes " << seconds << " seconds" << endl << endl;

    system("PAUSE");
    return 0;
    }



    *END*

    EDIT2: if you dont know, convert degrees to minutes/seconds:

    1. subtract whole number and then multiply by 60 (gets you number of minutes)

    2. subtract whole number of minutes and multipaly 60 to get number of seconds.
    Last edited by photoresistor; 12-06-2002 at 07:16 PM.

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Almost certainly a rounding error - change your floats to doubles and see if that helps.

    And I'm not sure if there is a math function that rounds numbers properly, but its easy to write one, just do something like:
    Code:
    int round(float x)
    {
        if (x-int(x)>=.5)
            return int(x)+1;
        else
            return int(x);
    }

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    3
    EDIT: Thanks for the reply! must sound like im really impatient huh? sorry. will try that.

    Okay, I've cleaned up my code just a tiny bit but it seems as though no one is interested in helping me out here?

    So how about instead of asking you to go through all that (nasty) newbie's code I ask you a better way to truncate a float type variable to the decimal point?

    I've just been doing it just by converting it to an int type but its really messy and complicates things up. Maybe its the source of my problem as well?

    So anyone know a better way to do this? Any responce appreciated much. Thanks!

    Here is the cleaner code:

    Code:
    #include <stdlib.h>
    #include <iostream>
    using namespace std;
    
    int deg(float input);
    int min(float input);
    int sec(float input);
    
    int deg(float input)
    {
     int degrees = input; //conver to int to truncate
     return degrees;
    }
    
    int min(float input) // same thing, formula for minutes
    {
     int in = input;
     float deci = input - in;
     int minutes = deci*60;
     return minutes;
    }
    
    int sec(float input) //same, get seconds
    {
     int in = input;
     float deci = input - in;
     float semi = deci*60;
     int minutes = semi;
     float b = semi - minutes;
     int seconds = b*60;
     return seconds;
    }
    
    int main()
    {
     float input = 0;
     
     cout << "Enter measure of angle in degrees: ";
     cin >> input;
    
     cout << endl << deg(input) << " degrees " << min(input) << "' " << sec(input) << "'' " << endl << endl;
    
     system("PAUSE");
     return 0;
    }
    Again... thankyou.... in advance....

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    3
    Okay, I did your suggestion and it worked out. Thanks for the help.

    Just thought I'd post in the code... I added in a little menu system too.

    Code:
    //**************************************|
    //  Trig.cpp                            |
    //  convert degree decimels to minutes  |
    //  and seconds using functions.        |
    //  use rounding and truncating func.   |
    //                                      |
    //**************************************|
    
    #include <stdlib.h>
    #include <iostream>
    using namespace std;
    
    double deg(double input);       // calculate the degrees (just truncate to whole number)
    double min(double input);       // calculate the minutes (truncate whole # & x60 and truncate) 
    double sec(double input);       // calculate seconds (truncate minute whole # & x60 and round)
    int round(double seconds);      // round the seconds decimels
    double deci(double x);          // function to truncate the whole number portion of the number
    int display();                  // function to get data and report data to user
    
    double deg(double input)        // convert double input to type int to truncate after decimel
    {
     int degree = input;
     double degrees = degree;
     return degrees;
    }
    
    double min(double input)        // have function 'deci()' truncate whole number part and then times that by 60
    {
     double in = deci(input);
     int min = in*60;
     double minutes = min;
     return minutes;
    }
    
    double sec(double input)        // 'deci()' to get minutes and then 'deci()' again to get seconds (times 60)
    {
     double in = deci(input);
     input = in*60;
     double mi = deci(input);
     double seconds = mi*60;
     int seco = round(seconds);
     double sec = seco;
     return sec;
    }
    
    double deci(double input)               // take the double minus the int so you're left w/ .something
    {
      double real = input - int(input);
      return real;
    }
      
    int round(double seconds)               // round the seconds
    {
        if (seconds-int(seconds)>=.5)
            return int(seconds)+1;
        else
            return int(seconds);
    }
    
    int display()
    {
      double input = 0;
      int choice = 0;
     
      cout << "\nEnter measure of angle in degrees: ";
      cin >> input;
    
      cout << endl << deg(input) << " degrees " << min(input) << "' " << sec(input) << "'' " << endl;
      cout << "\n\nPress '1' to do another. Press '2' to quit program. ";
      cin >> choice;
      
      if(choice == 1)
        return choice;
      if(choice == 2)
        return 0;
      else
         cout << "\n\nYou have entered a bad number. Program will terminate.\n\n";  
    }
    
    int main()
    {
     int choice = 0;
     
      do 
       {
        choice = display();
       } while(choice == 1);
    
     return 0;
    }
    Well thats it. Works like a charm. Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Newbie in C++ and error with run program
    By Daydream in forum C++ Programming
    Replies: 15
    Last Post: 12-02-2008, 12:47 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Maze Program, Any ideas
    By drkavngr1911 in forum C++ Programming
    Replies: 14
    Last Post: 11-13-2003, 03:32 PM
  5. Help writing calculator program (newbie)
    By Danny 2 in forum C Programming
    Replies: 3
    Last Post: 07-25-2002, 06:44 AM