Thread: Minute Program output problems

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    4

    Minute Program output problems

    Hey, i'm just learning c++ and a program i wrote awhile ago i am going back to fix and am having 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);
    }

  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    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);
    }
    -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

  3. #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

  4. #4
    Comment your source code! Lynux-Penguin's Avatar
    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);
    }
    -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

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    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

  6. #6
    Comment your source code! Lynux-Penguin's Avatar
    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

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    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

  8. #8
    Comment your source code! Lynux-Penguin's Avatar
    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

  9. #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.

  10. #10
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    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;
    Does that help?

  11. #11
    Comment your source code! Lynux-Penguin's Avatar
    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

  12. #12
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    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.

  13. #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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Minute to hour/minute conversion program
    By Remius in forum C Programming
    Replies: 7
    Last Post: 12-29-2007, 08:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Redirecting program output straight to an edit control
    By bennyandthejets in forum C++ Programming
    Replies: 5
    Last Post: 07-05-2004, 08:25 AM
  4. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM
  5. Problems with output formatting
    By supaben34 in forum C++ Programming
    Replies: 0
    Last Post: 11-22-2002, 11:22 PM