![]() |
| | #1 |
| Registered User Join Date: Oct 2003
Posts: 4
| Minute Program output problems Specs: obtain minutes from user then output as hours:minutes example: input 327 from user output 5:27 problem i'm having is if they input 302 etc. i get 5:2 rather than 5:02 or if they put 300 minutes it comes out with 5:0 My book says it's possible and says (hint: Use the modulus operator) what i have: =========================== Code: #include <iostream.h>
int main ()
{
int minutes;
cout << endl;
cout << endl;
cout << "\tEnter the number of minutes: ";
cin >> minutes;
cout << "\tThis is " << (minutes/60) << ":" << (minutes%60);
cout << endl;
cout << endl;
cout << endl;
cout << "\t";
return (0);
}
|
| Cobalt is offline | |
| | #2 |
| Comment your source code! Join Date: Apr 2002
Posts: 533
| Code: #include <iostream.h>
int main ()
{
int minutes;
cout << endl;
cout << endl;
cout << "\tEnter the number of minutes: ";
cin >> minutes;
cout << "\tThis is " << (minutes/60) << ":";
if((minutes%60)<10)
cout<<"0"<<minutes%60;
else
cout<<minutes%60;
cout << endl;
cout << endl;
cout << endl;
cout << "\t";
return (0);
}
__________________ Asking the right question is sometimes more important than knowing the answer. Please read the FAQ C Reference Card (A MUST!) Pointers and Memory The Essentials CString lib |
| Lynux-Penguin is offline | |
| | #3 |
| Registered User Join Date: Oct 2003
Posts: 4
| Thanks for that speedy response man i was pretty suprised, but i can't use if else statements or any if type statements says i am able to do it with simply using mod |
| Cobalt is offline | |
| | #4 |
| Comment your source code! Join Date: Apr 2002
Posts: 533
| i dont think you can because the '0' won't be included if it's less than 10 no matter what, but if it explicitly says just ifs and elses try: Code: #include <iostream.h>
#include <iomanip.h>
int main ()
{
int minutes;
cout << endl;
cout << endl;
cout << "\tEnter the number of minutes: ";
cin >> minutes;
cout << "\tThis is " << (minutes/60) << ":";
minutes%60<10 ? cout<<"0"<<minutes%60 : cout<<minutes%60;
cout << endl;
cout << endl;
cout << endl;
cout << "\t";
return (0);
}
__________________ Asking the right question is sometimes more important than knowing the answer. Please read the FAQ C Reference Card (A MUST!) Pointers and Memory The Essentials CString lib |
| Lynux-Penguin is offline | |
| | #5 |
| C++ Developer Join Date: Jun 2002 Location: UWaterloo
Posts: 2,718
| Code: #include <iostream> //iostream, not iostream.h
#include <iomanip> // for setw
using namespace std;
int main ()
{
int minutes;
cout << endl;
cout << endl;
cout << "\tEnter the number of minutes: ";
cin >> minutes;
cout << "\tThis is " << (minutes/60) << ":" << setw( 2 )<< (minutes%60);
cout << endl;
cout << endl;
cout << endl;
cout << "\t";
return (0);
}
__________________ Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie |
| XSquared is offline | |
| | #6 |
| Comment your source code! Join Date: Apr 2002
Posts: 533
| XSquared is right but that doesn't output a 0 it just has a space. -LC
__________________ Asking the right question is sometimes more important than knowing the answer. Please read the FAQ C Reference Card (A MUST!) Pointers and Memory The Essentials CString lib |
| Lynux-Penguin is offline | |
| | #7 |
| C++ Developer Join Date: Jun 2002 Location: UWaterloo
Posts: 2,718
| Code: cout << "\tThis is " << (minutes/60) << ":" << setfill( '0' ) << setw( 2 )<< (minutes%60);
__________________ Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie |
| XSquared is offline | |
| | #8 |
| Comment your source code! Join Date: Apr 2002
Posts: 533
| wow, i'll be darned there is a way. I didn't know about setfill tho. hmm, that could prove usefull. -LC
__________________ Asking the right question is sometimes more important than knowing the answer. Please read the FAQ C Reference Card (A MUST!) Pointers and Memory The Essentials CString lib |
| Lynux-Penguin is offline | |
| | #9 |
| Registered User Join Date: Oct 2003
Posts: 4
| Write a program that allows the user to enter a time in minutes and then displays the time in hours:minutes format. The program output should look similar to: Enter teh number of minutes: 327 This is 5:27 Be sure toe consider ime where the number of minutes left over is fewer than 10. For example, 184 minutes should display 3:04. (Hint: use the modulus operator.) This is the actual problem that is in my textbook, it is in the lawrenceville press A guide to programming in C++ book, and because it says use modulus operator it makes me think i have to use it 2x's or something but it's in chapter 3 of the book so it's simple stuff no other binaries or if statments etc. so it must be possible since it's a published problem in a educational textbook. |
| Cobalt is offline | |
| | #10 |
| Registered User Join Date: Jul 2003
Posts: 1,088
| Code: int num = 502; int digit = num % 10; // Right now: digit = 2; num = num / 10; digit = num % 10; // Right now: digit = 0; num = num / 10; digit = num % 10; // Right now: digit = 5; |
| jlou is offline | |
| | #11 |
| Comment your source code! Join Date: Apr 2002
Posts: 533
| other than that it's not 0 will never appear before a number without a special flag set or a function to output it as such. Or an IF statement. [edit]: I am again proven wrong! -LC
__________________ Asking the right question is sometimes more important than knowing the answer. Please read the FAQ C Reference Card (A MUST!) Pointers and Memory The Essentials CString lib |
| Lynux-Penguin is offline | |
| | #12 |
| Registered User Join Date: May 2003
Posts: 1,199
| Well, you could always do: cout << "\tThis is " << (minutes/60) << ":" << ((minutes%60)/10) << ((minutes%60)%10);
__________________ You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards. |
| Cat is offline | |
| | #13 |
| Registered User Join Date: Oct 2003
Posts: 4
| Thanks for the awesome help guys i really appreciate it, it works great now, glad i've found somewhere i can get help if i need it. |
| Cobalt is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Minute to hour/minute conversion program | Remius | C Programming | 7 | 12-29-2007 08:39 AM |
| Using variables in system() | Afro | C Programming | 8 | 07-03-2007 12:27 PM |
| Redirecting program output straight to an edit control | bennyandthejets | C++ Programming | 5 | 07-05-2004 08:25 AM |
| Output problems with structures | Gkitty | C Programming | 1 | 12-16-2002 05:27 AM |
| Problems with output formatting | supaben34 | C++ Programming | 0 | 11-22-2002 11:22 PM |