Calculating pace.

This is a discussion on Calculating pace. within the C++ Programming forums, part of the General Programming Boards category; I'm trying to write a program to convert your pace in minutes and seconds to MPH. All the program is ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    23

    Calculating pace.

    I'm trying to write a program to convert your pace in minutes and seconds to MPH. All the program is doing is taking in the input of minutes and seconds. What am I doing wrong?

    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    double ConvertToMPH (int paceInMin, int paceInSec);
    double ConvertToMPH (double kph);
    
    int main()
    {
    
      double paceInMin;
      double paceInSec;
    
      cout << "Please enter your pace in minutes: ";
      cin >> paceInMin;
      cout << "Please enter your pace in seconds: ";
      cin >> paceInSec;
      
      return 0;
    }
    
    double ConvertToMPH(int paceInMin, int paceInSec)
    {
        return 60/(paceInMin + paceInSec/60.);
    }
    
    double ConvertToMPH(double kph)
    {
        return kph/1.61;
    }
    Last edited by mc74; 09-13-2012 at 08:27 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    If you want either of those ConvertToMPH() functions to be called, then you need to write code that calls them. You have not done so. The compiler does not just magically call a function because you defined it.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    23
    Ah, right. I have never been good with functions. That's why I'm playing around with this for practice.

    I tried this, it runs, but I get automatic garbage data :/

    Code:
    cout << "Please enter your pace in minutes: " << ConvertToMPH(paceInMin) << endl;
    cin >> paceInMin;
    cout << "Please enter your pace in seconds: " << ConvertToMPH(paceInSec) << endl;
    cin >> paceInSec;

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    You have to read the input, then call your function.
    Your instructions are executed by the computer sequentially.

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    23
    I tried calling ConvertToMPH after each cin, and I got a lot of errors.

    I also tried to call it after the cout, cin block. Ran, but it only took the input and exited out of teh program.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    We can't guess what you've tried. Post some code.

  7. #7
    Registered User
    Join Date
    Aug 2012
    Posts
    23
    Sorry man! Here is some code of things I have tried!

    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    double ConvertToMPH (int paceInMin, int paceInSec);
    double ConvertToMPH (double kph);
    
    int main()
    {
    
      double paceInMin;
      double paceInSec;
    
      ConvertToMPH (paceInMin, paceInSec);
      cout << "Please enter your pace in minutes: ";
      cin >> paceInMin;
      cout << "Please enter your pace in seconds: ";
      cin >> paceInSec;
    
      return 0;
    }
    
    double ConvertToMPH(int paceInMin, int paceInSec)
    {
        return 60/(paceInMin + paceInSec/60.);
    }
    
    double ConvertToMPH(double kph)
    {
        return kph/1.61;
    }


    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    double ConvertToMPH (int paceInMin, int paceInSec);
    double ConvertToMPH (double kph);
    
    int main()
    {
    
      double paceInMin;
      double paceInSec;
    
      cout << "Please enter your pace in minutes: ";
      cin >> paceInMin;
      cout << "Please enter your pace in seconds: ";
      cin >> paceInSec;
      ConvertToMPH (paceInMin, paceInSec);
    
      return 0;
    }
    
    double ConvertToMPH(int paceInMin, int paceInSec)
    {
        return 60/(paceInMin + paceInSec/60.);
    }
    
    double ConvertToMPH(double kph)
    {
        return kph/1.61;
    }


    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    double ConvertToMPH (int paceInMin, int paceInSec);
    double ConvertToMPH (double kph);
    
    int main()
    {
    
      double paceInMin;
      double paceInSec;
    
      
      cout << "Please enter your pace in minutes: ";
      cin >> paceInMin >> ConvertToMPH (paceInMin);
      cout << "Please enter your pace in seconds: ";
      cin >> paceInSec >> ConvertToMPH (paceInSec);
    
      return 0;
    }
    
    double ConvertToMPH(int paceInMin, int paceInSec)
    {
        return 60/(paceInMin + paceInSec/60.);
    }
    
    double ConvertToMPH(double kph)
    {
        return kph/1.61;
    }

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    Stop trying random things...
    What is this? is it an assignment? are you trying to learn C++ by yourself? what is the context here?

    ATTEMPT#1
    Same remark than earlier, you need to get the input first, then do something with it.

    ATTEMPT#2
    That one is better, you have your input, your calculate the result but... you do nothing with it. Use cout to print it (for instance).

    ATTEMPT#3
    Is this even compiling?
    Last edited by root4; 09-13-2012 at 09:40 AM.

  9. #9
    Registered User
    Join Date
    Aug 2012
    Posts
    23
    I am taking a C++ class, but I pulled this out of the book to try for some practice.

    Here is the actual question:
    You would like to know how fast you can run in Miles Per Hour. Your treadmill will output your speed in terms of a pace (Minutes and Seconds per mile, e.g. a "5:30 mile") or in terms of kilometers per hour (KPH).
    Write an overloaded function called "ConvertToMPH". The first definition should take as input two integers that represent the pace in Minutes and Seconds per mile and return the speed in MPH as a double. The second definition should take as input one double that represents the speed in KPH and return the speed in MPH as a double. One mile is approximately 1.61 kilometers. Write a test/driver program to test your function.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    Ok. I've edited my previous post to add remarks.
    Remember a program is like a recipe, you prepare the ingredients first then you mix them, not the opposite.
    Try to review I/O to understand why your 3rd attempt is wrong.

  11. #11
    Registered User
    Join Date
    Aug 2012
    Posts
    23
    I figured it out! It works correctly now! Thanks for the nudges, fellas! Much appreciated!!


  12. #12
    Registered User
    Join Date
    Aug 2012
    Posts
    23
    root4, attempt 3 did compile but I got garbage data.

  13. #13
    Registered User
    Join Date
    Aug 2012
    Posts
    23
    Here is my working code:

    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    double ConvertToMPH (int paceInMin, int paceInSec);
    double ConvertToMPH (double kph);
    
    int main()
    {
    
      double paceInMin;
      double paceInSec;
      double kph;
    
      cout << "Please enter your pace in minutes: ";
      cin >> paceInMin;
      cout << "Please enter your pace in seconds: ";
      cin >> paceInSec;
      cout << "Your pace in MPH is: " << ConvertToMPH (paceInMin, paceInSec);
    
      cout << "Please enter your speed in KPH: ";
      cin >> kph;
      cout << "Your KPH pace in MPH is: " << ConvertToMPH (kph);
    
      return 0;
    }
    
    double ConvertToMPH(int paceInMin, int paceInSec)
    {
        return 60/(paceInMin + paceInSec/60.);
    }
    
    double ConvertToMPH(double kph)
    {
        return kph/1.61;
    }
    Thanks again!

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    You're welcome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calculating e
    By roelof in forum C Programming
    Replies: 11
    Last Post: 06-11-2011, 01:49 AM
  2. Calculating GCD
    By roelof in forum C Programming
    Replies: 15
    Last Post: 05-27-2011, 10:40 AM
  3. Calculating MPG
    By Robot Kris in forum C Programming
    Replies: 7
    Last Post: 09-08-2010, 08:29 PM
  4. Calculating Pi with GMP
    By blkhockeypro19 in forum C Programming
    Replies: 0
    Last Post: 06-10-2010, 04:30 PM
  5. Help with calculating
    By Moffia in forum Windows Programming
    Replies: 3
    Last Post: 08-05-2005, 01:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21