Thread: Using the Modulus Operator

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    2

    Question Using the Modulus Operator

    Hi Everyone,
    I'm new and I'm stuck. I will try to make sure that I post this correctly. The assignment is:
    Create at least two function beside the main function. One to take user input and one to display the output. It is your option to create a 3rd
    function to do the computation or do the computation in the main(). The
    program must also handle up to 24 hours of waiting time,ie (the next day).
    For example if the start time is 22:00 and the wait time is 4:10. then
    the correct result is 2:10, not 26:10. (Modulus operator helps here)
    You will need to use reference parameters to do handle user input.
    I think that I have everything I need. When I do my calculation I can get the correct result for the above example, but with any other input I do not get the correct results i.e. 13:25 current time and 2:20 added time = 15:45 new time. Two questions first am I going in the right direction for my calculation? Secondly, I can not get a colon to work with my input of hours and minutes, I have to use a space in between the hours and minutes i.e. 22 00 and 4 10 and suggestions on how to get this to work properly without having the hours and minutes appear on seperate lines? Thanks for any suggestions. Here is the code I have so far:
    Code:
    #include <iostream>
    using namespace std;
    
    // function declarations
    void get_input(int& hours, int& minutes);
    int wait_time(int hours, int minutes);
    void give_output(int hours, int minutes, int wait_time);
    
    int main()
    {
      int current_time, added_time, new_time;
      char answer;
      
      do
      {
        get_input(current_time, added_time);    
        new_time = wait_time(current_time, added_time);
        give_output(current_time, added_time, new_time);
    
       
              
    
        cout << endl << "Enter Y or y to continue, any other halts program: ";
        cin >> answer;   
        
      } while ((answer == 'Y') || (answer == 'y'));
    
      
    } 
    void get_input(int& hours, int& minutes)
    {
      cout << "Compute completion time from current time and waiting period.\n"
              << "Current time:\n"
              << "Enter 24 hour time in the format HH:MM\n";  
      cin >> hours >> minutes;
    
      
      cout << endl << endl << "Waiting time:\n"
              << "Enter 24 hour time in the format HH:MM\n";
      cin >> hours >> minutes;
     
    }            
    void give_output(int hours, int minutes, int new_time)
      {
        cout << endl << "Completion Time in 24 hour format: " << new_time << ":"; 
      
    // special handling for leading 0s on the minutes
       cout.width(2);
       cout.fill('0');
       cout << minutes;
    }
    
    int wait_time(int hours, int minutes)
    {  
       int new_time, current_time, added_time;
       if (new_time > 24)
       {
          hours = current_time + added_time;
          new_time = hours % 24;
       }
       else 
       {
          new_time = current_time + added_time;
          
       }  
    }

  2. #2
    Registered User
    Join Date
    Apr 2007
    Posts
    102
    Well I'm not going to say much, but one problem I see right off the back.

    Code:
    new_time = wait_time(current_time, added_time);
    
    //....
    
    int wait_time(int hours, int minutes)
    {  
       int new_time, current_time, added_time;
       if (new_time > 24)
       {
          hours = current_time + added_time;
          new_time = hours &#37; 24;
       }
       else 
       {
          new_time = current_time + added_time;
          
       }  
    }
    Look through those pieces and you will find many problems that could cause many miscalculations.

    Also there is are similiar problems in your give_output function.
    My Favorite Programming Line:
    Code:
    #define true ((rand() % 2) ? true : false)

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Two questions first am I going in the right direction for my calculation?
    Hard to tell, as it probably won't work at all. Take into account that variables need to be initialized, sum of minutes can be over 60 and sum of hours can be over 24. Yes, modulo helps.

    Secondly, I can not get a colon to work with my input of hours and minutes, I have to use a space in between the hours and minutes i.e. 22 00 and 4 10 and suggestions on how to get this to work properly without having the hours and minutes appear on seperate lines?
    Either read the colon into a single character, so typing (HH:MM[Enter]) would work (as well as HH[Enter]:[Enter]MM[Enter] etc), or read user input into a string and then use stringstream to parse it into hours and minutes.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    2
    Thanks for the advice. I did get the colon to work by using the following.
    Code:
    char colon; 
    cin >> hours >> colon >> minutes;
    Now to figure the rest out.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    new_time in wait_time() is uninitialized. Also, you have no way of returning the values to the calling function. You might want to use references.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. modulus operator with double operands?
    By chickenandfries in forum C Programming
    Replies: 7
    Last Post: 03-28-2008, 07:21 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. modulus operator / '%' question
    By musikluvah in forum C Programming
    Replies: 4
    Last Post: 01-17-2006, 12:09 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM