Hello,

I'm a little lost as to how to get the day part of my function to come up properly without a garbage number as it wants to do at the moment. I have everything else working well until it doesn't return the day on line .

#include<iostream>
#include<cmath>

void getTime(int& cHours,int& cMinutes,int& hWait, int& mWait, char colon);
void timeCalc(int& cHours,int& cMinutes,int& hWait, int& mWait, int& waitH,int& waitM);
void printTime(int& cHours,int& cMinutes,int& hWait, int& mWait, int& waitH,int& waitM, int& nDays);
char getResponse();


int main()
{
    int cHours,cMinutes,hWait,mWait,waitH,waitM,nMin,nDays;
    char rerun,colon;

    do
    {
         getTime(cHours,cMinutes,hWait,mWait,colon);
	     timeCalc(cHours,cMinutes,hWait,mWait,waitH,waitM);
	     printTime(cHours,cMinutes,hWait,mWait,waitH,waitM,nDays);
	     rerun = getResponse();

    }while(rerun == 'y');
    return 0;
}

void getTime(int&  cHours,int& cMinutes,int& hWait, int& mWait, char colon)
{
     using namespace std;
    
     cout << "\nPlease enter the current time in 24 hour notation HH:MM." << endl;
     cin  >> cHours >> colon >> cMinutes;
     cout << "\nPlease enter the amount of time to wait. Example HH:MM." << endl;
     cin  >> hWait >> colon >> mWait;

    //cout << "\nThe time is: " << cHours << colon << cMinutes << endl;
    //cout << "\nThe wait is: " << hWait << colon << mWait <<endl;
    
}

void timeCalc(int& cHours,int& cMinutes,int& hWait, int& mWait, int& waitH,int& waitM)
{
     using namespace std;
     int nHours,nDays;

     waitH = cHours + hWait;
     waitM = cMinutes + mWait;
        
     if ( waitM > 59 )
	 {
      nHours = waitM /60;
      waitM = waitM % 60;
      waitH = waitH + nHours;
      
      cout << waitH << " hours" <<endl;
      //cout << nHours << "hours2";
      //cout << waitM << "minutes";
	  
      nDays = waitH / 24;
	  //waitH = waitH % 24;
	  //nDays = waitH + days;
      
      //cout << days << "\n days";
      //cout << waitH << " hours";
      cout << nDays << " new days" << endl;;
      return;     
     }
     else( waitM <=59);
     {
     		       printTime(cHours,cMinutes,hWait,mWait,waitH,waitM,nDays);
     }


}
void printTime(int& cHours,int& cMinutes,int& hWait, int& mWait, int& waitH,int& waitM, int& nDays)
{
     using namespace std;


       cout << "\nThe time after the waiting period will be." <<endl;
       if( nDays >= 1)
       {
       cout << nDays << " day(s) and" << waitH << ":" << waitM << " and minute(s)";
       }
       else( nDays < 1);
       {
             cout << waitH << ":" << waitM;
       }      
     cout << endl;

   
}
       

char getResponse()//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.
     int cHours,cMinutes,hWait,mWait,waitH,waitM;
     char response,rerun,colon;//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(cHours,cMinutes,hWait,mWait,colon);
		       timeCalc(cHours,cMinutes,hWait,mWait,waitH,waitM);
	           rerun = getResponse();
          }
          else
          {
               cout << "\nGoodbye!";//If user chooses n then program terminates.
		       cout << endl;
          }     
     }while(response != 'y' && response != 'n');
     response = rerun;
}

Recommended Answers

All 4 Replies

On lines 60, 66 and 84.

try initializing your variables. in your function void timeCalc, if line 70 executes, days will equal a garbage value.

days goes to printTime as nDays and you immedietly start using it in comparisons on line 80.

Thanks,
I don't know why i didn't see it earlier. I added the variable nDays to the function calls of timeCalc and printTime and its producing the correct values.

Something else I noticed, nDays in main and nDays in timeCalc are not the same variable. When you pass nDays from main to your printTime, it is a garbage value regardless of how timeCalc finishes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.