User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,666 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,878 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 1605 | Replies: 3
Reply
Join Date: Oct 2004
Posts: 2
Reputation: suptboyr is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
suptboyr suptboyr is offline Offline
Newbie Poster

I need help with this c++ program please!!!

  #1  
Oct 12th, 2004
Input a time specified as the hour(1-12), minute (0-59), and the meridian (A or P for A.M or P.M); then output that time in the form HH:MM A.M/P.M.
Then enter a nonnegative number of minutes and output the time that is that number of minutes later than the original time. A sample run would look like:

Enter hours, minutes, and (A)m or (P)m: 10 5 p
The time is 10:05 PM
Enter a number of minutes (>0): 200
After 200 minutes, the time will be 1.25 AM




OK, here is what i came up with so far:

There are 3 numbers that have to be input, so entering a number, could be 1 function. convert minutes to hours and minutes, that could be a second function. first thing is to get the info.convert the minutes (to be added) to hours(newhours) and minutes(newminutes) using division by 60 and modular division by 60.and newhours to original HH, add newminutes to MM.
If the results are > 60 add 1 to the hour and subtract 60 from the minutes.

Can someone please help me to put this into coding. Thank you.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,333
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Rep Power: 11
Solved Threads: 101
Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: I need help with this c++ program please!!!

  #2  
Oct 12th, 2004
Can you write some pseudo code or something? Just outline what you've thought up in an indented- type format, so you can logically see what you're thinking of. Then, perhaps post that here, and let us help you with specific points of your reasoning.
Alex Cavnar, aka alc6379
Reply With Quote  
Join Date: Sep 2004
Location: Overflow State
Posts: 183
Reputation: Stack Overflow is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: I need help with this c++ program please!!!

  #3  
Oct 12th, 2004
Greetings suptboyr,

Good to see you have an idea of how this process works. Creating a program to use mathematics in C is not difficult what so ever. It's similar to the syntax of how you write it on paper, but with easier calls.

Convert minutes to [hours and minutes]
Creating a function in C for this type of algorithm is not difficult what-so-ever. It's actually very simple if we take a closer look.

Let's take for example you send minutes to a function, say, minutesToTime(). All we would do is create a while loop, find if minutes is greater than 60, add one to our hours variable and remove 60 from our minutes variable.

void minutesToTime(int minutes) {
	int hours = 0; // Be sure to initialze to 0

	// If our variable is greater than 60
	while (minutes > 60) {
		// Remove 60 from our variable
		minutes -= 60;

		// Add 1 to our hours [as 60 minutes is equivelant to one hour]
		hours++;
	}

	// Just print our new calculation
	printf("%d:%d\n", hours, minutes);
}
This is simple. We know that while loops continue again as our expression is true. As long as minutes is greater than 60, our while loop will continue. The reason this loop will break after a period of time is because we subtracted 60 every time our loop was called. Analogy:

As long as there are 3 pies then...
ยป Eat one

You will probably say our loop will break after the 3rd time. Our -= call was put there to eat 60 minutes if there were over 60 minutes originally.

So if we sent 200 minutes to our function, it would produce an answer of 3 hours and 20 minutes. Now of course we would need to increase the current time set.

Get time set by user
This is not hard what so ever. Just a few function calls, and we can get what a user entered in. In this case we can use scanf(), as it reads data from the standard input (stdin) and stores it into the locations given by argument(s).

We could probably write a function called getInput() for simplicity:

void getInput(void) {
	int hour, minute;
	char c;

	// Ask for input
	printf("Enter hours, minutes, and (A)m or (P)m: ");

	// Get input
	if (scanf("%d %d %c", &hour, &minute, &c) == 3) {
		// Send data to global variable
	}
}
The context of this is also quite simple. scanf() is similar to the printf syntax, though with different rules. As scanf() returns the value of successfully read arguments, and preceding the reference operator (&) on standard variables, it should work appropiately.

Once we send our data from getInput() to the global variable, it will not be hard to add our new numbers from minutesToTime(). Of course, I did not add any checking if the hours combined would be greater than 12 and if so to change PM to AM or vice versa, but that in itself will not be difficult.

Conclusion
Using the C language is not difficult for simple tasks. Once you understand the syntax, everything else should make sense. Using the printf() and scanf() calls require the include of <stdio.h>. If you have further questions, please feel free to ask.


- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote  
Join Date: Oct 2004
Posts: 2
Reputation: suptboyr is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
suptboyr suptboyr is offline Offline
Newbie Poster

Re: I need help with this c++ program please!!!

  #4  
Oct 12th, 2004
Originally Posted by alc6379
Can you write some pseudo code or something? Just outline what you've thought up in an indented- type format, so you can logically see what you're thinking of. Then, perhaps post that here, and let us help you with specific points of your reasoning.


Ok i will stay up tonight and do as best as i can with pseudo code. Keep in touch .
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 1:49 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC