Thread: Problem with a program

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    30

    Problem with a program

    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:
    1. include <iostream>
    2. using namespace std;
    3. void get_current_time ( int& hours, int& minutes, int& hours_waited, int& minutes_waited );
    4. // Reads two integers from the input of current time.
    5. void give_results( int final_hours, int final_minutes );
    6. int main()
    7. {
    8. int hours, minutes, hours_waited, minutes_waited, final_hours, final_minutes;
    9. char answer = 'y'; // Allows the user to recall the program.
    10. do
    11. {
    12. cout << "We are going to give the time it will be after you wait a certain ammount of time." << endl;
    13. get_current_time ( hours, minutes, hours_waited, minutes_waited );
    14. final_hours = hours + hours_waited % 12;
    15. final_minutes = minutes + minutes_waited;
    16. give_results ( final_hours, final_minutes );
    17. if ( final_hours > 1300 ){
    18. cout << " PM." << endl;
    19. }
    20. else if ( final_hours < 1300 ) {
    21. cout << " AM." << endl;
    22. }
    23. cout << "Would you like to run this program again?" << endl;
    24. cout << "( Y - Yes , N - No )" << endl;
    25. cin >> answer;
    26. }
    27. while ( answer == 'Y' || answer == 'y' );
    28. }
    29. void get_current_time ( int& hours, int& minutes, int& hours_waited, int& minutes_waited )
    30. {
    31. char colon;
    32. cout << "Please enter the current time in 24 hour notation. " <<endl;
    33. cout << "(Hours:Minutes) : ";
    34. cin >> hours >> colon >> minutes;
    35. cout << endl;
    36. cout << "Please enter the ammount of time you waited." << endl;
    37. cout << "(Hours:Minutes) : ";
    38. cin >> hours_waited >> colon >> minutes_waited;
    39. cout << endl;
    40. }
    41. void give_results ( int final_hours, int final_minutes )
    42. {
    43. cout << "After waiting the time will be ";
    44. if ( final_hours > 24) {
    45. final_hours -= 24;
    46. }
    47. if ( final_minutes > 59 ) {
    48. final_hours + 1;
    49. }
    50. if ( final_minutes > 59 ) {
    51. final_minutes -= 60;
    52. }
    53. cout << final_hours << ":" << final_minutes;
    54. }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    I'll bet your final_hours is not > 1300. Thats a lot of hours.

  3. #3
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Change this also...

    Code:
    if ( final_minutes > 59 ) 
    {
         final_hours + 1;
    }

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    30
    The reason why I use that is so i don't get an output of say, 3:60 AM

  5. #5
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    It should be

    Code:
    final_hours += 1;
    or
    Code:
    final_hours++;

    Code:
    final_hours + 1; //does nothing

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Program
    By slick00_2 in forum C++ Programming
    Replies: 5
    Last Post: 09-22-2010, 01:20 AM
  2. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  3. Math Equation Program (I can't find the problem with my program!)
    By masked_blueberr in forum C Programming
    Replies: 14
    Last Post: 07-06-2005, 11:53 AM
  4. program problem...
    By ashesh in forum C Programming
    Replies: 5
    Last Post: 06-10-2005, 09:35 PM
  5. program problem
    By Korn1699 in forum C++ Programming
    Replies: 6
    Last Post: 11-06-2001, 05:59 PM