military time converter using functions

This is a discussion on military time converter using functions within the C++ Programming forums, part of the General Programming Boards category; hello i am making a program where the user enters the hours and minutes in military time(24hrs) and the program ...

  1. #1
    new to c++
    Join Date
    Feb 2009
    Posts
    53

    military time converter using functions

    hello

    i am making a program where the user enters the hours and minutes in military time(24hrs) and the program will output the time in regular time(12hrs). i need to use funtions, one for the input, one for the coverting, and one for the output. im new to funtions and cant seem to get it to work. i can input the time fine but the program will not convert or output the right time. it keeps saying the variable afternoon is being used without being initialized, and then it outputs the same time that i input. can i get some hints on what may be wrong or something.

    thanks
    Code:
    #include<iostream>
    using namespace std;
    
    void military(int& mhour,int& mmin);
    void convert(int mhour, int& hours, char afternoon);
    void print(int hours,int mmin,char afternoon);
    
    int main()
    {
    	int mhour,mmin,hours;
    	char afternoon;
    
    	military(mhour,mmin);
    
    	convert(mhour,hours,afternoon);
    
    	print(hours,mmin,afternoon);
    
    
    	system("pause");
    	return 0;
    }
    void military(int& mhour,int& mmin)
    {
    	cout<<"enter the hours in 24 hour format<0-24>\n";
    	cin>>mhour;
    	cout<<"enter the minutes in 24 hour forman<00-59>\n";
    	cin>>mmin;
    
    }
    void print(int hours,  int mmin,char afternoon)
    {
    	cout<<"The time in 12 hour format is "<<hours<<":"<<mmin<<" "<<afternoon<<"m \n";
    }
    void convert(int mhour, int& hours, char afternoon)
    {
    	if(mhour>12)
    	{
    		hours=mhour;
    		afternoon='A';
    	}
    	if(mhour<=13)
    	{
    		hours=mhour-12;
    		afternoon='P';
    	}
    }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,522
    Why did you choose not to make afternoon reference as well?

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    11
    Quote Originally Posted by rfoor View Post
    it keeps saying the variable afternoon is being used without being initialized, and then it outputs the same time that i input.
    Right, the 'afternoon' in main() is not initialized. I think you meant to use 'char&' instead of 'char' on the parameter list for convert() (then convert() would initialize 'afternoon' for you). You may still get a warning that the variable may not be initialized; you can just replace the second conditional statement in convert() with an else, and that should make the compiler realize that initialization happens in every branch. And your conditions are backward. If mhour > 12 (should be <), you're doing nothing to it and calling it "AM". If mhour <= 13 (should be >=), you're subtracting 12 from it (sometimes getting a negative number?) and calling it "PM".

    As a side note, "military time" is a name Americans give the 24-hour clock (I know, I'm from the U.S., I've heard this many times). But the 24-hour clock is less ambiguous (6 means morning, not evening, and 12 means noon, not midnight), and for that reason it is used officially around the world and it is an ISO standard. Like with the imperial and SI/metric measurements, the U.S. is behind the rest of the world in standardized units.
    Last edited by PehJota; 10-26-2010 at 09:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert a local time to a timezone's bias
    By NightCode in forum Windows Programming
    Replies: 1
    Last Post: 03-09-2010, 10:45 AM
  2. Need help with time
    By Gong in forum C++ Programming
    Replies: 7
    Last Post: 01-11-2007, 01:43 PM
  3. need help in time zone
    By Gong in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2007, 03:44 AM
  4. The space time continueimnms mm... (rant)
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 06-27-2004, 01:21 PM
  5. I apologize. Good bye.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 06:51 PM

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