Pointers...Problem still exists

This is a discussion on Pointers...Problem still exists within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, I need help with the pointers. Here's my question: Using a pointer, write a function named time() that ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    11

    Pointers...Problem still exists

    Hi everyone, I need help with the pointers. Here's my question:

    Using a pointer, write a function named time() that accepts an integer number of seconds and the addresses of three variables named hours, min, nad sec. The function is to convert the passed number of seconds into an equivalent number of hours, minutes, and seconds and directly alter the vlaue of respective variables using the passed addresses.

    To test, run the program using 10,000 seconds.

    This is what I have done so far, but its not working...


    Code:
    #include<stdio.h>
    
    int time(int time, int *hours, int *mins, int *secs);
    
    int main()
    
    {
    
    int time, secs, mins, hours;
    
    printf("Enter the number of time in seconds: ");
    scanf("%d", &time);
    printf("\nHours: ", &hours);
    printf("\nMinutes: ", &mins);
    printf("\nSeconds: ", &secs);
    
    return 0;
    }
    
    int time (int time, int *hours, int *mins, int *secs)
    {
    
    	*hours = time / 3600;
    	time %= 3600;
    
    	*mins = time / 60;
    	time %= 60;
    
    	*secs = time;
    
    
    }

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    It might help if you pass your variables to the function.
    Oh and P.S posting on all the boards will get you hated real fast.
    Try not to be an idiot and learn how to wait to get help with your problem.
    Oh and learn how to pay attention in class.
    Last edited by prog-bman; 03-29-2005 at 09:23 AM.
    Woop?

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Prepare now for the flaming you are about to recieve for:

    1. reposting your problem.
    2. being advised we are not going to do your homework.
    3. someone actually did your homework for you in your previous thread and for some reason you are still reposting your problem
    4. posting C code in a C++ forum

    You should view this tutorial



    With all that aside, I would be willing to help you out, but I am not down with the C method your are using to pass arguments into time( ). If you were using c++, I would advise passing your arguments in by reference where you can then assign them directly into pointer variables.
    Attached Images Attached Images  
    Last edited by The Brain; 03-29-2005 at 09:44 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    The Brain: Just as an addendum, it isn't always better to post in an existing thread. Old threads that have been bumped get locked down pretty fast. It is always better to post in an existing active thread than to start a new one.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    As far as I'm aware, a variable and a function cannot have the same name. Also you are printing the addresses of the variables, not their actual values.

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Then when they decided to standardise it, what possessed them to change it from what was the current convention?
    >Further replies here.

    Not being a member of the committee, I can only speculate; perhaps it was for the sake of easy distinguishment between what is a standard header and what belongs to a 3rd party library, i.e. improved code readability, and therefore also easier porting between platforms, easier code maintenance, etc.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    > scanf("%d", &time);
    OK so far, but now your challenge is to call the function with the same name.
    However, since you seem undecided as to whether this is C or C++, lets go with C++ since this is the board you posted on.
    Then you can do.
    Code:
    ::time( time, &hours, &mins, &secs );

    > printf("\nHours: ", &hours);
    1. Don't pass addresses to printf (it's not scanf), unless you mean to print an address.
    2. You need some conversion format in there as well, if you hope to see anything other than the text.
    Say for example
    Code:
    printf( "Hours: %d\n", hours );
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with file handling (pointers)
    By hmk in forum C Programming
    Replies: 5
    Last Post: 09-19-2008, 10:03 AM
  2. Problem with pointers
    By kotoko in forum C Programming
    Replies: 3
    Last Post: 06-12-2008, 05:17 AM
  3. Returning pointer to array of pointers problem
    By jimzy in forum C Programming
    Replies: 15
    Last Post: 11-11-2006, 05:38 AM
  4. pointers problem
    By toshog in forum C Programming
    Replies: 4
    Last Post: 09-11-2006, 03:17 AM
  5. Problem writing swap using pointers
    By joshdick in forum C++ Programming
    Replies: 1
    Last Post: 02-29-2004, 09:06 PM

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