Noob + Functions == Problems

This is a discussion on Noob + Functions == Problems within the C++ Programming forums, part of the General Programming Boards category; Ok, so I've been tinkering with this code meant to take an input of time and display it in Hours:minutes:seconds ...

  1. #1
    Apprentice to Sly & Shane Inao's Avatar
    Join Date
    Nov 2005
    Location
    Oklahoma
    Posts
    32

    Red face Noob + Functions == Problems

    Ok, so I've been tinkering with this code meant to take an input of time and display it in Hours:minutes:seconds using two functions... Now when I define values for variables within functions they don't seem to "stick" when they move to other functions. Help please?

    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    int returnTime(int min, int sec, int hours);
    void displayTime(int hours, int min, int sec);
        
    
         
    int main()
    {
        int min, sec;
        int hours;
        cout << "Enter a time: \n";
        returnTime(min, sec, hours);
        cout << "Your time was: ";
        displayTime(hours, min, sec);
        
        cin.ignore();
        cin.get();
        
        return 0;
    }
    
    int returnTime(int min, int sec, int hours)
    {
        cout<< "Enter minutes: \n";
        cin>>min;
        cout<< "Enter seconds: \n";
        cin>>sec;
        if (min >= 60) {
              hours = min/60;
              min = min%60;
    }
    }
    void displayTime(int hours, int min, int sec){
         cout<<hours;
         cout<<" : ";
         cout<<min;
         cout<<" : ";
         cout<<sec;
         }
    The Pristine Angel lives on!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Code:
    int returnTime(int & min, int & sec, int & hours);
    
    int returnTime(int & min, int & sec, int & hours)
    {
        cout<< "Enter minutes: \n";
        cin>>min;
        cout<< "Enter seconds: \n";
        cin>>sec;
        if (min >= 60) {
              hours = min/60;
              min = min%60;
        }
    }
    You need to pass the arguments as references, otherwise you are only passing copies and the changes in the function affect the copies and not the real values that you passed in to the function. That function also should probably be of return type void, not int.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Apprentice to Sly & Shane Inao's Avatar
    Join Date
    Nov 2005
    Location
    Oklahoma
    Posts
    32
    oh wow, thank you so much - it works now!
    The Pristine Angel lives on!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework help
    By mkdl750 in forum C Programming
    Replies: 45
    Last Post: 07-17-2008, 09:44 PM
  2. Separate long string into multiple arrays
    By cashmerelc in forum C Programming
    Replies: 6
    Last Post: 11-27-2007, 01:57 AM
  3. One quick question...
    By Kross7 in forum C++ Programming
    Replies: 10
    Last Post: 04-13-2007, 09:50 PM
  4. Replies: 4
    Last Post: 11-23-2003, 06:15 AM
  5. Massive Function Problem
    By Marc Sharp in forum C Programming
    Replies: 10
    Last Post: 11-19-2003, 07:49 PM

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