GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Nov-2005, 13:30
tylerfelix tylerfelix is offline
New Member
 
Join Date: Sep 2005
Posts: 28
tylerfelix is on a distinguished road

Creating a time converter


Ok, I want to build a time converter, which takes numbers from 24-hour time and converts it to time with AM/PM. I want the output to look something like this (through using c++):

Enter 24 hour time in the format HH:MM
00:20
Time in 12 hour format:
12:20 AM

Would you like to do another conversion?
(`Y' or `y' for yes; `N' or `n' for No): Y

I'm trying to do this by using the call by reference (byref) function to place in A or P (am or pm) in the proper instance.

Could someone point me in the right direction, maybe through some wording, not with code.
  #2  
Old 20-Nov-2005, 15:11
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Creating a time converter


Subract 12 from the hour if > 12...
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #3  
Old 20-Nov-2005, 15:28
tylerfelix tylerfelix is offline
New Member
 
Join Date: Sep 2005
Posts: 28
tylerfelix is on a distinguished road

Re: Creating a time converter


Quote:
Originally Posted by WaltP
Subract 12 from the hour if > 12...

ok, i've got that part thought out.. I don't know how I would call a byref to use AM and PM. :S

thanks.
  #4  
Old 20-Nov-2005, 18:35
tylerfelix tylerfelix is offline
New Member
 
Join Date: Sep 2005
Posts: 28
tylerfelix is on a distinguished road

Re: Creating a time converter


This is my base for the program. I have the output subtracting 12 when time is 12 or greater, but I do not know how to create a call for a byref function in each the Am or Pm. My final problem is that I do not know how to make either my hours or minutes be two zeros, such as 00:50 or 12:00

CPP / C++ / C Code:
#include <iostream>

using namespace std;

int main()
{

	int hours;
	int minutes = 00;
	char yesOrNo;

	do 
	{
	
	cout << "Please enter in the time of day in 24 hour time HH MM ";
	cout << "that you would wish to see in 12 hour time (AM/PM)" << endl;

		cin >> hours >> minutes ;

		if (hours >= 12)
		{
			(hours - 12);
				cout << "It is: " << hours << " : " << minutes << endl;
		}

		else
		{
			cout << "It is: " << hours << " : " << minutes << endl;
		}

		cout << "Would you like to convert another time? Input y for yes, n for no." << endl;
		
		while ((yesOrNo == 'y' || 'Y') || (yesOrNo != 'n' || 'N'));

		

	}
	return 0;
}
I will use toupper, don't worry about that part.

also i have 2 problems, and I do not know how to debug .

D:\Program Files\Microsoft Visual Studio\MyProjects\ergaergaergaerg\wregfaerge.cpp(3 : error C2059: syntax error : 'return'
D:\Program Files\Microsoft Visual Studio\MyProjects\ergaergaergaerg\wregfaerge.cpp(3 9) : warning C4508: 'main' : function should return a value; 'void' return type assumed
  #5  
Old 20-Nov-2005, 18:51
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Creating a time converter


Hi tyler,

The problem is that, you misplaced the braces.
The closing brace: "}" above the return statement should come before the while loop.
like this:
CPP / C++ / C Code:
    }while ((yesOrNo == 'y' || 'Y') || (yesOrNo != 'n' || 'N'));
		
    return 0;
}

There is no need to use function call by reference for calculating AM or PM.

Assume that the time is AM and store it in a string.
like:
CPP / C++ / C Code:
string s = "AM";

If the hours is greater than 12, then change the string to "PM".
Then print this string after you print the date.

Regarding putting extra zero, you can use a simple method of printing a "0" if the minutes is less than 10.
like:
CPP / C++ / C Code:
if( minutes < 10 )
    cout<<"0";

There is one more error in the program. Do you know what it is?

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #6  
Old 20-Nov-2005, 18:54
tylerfelix tylerfelix is offline
New Member
 
Join Date: Sep 2005
Posts: 28
tylerfelix is on a distinguished road

Re: Creating a time converter


firstly, I don't quite understand your logic for if (minutes < 10) care to indulge me? Lastly, my stupidity on the bracket thing was funny, but your solution fixed all the noticable problems. What's my error? ALSO for some reason my prof has skipped string variables, but i can deal with them just as a char, int, etc. right?


EDIT: nvm... now i get the < 10 thing, so i would do something like cout "0" << minutes?
  #7  
Old 20-Nov-2005, 19:02
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Creating a time converter


Quote:
Originally Posted by tylerfelix
firstly, I don't quite understand your logic for if (minutes < 10) care to indulge me? Lastly, my stupidity on the bracket thing was funny, but your solution fixed all the noticable problems. What's my error? ALSO for some reason my prof has skipped string variables, but i can deal with them just as a char, int, etc. right?


EDIT: nvm... now i get the < 10 thing, so i would do something like cout "0" << minutes?

I used that logic because you simply cant print "00" using an integer.
So, if minutes is less than 10, or hour is less than 10, we should precede that by 0 isnt it?
For example, if the time is 5 : 5, we should print it as:<br>05:05 isnt it?
So. thats a simple way of preceding it by zeroes.

If you havent come up to strings, then use
CPP / C++ / C Code:
char s[] = "AM";

Regarding the error:
Run the program once and check. It dont asks for the input whether to continue or not...
How does this error happen?
Because you use the cin statement.
A simple way to solve this problem is to use a getchar() after your cin statement.

edit:
Yes. print : cout << "0" << minutes;

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #8  
Old 20-Nov-2005, 19:31
tylerfelix tylerfelix is offline
New Member
 
Join Date: Sep 2005
Posts: 28
tylerfelix is on a distinguished road

Re: Creating a time converter


This is how I interpreted the "0" part... we'll see if it's a good way. I hope i didn't mess up the char [] variable. :
I realize that "int minutes = 00;" is pointless, but my cout 0 isn't working when i input 12 00
CPP / C++ / C Code:
#include <iostream>

using namespace std;

int main()
{

	int hours;
	int minutes = 00;
	char yesOrNo;
	char s[] = "AM";
	char p[] = "PM";
	

	do 
	{
	
	cout << "Please enter in the time of day in 24 hour time HH MM ";
	cout << "that you would wish to see in 12 hour time (AM/PM)" << endl;

		cin >> hours >> minutes ;


		if ((hours < 10 && minutes < 10))
		{
			cout << "It is: 0" << hours << ":0" << minutes << endl;
		
		}

		if (hours < 10)
		{
			cout << "It is: 0" << hours << ":" << minutes << endl;
		}

		if (minutes < 10)
		{
			cout << "It is: " << hours << ":0" << minutes << endl;
		}
		if (hours >= 12)
		{
			(hours - 12);
			
							
				cout << "It is: " << hours << " : " << minutes << p << endl;
		}

		else
		{
			cout << "It is: " << hours << " : " << minutes << s << endl;
		}

		cout << "Would you like to convert another time? Input y for yes, n for no." << endl;

		cin >> yesOrNo;

	}while ((yesOrNo == 'y' || 'Y') || (yesOrNo != 'n' || 'N'));

		

	

	return 0;
}
  #9  
Old 20-Nov-2005, 19:45
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Creating a time converter


Good work.

But we need to have some changes...
Here is what the output i got:
Code:
Please enter in the time of day in 24 hour time HH MM that you would wish to see in 12 hour time (AM/PM) 5 5 It is: 05:05 It is: 05:5 It is: 5:05 It is: 5 : 5AM Would you like to convert another time? Input y for yes, n for no.

Do you see what the error is?
Use else if statements.
and:
Code:
Please enter in the time of day in 24 hour time HH MM that you would wish to see in 12 hour time (AM/PM) 12 12 It is: 12 : 12PM Would you like to convert another time? Input y for yes, n for no. n Please enter in the time of day in 24 hour time HH MM that you would wish to see in 12 hour time (AM/PM)


and still not working in the input section. Try solving this one..

What does this statement mean:
CPP / C++ / C Code:
            (hours - 12);
You should not do this.
Instead, you should vary the string AM or PM.
edit:
Also, it should be hours = hours - 12;

You can simplify the whole process by using only one character:
CPP / C++ / C Code:
char m[] = "AM";


Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #10  
Old 20-Nov-2005, 19:50
tylerfelix tylerfelix is offline
New Member
 
Join Date: Sep 2005
Posts: 28
tylerfelix is on a distinguished road

Re: Creating a time converter


WaltP had the (hours - 12) idea, it fits my purpose well.. but you're right because I was having a lot of trouble incorporating it with if statements.

EDIT: also if i put an 'n' or 'N' in to leave the program I get an endless loop... well.. the second time.


EDIT of an EDIT: i'm looking for that input error

the input i'm taking in isn't taken care of very well. if someone puts in 11 00 then it won't put the AM after it, and things like that, i'm going to have to make separate if statements but that isn't a huge issue.

for the input error, I was thinking maybe if nothing is entered, assume it as 00 ?
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Knights Tour - Reloaded . kobi_hikri C Programming Language 12 03-Oct-2005 13:15
Simulation Problem wu_weidong C++ Forum 7 12-Mar-2005 23:56
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 16:13
time Problem zuzupus MySQL / PHP Forum 9 24-Jul-2003 08:02

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 10:26.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.