# Using the Modulus Operator

• 06-29-2007
tlove71
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;         }  }```
• 06-29-2007
JJFMJR
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.
• 06-29-2007
anon
Quote:

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.

Quote:

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.
• 06-29-2007
tlove71
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.
• 06-30-2007
dwks
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.