Thread: Question using functions...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    29

    Question using functions...

    So here is my problem... I need to turn 24 hour notation into 12 hour notation, for example 14 34 into 2 34, but when my function runs I am just getting out exactly what I put in...

    Here is my code:
    Code:
    #include <iostream>
    
    void get_input(int& hour, int& minute);
    
    int time(int& hour, int& minute);
    
    void give_output(int& hour, int& minute);
    
    void introduction();
    
    int main()
    {
            int hour, minute;
    
            introduction();
            get_input(hour, minute);
            time(hour, minute);
            give_output(hour, minute);
            return 0;
    }
    
    void introduction()
    {
            using namespace std;
            cout << "This program ...\n";
    }
    
    void get_input(int& hour, int& minute)
    {
            using namespace std;
            cout << "Please enter a time (ex. xx xx)";
            cin >> hour >> minute;
    }
    
    int time(int& hour, int& minute)
    {
            if(hour > 12)
               return ( hour - 12 );
            else
               return 0;
    }
    
    void give_output(int& hour, int& minute)
    {
            using namespace std;
            cout << "Your time written in 12 hour notation is: "
                 << hour << " " << minute << endl;
    }
    If anyone can see anything wrong, it would help me out a lot! Thanks!

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    and i just added the using namespace std; in my time function

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your function is making a calculation and returning it. You are not using the return value of the function. You are also not modifying the parameters that are passed by reference. You have to do one or the other for the function to have an effect on the data you are using, otherwise calling the function has no effect and when you pass hour and minute to give_output they still have the original values in 24 hour notation.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    12
    Daved: I thought of that too, but in that case, why does it output at all. Because he uses a function for getting the time in the first place and doesn't return anything, it should only store the time in the local variables to the get function, but according to him, he's getting output.

    That still is probably your problem, but I'm just wondering how you get any output...

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    He gets output because he makes output statements. Duh.

    By the way, there's no reason to pass the values by reference to the output function.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I'm just wondering how you get any output...
    Because the input function takes its variables by reference. So when the input function is called, it changes the values of hour and minute inside main. Then later main passes those values to the output function.

    Look up pass-by-reference if you're not familiar with it, that seems to be where you're confused.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner's question about functions.
    By Crocodile23 in forum C Programming
    Replies: 4
    Last Post: 01-13-2009, 07:00 AM
  2. Functions Question
    By audinue in forum C Programming
    Replies: 2
    Last Post: 01-09-2009, 09:39 AM
  3. functions question.
    By Boozel in forum C Programming
    Replies: 1
    Last Post: 02-23-2008, 12:38 AM
  4. Question concerning functions
    By Warrax in forum C++ Programming
    Replies: 5
    Last Post: 04-04-2007, 11:00 AM
  5. Question about creating flash functions
    By jbh in forum C++ Programming
    Replies: 8
    Last Post: 11-21-2005, 09:39 AM