My question for this is regarding a time computation error I am having. Rather than outputting AM or PM, regardless of what I input, I get AM. Is it the if/ else statement or should I use a for statement instead.
Code:
- include <iostream>
- using namespace std;
- void get_current_time ( int& hours, int& minutes, int& hours_waited, int& minutes_waited );
- // Reads two integers from the input of current time.
- void give_results( int final_hours, int final_minutes );
- int main()
- {
- int hours, minutes, hours_waited, minutes_waited, final_hours, final_minutes;
- char answer = 'y'; // Allows the user to recall the program.
- do
- {
- cout << "We are going to give the time it will be after you wait a certain ammount of time." << endl;
- get_current_time ( hours, minutes, hours_waited, minutes_waited );
- final_hours = hours + hours_waited % 12;
- final_minutes = minutes + minutes_waited;
- give_results ( final_hours, final_minutes );
- if ( final_hours > 1300 ){
- cout << " PM." << endl;
- }
- else if ( final_hours < 1300 ) {
- cout << " AM." << endl;
- }
- cout << "Would you like to run this program again?" << endl;
- cout << "( Y - Yes , N - No )" << endl;
- cin >> answer;
- }
- while ( answer == 'Y' || answer == 'y' );
- }
- void get_current_time ( int& hours, int& minutes, int& hours_waited, int& minutes_waited )
- {
- char colon;
- cout << "Please enter the current time in 24 hour notation. " <<endl;
- cout << "(Hours:Minutes) : ";
- cin >> hours >> colon >> minutes;
- cout << endl;
- cout << "Please enter the ammount of time you waited." << endl;
- cout << "(Hours:Minutes) : ";
- cin >> hours_waited >> colon >> minutes_waited;
- cout << endl;
- }
- void give_results ( int final_hours, int final_minutes )
- {
- cout << "After waiting the time will be ";
- if ( final_hours > 24) {
- final_hours -= 24;
- }
- if ( final_minutes > 59 ) {
- final_hours + 1;
- }
- if ( final_minutes > 59 ) {
- final_minutes -= 60;
- }
- cout << final_hours << ":" << final_minutes;
- }