Thread: Convert seconds to hours and minutes

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    98

    Convert seconds to hours and minutes

    My first issue is I am receiving a warning that the float* differs in levels of indirection from int. I have tried changing min an hours to int with the same warning but replacing int* in the warning.

    The assignment is to write a c function name time () that accepts an interger number of seconds and the addresses of the 3 variable name hours, min, and sec. The function is to conver the passed number os seconds into the equivalent of hours minutes and seconds and directly alter the value of the respective variable using their passed addresses.

    Here is the code I have so far

    .
    Code:
    #include
    <stdlib.h>
    #include
    <stdio.h>
    #pragma
    warning (disable: 4996)
    void
     time(int, float*, float*); /*function prototype*/
     
    int
     main ()
    {
    	
    int seconds;
    	
    float hours, min;
    		
    	printf(
    "Enter an amount of seconds passed: ", &seconds);
    	scanf(
    "%d", &seconds);
    	time (seconds, &hours, &min);
    	printf(
    "The equivalent of %d seconds in hours is %6.2f, minutes is %6.2f, and seconds is %d)", seconds, hours, min, seconds);
    	system (
    "pause");
    	
    return 0;
    }
    void
     time(int seconds, float *hoursaddr, float *minaddr)
    {
    	hoursaddr = seconds;
    	minaddr = seconds/60;
    }
     
     
    

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You need to learn how to paste code "as text" so that your code is readable.
    Pasting HTML/RTF does NOT work.

    This is what code in code tags should look like.
    Code:
    int main ( ) {
        // this is a comment
        return 0;
    }
    If it doesn't look like this, you're doing it wrong.

    I fixed your first couple of threads, but it's not an indefinite service.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I believe that hours, minutes and seconds variables should all be ints, not floating point variables. 3.15 hours is misleading in the presence of minutes and seconds, for example. Minutes and seconds *are* fractions of hours, and don't correlate directly with base 10 fractions of an hour.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    I tried switching everything to int, I receive the same warning instead of saying *float to int it says int* to int differs in level of indirection

    To Salem; I looked up the issue I was having with the pasting of the code it said to try to paste to notepad then to the website so hopefully this is right

    Code:
    void time(int, int*, int*); /*function prototype*/
    
    int main ()
    {
     int seconds;
     int hours, min;
      
     printf("Enter an amount of seconds passed: ", &seconds);
     scanf("%d", &seconds);
     time (seconds, &hours, &min);
     printf("The equivalent of %d seconds in hours is %6.2f, minutes is %6.2f, and seconds is %d)", seconds, hours, min, seconds);
     system ("pause");
     return 0;
    }
    void time(int seconds, int *hoursaddr, int *minaddr)
    {
     hoursaddr = seconds;
     minaddr = seconds/60;
    }

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It's pasted correctly (as plain text).

    Code:
    void time(int, int*, int*); /*function prototype*/
    
    int main ()
    {
     int seconds;
     int hours, min;
      
     printf("Enter an amount of seconds passed: ", &seconds);
     scanf("%d", &seconds);
     time (seconds, &hours, &min);
     printf("The equivalent of %d seconds in hours is %6.2f, minutes is %6.2f, and seconds is %d)", seconds, hours, min, seconds);
     system ("pause");
     return 0;
    }
    void time(int seconds, int *hoursaddr, int *minaddr)
    {
     hoursaddr = seconds;
     minaddr = seconds/60;
    }
    The way C works is, it ALWAYS passes parameters by COPY. If you pass seconds to time(), then the seconds that time works with, will be a copy of the original seconds in main(), and when the program returns from the time() function, the value of seconds that you had in time(), will be gone, and the seconds value will be the same value it had, before the call to time().

    If you are sending an address, then the COPY of the address, is still going to be pointing to the original variable - so any changes you make to those variables will be retained when the program returns from the time() function.

    There are three ways to keep your seconds value from time:

    1) make seconds global - declared above main(). Generally not good.

    2) send time the address of seconds, instead of a copy of the variable.

    3) return the value of seconds, and have that value "caught" by the seconds variable:

    Code:
    seconds = time(seconds, &minutes, &hours);
    Note that the seconds in main(), is a local variable, and is NOT the same variable as the seconds variable that is in time - they just happen to share the same name, but that is all that is the same.

    I suspect you want way #2 or #3 above, but you decide.

    hoursaddr and minaddr are not what you want to be changing - those are addresses. You want to change the VALUE of what they point to, not the address of the pointer itself.
    Last edited by Adak; 03-27-2012 at 03:40 PM.

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    Okay, that makes sense. I am receive an error in the fuction time that "/" illegal, left opperance includes int*

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    An int and an int * (pointer to int) are not the same thing. You can not assign from one to the other, and you can't multiply or divide them*. You must dereference the pointer before you can get the the number it points to, and do regular number stuff like assigning, multiplying, etc. You need *minaddr = seconds / 60;

    * You are allowed to add and subtract integer values from pointers, but I wouldn't worry about the details of that just yet.

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Presumably you mean something more like this:
    Code:
    void time(int seconds, int *hoursaddr, int *minaddr)
    {
     *hoursaddr = seconds;
     *minaddr = seconds/60;
    }
    Note the asterisks (to dereference the pointers).

    Of course, the calculation is still way off....
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    I changed my formulas to *hoursaddr = seconds/360 and same for minutes. When I run m program both min and hours print 0

  10. #10
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    A few things:
    1) Don't print &seconds in your first printf statement. There's no need for that
    2) You're printing hours and minutes as floats but they are declared as integers.
    3) There are 3600 seconds in an hour, not 360.
    Code:
    while(!asleep) {
       sheep++;
    }

  11. #11
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    Careless mistake. I am getting a warning on line with the time function below scanf.
    It says function int * differs in levels of indirection from int and that time: different types for formal and actual parameter
    Also, when I run my program if I enter for example 120 seconds I receive 0 for hours. I tried switching hours to float but I just received 0.000000, is there any way to make it so the answer is hours as an accurate decimal?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    Code:
    void time(int, int*, int*); /*function prototype*/
    
    int main ()
    {
     int seconds;
     int hours, min;
      
     printf("Enter an amount of seconds passed: ", &seconds);
     scanf("%d", &seconds);
     time (seconds, &hours, &min);
     printf("The equivalent of %d seconds in hours is %d, minutes is %d, and seconds is %d)", seconds, hours, min, seconds);
     system ("pause");
     return 0;
    }
    void time(int seconds, int *hoursaddr, int *minaddr)
    {
     *hoursaddr = seconds/3600;
     *minaddr = seconds/60;
    }

  14. #14
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Almost everything you need has already been said.

    I'll only add that if "seconds/3600" is hours "seconds/60" can't be minutes.

    Soma

  15. #15
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    The only error I'm getting now is that you still haven't taken the &seconds out of the first printf statement.
    Code:
    while(!asleep) {
       sheep++;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert seconds to hours, minutes and seconds
    By kkk in forum C Programming
    Replies: 2
    Last Post: 07-26-2011, 10:47 AM
  2. hour,minutes & seconds
    By anaer0bic in forum C Programming
    Replies: 14
    Last Post: 06-28-2011, 11:19 AM
  3. Replies: 12
    Last Post: 10-05-2009, 12:19 AM
  4. Algo for create hours and minutes with given seconds
    By BianConiglio in forum C Programming
    Replies: 6
    Last Post: 05-30-2004, 06:50 PM
  5. Converting MInutes to Hours and MInutes
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 08-30-2001, 08:07 PM