Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. rookie question

rookie question

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Binary0110
    wrote on last edited by
    #1

    I have this program I have to write for school that accepts two integers. hours and minutes. I then have to output the format in 6:00 format. I'm only allowed integer division and and modulus division. I've tried everything I can think of but my output still looks like this. 6:0 Any tips? BINARY

    A 2 Replies Last reply
    0
    • B Binary0110

      I have this program I have to write for school that accepts two integers. hours and minutes. I then have to output the format in 6:00 format. I'm only allowed integer division and and modulus division. I've tried everything I can think of but my output still looks like this. 6:0 Any tips? BINARY

      A Offline
      A Offline
      Anonymous
      wrote on last edited by
      #2

      How about this?

      int main(int argc, char* argv[])
      {
      int hours = 6;
      int minutes = 7;
      int seconds = 8;

      printf("%d:%02d:%02d\\n", hours, minutes, seconds);
      
      return 0;
      

      }

      1 Reply Last reply
      0
      • B Binary0110

        I have this program I have to write for school that accepts two integers. hours and minutes. I then have to output the format in 6:00 format. I'm only allowed integer division and and modulus division. I've tried everything I can think of but my output still looks like this. 6:0 Any tips? BINARY

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        or, without using printf formatting trick, try this:

        int main(int argc, char* argv[])
        {
        int hours = 6;
        int minutes = 7;

        printf("%d:%d%d\\n", hours, minutes / 10, minutes % 10);
        
        return 0;
        

        }

        B 1 Reply Last reply
        0
        • A Anonymous

          or, without using printf formatting trick, try this:

          int main(int argc, char* argv[])
          {
          int hours = 6;
          int minutes = 7;

          printf("%d:%d%d\\n", hours, minutes / 10, minutes % 10);
          
          return 0;
          

          }

          B Offline
          B Offline
          Binary0110
          wrote on last edited by
          #4

          little too advance for a student who just started learn C++ three weeks ago. Plus It has to advance the hour when the minutes go over 59. I am so stuck. I cant even use conditional operators Here's what I got: //Preprocessing directives #include #include using namespace std; // Main Program int main() { // variable declarations int hours, minutes ; // User input cout << "Input the time as hours and minutes (e.g. 5 23 means 5:23) " ; cin >> hours >> minutes ; // Program constants const int minutes_per_hour = 59; // Computations minutes = minutes + 1; /*hours = hours + (minutes_per_hour % minutes);*/ //Output cout << " The time is: " << hours<< " :"<< setfill('0')< BINARY -- modified at 2:22 Sunday 23rd October, 2005

          PJ ArendsP A 2 Replies Last reply
          0
          • B Binary0110

            little too advance for a student who just started learn C++ three weeks ago. Plus It has to advance the hour when the minutes go over 59. I am so stuck. I cant even use conditional operators Here's what I got: //Preprocessing directives #include #include using namespace std; // Main Program int main() { // variable declarations int hours, minutes ; // User input cout << "Input the time as hours and minutes (e.g. 5 23 means 5:23) " ; cin >> hours >> minutes ; // Program constants const int minutes_per_hour = 59; // Computations minutes = minutes + 1; /*hours = hours + (minutes_per_hour % minutes);*/ //Output cout << " The time is: " << hours<< " :"<< setfill('0')< BINARY -- modified at 2:22 Sunday 23rd October, 2005

            PJ ArendsP Offline
            PJ ArendsP Offline
            PJ Arends
            wrote on last edited by
            #5

            Change setw(1) to setw(2).


            "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" - mYkel - 21 Jun '04 "There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05 Within you lies the power for good - Use it!

            Within you lies the power for good; Use it!

            1 Reply Last reply
            0
            • B Binary0110

              little too advance for a student who just started learn C++ three weeks ago. Plus It has to advance the hour when the minutes go over 59. I am so stuck. I cant even use conditional operators Here's what I got: //Preprocessing directives #include #include using namespace std; // Main Program int main() { // variable declarations int hours, minutes ; // User input cout << "Input the time as hours and minutes (e.g. 5 23 means 5:23) " ; cin >> hours >> minutes ; // Program constants const int minutes_per_hour = 59; // Computations minutes = minutes + 1; /*hours = hours + (minutes_per_hour % minutes);*/ //Output cout << " The time is: " << hours<< " :"<< setfill('0')< BINARY -- modified at 2:22 Sunday 23rd October, 2005

              A Offline
              A Offline
              Anonymous
              wrote on last edited by
              #6

              Couple more things: - your minutes_per_hour constant should 60, not 59. - your hours calculation, even though it is commented out, is not correct. - after incrementing minutes, you should probably adjust its value using the mod function (% function). Below is example code also using PJ's setw(2) change:

              // Program constants 
              const int minutes\_per\_hour = 60;
              
              // Computations
              minutes = minutes + 1;
              
              hours = hours + (minutes / minutes\_per\_hour);   // Line 1
              minutes = minutes % minutes\_per\_hour;           // Line 2
              
              // Output
              cout << " The time is: " << hours << ":"<< setfill('0') << setw(2) << minutes << endl;
              

              In Line 1, the expression "x / y" returns the integral portion of x divided by y. When x is 60 and y is 60, this expression yields 1, which will increment the hours by 1. When x is less than y, this expression yields 0, and the hours value is unchanged. In Line 2, the expression "x % y" returns the remainder value that you get when you divide x by y. If x is less than y, it returns just the value x. If x is 60 and y is 60, the remainder is 0, which handles the case when the minutes goes past 59 (you want it to display 00).

              B 1 Reply Last reply
              0
              • A Anonymous

                Couple more things: - your minutes_per_hour constant should 60, not 59. - your hours calculation, even though it is commented out, is not correct. - after incrementing minutes, you should probably adjust its value using the mod function (% function). Below is example code also using PJ's setw(2) change:

                // Program constants 
                const int minutes\_per\_hour = 60;
                
                // Computations
                minutes = minutes + 1;
                
                hours = hours + (minutes / minutes\_per\_hour);   // Line 1
                minutes = minutes % minutes\_per\_hour;           // Line 2
                
                // Output
                cout << " The time is: " << hours << ":"<< setfill('0') << setw(2) << minutes << endl;
                

                In Line 1, the expression "x / y" returns the integral portion of x divided by y. When x is 60 and y is 60, this expression yields 1, which will increment the hours by 1. When x is less than y, this expression yields 0, and the hours value is unchanged. In Line 2, the expression "x % y" returns the remainder value that you get when you divide x by y. If x is less than y, it returns just the value x. If x is 60 and y is 60, the remainder is 0, which handles the case when the minutes goes past 59 (you want it to display 00).

                B Offline
                B Offline
                Binary0110
                wrote on last edited by
                #7

                Thank you so much you people are my heroes BINARY

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups