0

Hello,

I had quite a few bugs with this program and it's fore-bearer. I got all but one of them fixed and it's in one of my if-else loops. When the program runs it runs good and then when it gets to the loop it only pulls out the first part of the if-else and only shows it in the AM mode. The PM mode should be the one that is used. The if loop that is called when the time is greater than a certain time works fine. I am probably sure it's going to be a simple problem, but I can't seem to grasp it. The if-else loop that is not working is on lines 88 - 92.

#include<iostream>
#include<cmath>

void getTime(int&hour, int& minute, int& waitH, int& waitM, char& let1, char& let2, char& colon);//Declaration for the function that receives input from the user.
void conversion(int& hour, int& minute, int& waitH, int& waitM, int& days, int& nHours, int& min1, char& colon);//Declaration for the function that does the calculation converting seconds into minutes, hours and days.
void printTime(int& days, int& nHours, int& min1, char& let1, char& colon);//Declaration for the function that prints out the results onto the screen.
char getResponse(int& hour, int& minute, int& waitH, int& waitM, int& days, int& nHours, int& min1, char& let1, char& let2, char& colon);//Declaration for the funtion that asks the user if he/she would like to repeat the program.
const int SECONDS_IN_DAY = 86400;//Constant for the number of seconds in a day.
const int SECONDS_IN_HOUR = 3600;//Constant for the number of seconds in an hour.
const int SECONDS_IN_MINUTE = 60;//Constant for the number of seconds in a minute.
const int TWELVE_HR_NOTATION = 12;//Constant for the 12 hour notation.
const int SECONDS_IN_ELEVEN_FIFTY_NINE = 43140;//Constant for seconds in 12 hours.


int main()
{

    int  hour, minute, waitH, waitM, nHours, days, sec, min1;//Variables of the type int used in the program.
    char rerun, colon = ':', let1, let2;//Variables of the type char used in the program.
    do//Beginning of the do-while loop in the main body
    {
         getTime(hour, minute, waitH, waitM, let1, let2, colon);//Calls the results of the getTime function in to the body of main.
         conversion(hour, minute, waitH, waitM, days, nHours, min1, colon);//Calls the results of the conversion function in to the body of main.
         printTime(days, nHours, min1, let1, colon);//Calls the results of the printTime function in to the body of main.
         rerun = getResponse(hour, minute, waitH, waitM, days, nHours, min1, let1, let2, colon);//Calls the getResponse function allowing the program to repeat if desired.

    }while(rerun == 'y');//End of the do-while loop in the main body.
    return 0;
    }
    
    void getTime(int& hour, int& minute, int& waitH, int& waitM, char& let1, char& let2, char& colon)//Function definition for the getTime function.
    {
         using namespace std;//Used locally in the function now.
    //Asks the users for information to begin the calculations to convert the numbers.
         cout << "\nPlease enter the current time: Example HH:MM AM or PM." << endl;//Times entered are in 24 hour notation.
         cin  >> hour >> colon >> minute >> let1 >> let2;
         cout << "\nPlease enter the amount of time to wait. Example HH:MM." << endl;//Times entered are in 24 hour notation.
         cin  >> waitH >> colon >> waitM;

         //cout << "\nThe time is: " << hour << colon << minute << let1 << let2 << endl;
         //cout << "\nThe wait is: " << waitH << colon << waitM << endl;
    
    }

    void conversion(int& hour, int& minute, int& waitH, int& waitM, int& days, int& nHours, int& min1, char& colon)//Function definition for the conversion function.
    {
         
         using namespace std;//Used locally in the function now.
         int tSec, totalH, totalM, hours;//Local variables.
     
         totalH = hour + waitH;//Combining the current hours and the waiting hours into one total.
         totalM = minute + waitM;//Combining the current minutes and the waiting minutes into one total.
         //cout <<endl << totalH << " and " << totalM << let1 << let2 << endl;//Test to make sure program is outputting the numbers correctly as entered.

         tSec = (totalH * SECONDS_IN_HOUR) + (totalM * SECONDS_IN_MINUTE);//This algorithm multiplies hours and minutes by seconds to get the total seconds.
         //cout << endl << tSec << " total seconds" << endl;//Test to make sure program is outputting the numbers correctly.
     
         min1 = (tSec / SECONDS_IN_MINUTE) % SECONDS_IN_MINUTE;//The first step in the modulus division algorithm. This step divides total seconds by seconds in minutes and then mod divides the result returning the amount of minutes if over 59 minutes.
            //cout << endl << min1 << " minutes" << endl;//Test to make sure program is outputting the numbers correctly.
         hours = ((tSec % SECONDS_IN_DAY) / SECONDS_IN_HOUR);//The second step in the modulus division algorithm. This step modulus divides total seconds by seconds_in_day and then divides the total by seconds_in_hour.
         //cout << endl << hours << " hours" << endl;//Test to make sure program is outputting the numbers correctly.
         days = (tSec / SECONDS_IN_DAY);//The last stage of the modulus division divides out the number of days if any.
         //cout << days << " days" << endl;//Test to make sure program is outputting the numbers correctly.

         nHours = hours % TWELVE_HR_NOTATION;
         //cout << nHours << " 12 hour notation" << endl;

    return;
    }

    void printTime(int& days, int& nHours, int& min1, char& let1, char& colon)//Function definition for the printTime function
    {
         
         using namespace std;//Used locally in the function now.

         int total;
         
         total = (days * SECONDS_IN_DAY) + (nHours * SECONDS_IN_HOUR) + (min1 * SECONDS_IN_MINUTE);
         //cout << total << " total" << endl;
         //cout << let1 << endl;
          
         if ( total <= SECONDS_IN_ELEVEN_FIFTY_NINE )
          {
              cout << endl << days << " # of days.";//Outputs the number of days if the number of hours exceeds 24 hours.
              cout << endl << "\nThe time after the waiting period will be: " << nHours << colon <<  min1 << " AM" << endl;//This will output the time after the waiting period.
         }
         else if( let1 == 'P' )
         {
              cout << endl << days << " # of days.";//Outputs the number of days if the number of hours exceeds 24 hours.
              cout << endl << "\nThe time after the waiting period will be: " << nHours << colon <<  min1 << " PM" << endl;//This will output the time after the waiting period.
         }   
         if( total >= SECONDS_IN_DAY )
         {              
                        
              if( nHours == 0 )
              {
                   nHours = TWELVE_HR_NOTATION;
              }              
              cout << endl << days << " # of days.";//Outputs the number of days if the number of hours exceeds 24 hours.
              cout << endl << "\nThe time after the waiting period will be: " << nHours << colon <<  min1 << " AM" << endl;//This will output the time after the waiting period.
         }
 
    } 
    char getResponse(int& hour, int& minute, int& waitH, int& waitM, int& days, int& nHours, int& min1, char& let1, char& let2, char& colon)//This is the function that will ask for input from the user so the program will either rerun of terminate..
    {
         
         using namespace std;//Used per function no longer used globally.
         char response,rerun;//Local variables to this function.
         do//Beginning of the do-while loop.
         {
              cout << "\nDo you want to continue? (y for yes, n for no): ";//Prompt asking if continuation is wanted.
              cin  >> response;

              if (response != 'y' && response != 'n' )//The if-loop to make sure the user inputs a y or n to get appropriate input.
              {
                   cout << "\nInvalid input, enter y or n" << endl;//Output asking user to make sure to use correct input. 
              }
              else if (response == 'y')
              {
                   getTime(hour, minute, waitH, waitM, let1, let2, colon);//Calls the results of the getTime function in to the body of main.
                   conversion(hour, minute, waitH, waitM, days, nHours, min1, colon);//Calls the results of the conversion function in to the body of main.
                      printTime(days, nHours, min1, let1, colon);//Calls the results of the printTime function in to the body of main.
                   rerun = getResponse(hour, minute, waitH, waitM, days, nHours, min1, let1, let2, colon);//Calls the getResponse function allowing the program to repeat if desired.

              }     
              else ( response == 'n');
              {

              }
    
    }while(response != 'y' && response != 'n');//End of the do-while loop in the getResponse function.
    response = rerun;//response from user equals rerun.
    return rerun;//Returns rerun to main.
    }

Edited by jmcorpse: added the line #'s

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by jonsca
0

If the user inputs 3:00 pm you need to convert that into 15 hours on line 56 so that you can compare it accurately against your seconds up to 11:59 am in line 83.

Otherwise you are comparing 3 to 12, which is less than, thereby outputting your AM value.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.