pointer issue

This is a discussion on pointer issue within the C Programming forums, part of the General Programming Boards category; I am tasked with writing a C program using the function time(). the program allows the entry of seconds and ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    9

    pointer issue

    I am tasked with writing a C program using the function time(). the program allows the entry of seconds and converts them to hours, mins, seconds.
    I have to use a pointer, which apparently is over my head. I think I am almost there but something is escaping me and after hours I just can not figure it out. Perhaps someone out there can point(er) me in the right direction?

    Thanks

    My Code:
    Code:
    #include <stdio.h>
    
    
    void time(int seconds, int *hours, int *min, int *sec);
    int main()
    {
      int time, seconds, hours, min, sec;
      printf("\nEnter the number of seconds: \n", &seconds);
      scanf ("%d", &seconds);    /* call the function */
      return 0;
    }
    
    
    void time(int seconds, int *hours, int *min, int *sec)
    
      {
        // int seconds 
         int holdScreen;
         *hours = seconds / 3600;
         *min = (seconds % 3600) / 60;
         *sec = (seconds % 3600) % 60;
         scanf("%d", &holdScreen);
         printf ("%d is equal to %d hours %d minutes and %d second(s)", seconds, hours, min, sec);
         scanf("%d", &holdScreen);
    
      }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ok, your scanf isn't actually calling your time function. (Which by the way, there is a standard C function called time so you should probably pick a different name. You actually need to call your own function after you read into seconds with that scanf call.
    Code:
    scanf( ... ); /* get seconds */
    mytime( ... ); /* pass those to the other function */
    Now, just like you are using seconds to scanf, you need to be passing the hours min and sec arguments the same way when you call your function. The seconds argument (first argument) is just passing a value, so you don't use & on it.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    Working on it now... Will post a revision soon...
    thanks for the prompt assistance!

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    OK this has me beat... I know I am close but I just am not getting something. Any further help would be nice! I know my math functions are wonkey...

    Code:
    #include <stdio.h>
    
    int main()
    {
        int holdScreen;
        void myTime(int seconds, int *hours, int *min, int *sec);
        int seconds, *hours, *min, *sec;
            
         printf("Enter the number of seconds: \n");
         scanf ("%d", &seconds); 
        
         printf ("%d is equal to %d hours %d minutes and %1d second(s)", seconds, hours, min, sec);
         scanf("%d", &holdScreen);
         return 0;
    }
    
    void myTime( int seconds, int *hours, int *min, int *sec)
    
      {
                   
        seconds = *sec%3600;
        *hours = seconds/3600;
        *min = seconds/60;
        *sec = seconds %60;
        
      }

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You still aren't actually calling myTime anywhere. You prototype it, but you never invoke it:
    Code:
    void foo( void ); /* prototype */
    int main ( void )
    {
        foo(); /* actually calling foo */
    
        return 0;
    }
    
    void foo( void )
    {
        /* actual function body */
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    reworking it again and Thanks for your patience.

    well at least I get my compile again... lol...

    Code:
    #include <stdio.h>
    void myTime(int seconds, int *hours, int *min, int *sec);
    
    int main()
    {
        int holdScreen;
       
       int seconds, *hours, *min, *sec;                                           // This line seems redundant
       void myTime(int seconds, int *hours, int *min, int *sec); 
    
            
         printf("Enter the number of seconds: \n");
         scanf ("%d", &seconds); 
        
         printf ("%d is equal to %d hours %d minutes and %1d second(s)", seconds, hours, min, sec);
         scanf("%d", &holdScreen);
         return 0;
    }
    
    void myTime( int seconds, int *hours, int *min, int *sec)  //this just doesn't look right to me 
    
      {
                   
        seconds = *sec%3600;         //calculations apparently wrong
        *hours = seconds/3600;
        *min = *sec/60;
        *sec = seconds %60;
        
      }
    Last edited by Nuggnugg; 03-29-2011 at 09:00 PM.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    Starting over again...

    Code:
    #include <stdio.h>
    void time(int second, int *, int *);
    int main()
     {
              int holdScreen;
              int second, min, hour;
              
              printf("Enter two numbers: ");
              scanf("%d %d",&min, &hour);
              time(&min,&hour);
              
              scanf("%d", &holdScreen);
      }
    void time(int sec, int *min, int *hour)
      {
        int sec;
        
        sec ( (*hour) *60 + min) *60;
        printf("the total numbers of seconds is %d", sec);
        return;

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to listen to your compiler.
    Code:
              time(&min,&hour);
              
              scanf("%d", &holdScreen);
      }
    void time(int sec, int *min, int *hour)
    Do you see anything missing?


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    seconds?.... lemme try that

    ahh the * ?

    Code:
    #include <stdio.h>
    void time(int seconds, int *, int *, int *);
    int main()
     {
              int holdScreen;
           int seconds, *hour, *min, *sec;  
              
              printf("Enter Seconds: ");
              scanf("%d",seconds);
              time(seconds,hour,min,sec);
              
              scanf("%d", &holdScreen);
      }
    void time(int seconds, int *min, int *hour, int *sec)
    
      {
      
        
        seconds ( (*hour) *60 + *min) *60;
        printf("the total numbers of seconds is %d", seconds);
        return;
    }
    Error saying:
    In function `void time(int, int*, int*, int*)':
    `seconds' cannot be used as a function

    then highlights this line:

    seconds ( (*hour) *60 + *min) *60;

    my end product so far:

    Code:
    #include <stdio.h>
    void time(int seconds, int *, int *, int *);
    int main()
     {
              int holdScreen;
              int seconds, *hour, *min, *sec;  
              
              printf("Enter Seconds: ");
              scanf("%d",seconds);
              time(seconds,hour,min,sec);
              
              scanf("%d", &holdScreen);
      }
    void time(int *hour, int *min, int *sec)
    
      {
      //int seconds;
        
        int seconds ( (*hour) *60 + *min) *60;
        printf("the total numbers of seconds is %d", seconds);
        return;
    }
    Last edited by Nuggnugg; 03-29-2011 at 11:47 PM.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    I gave up..... resigned myself to get a bad grade on this...

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Nuggnugg View Post
    I gave up..... resigned myself to get a bad grade on this...
    It's probably for the best. It doesn't seem like you are paying enough attention. Honestly you should probably drop your programming class if this is too hard for you:
    Code:
     time(seconds,hour,min,sec);
              
              scanf("%d", &holdScreen);
      }
    void time(int *hour, int *min, int *sec)
    {
    How many arguments are there in the red line? Here's a hint: FOUR
    How many arguments is your function supposed to have in the magenta line? Here's another hint: THREE

    If you actually plan on passing this class/program/whatever, then you need to actually start paying attention to your compiler, and to simple obvious things like using the wrong number of arguments to functions you yourself are writing.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    My grade in the class is a 96 to date. I hardly think that warrants quitting the class.

    As far as listening to my compiler, well, when the code runs below and I enter my seconds it just crashes with no error. Kind of hard to "listen" to something that isn't "talking"...


    Code:
    #include <stdio.h>
    void time(int seconds, int *, int *, int *);
    int main()
     {
              //int holdScreen;
              int seconds, *hour, *min, *sec;  
              
              printf("Enter Seconds: ");
              scanf("%d",seconds);
              time(seconds,hour,min,sec);
              
             // scanf("%d", &holdScreen);
              return 0;
      }
    void time(int seconds, int *hour, int *min, int *sec)
    
      {
      
        int holdScreen;
        *hour = seconds / 3600;
         *min = (seconds % 3600) / 60;
         *sec = (seconds % 3600) % 60;
         printf ("%d is equal to %d hours %d minutes and %d second(s)", seconds, hour, min, sec);
         scanf("%d", &holdScreen);
    
        return ;
    }

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
             printf("Enter Seconds: ");
              scanf("%d",&seconds);
    Your compiler should have been warning you about that.

  14. #14
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    So many things...

    - scanf("%d", &seconds); (add the ampersand)

    - in main you should define int seconds, hour, min, sec; (no asterisks)

    - time(seconds, &hour, &min, &sec); (add ampersand, ampersand, ampersand). I assume you want the function to return values to these variables.

    - int time(), if you deal with hour, min, sec, you must include asterisks to get at each value.
    printf ("%d is equal to %d hours %d minutes and %d second(s)", seconds, *hour, *min, *sec);

  15. #15
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    Ahhhhhhhhhh.......... works! But I have a question and this is where I have been confused the whole time. Perhaps you could shed some light for me. I'll pose my question(s) below the code. Code first:

    Code:
    #include <stdio.h>
    void time(int seconds, int *, int *, int *);
    int main()
     {
              
              int seconds, hour, min, sec;  
              printf("Enter Seconds: ");
              scanf("%d",&seconds);
              time(seconds,&hour,&min,&sec);
                      
              return 0;
      }
    void time(int seconds, int *hour, int *min, int *sec)
    
      {
      
        int holdScreen;
        *hour = seconds / 3600;
         *min = (seconds % 3600) / 60;
         *sec = (seconds % 3600) % 60;
         printf ("%d is equal to %d hours %d minutes and %d second(s)", seconds, *hour, *min, *sec);
         scanf("%d", &holdScreen);
    
        return ;
    }
    Questions:
    1. in line 2 "void time(int seconds, int *, int *, int *);" this is my prototype I presume... Why then do you have to put in line 6? Line six being "int seconds, hour, min, sec; "

    It seems to me this is redundant... then again on line 13 which is "void time(int seconds, int *hour, int *min, int *sec)"

    And again seems redundant to me...

    I guess my question is why so much redundancy?
    Last edited by Nuggnugg; 03-31-2011 at 02:43 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Argument from incompatible pointer type
    By AmritxD in forum C Programming
    Replies: 3
    Last Post: 08-15-2010, 04:23 PM
  2. Help with pointer issue
    By ph5 in forum C Programming
    Replies: 4
    Last Post: 11-17-2009, 04:19 PM
  3. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  4. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 03:29 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

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