2 Replies - 12568 Views - Last Post: 08 February 2011 - 02:11 AM Rate Topic: -----

#1 ftwjake  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-February 11

C++ time machine problem

Posted 07 February 2011 - 09:07 PM

This is from Problem Solving with C++ by Walter Savitch, 7th Edition.

Your time machine is capable of going forward in time up to 24 hours. The machine is configured to jump ahead in minutes. To enter the proper number of minutes into your machine, you would like a program that can take a start time (in hours, minutes,n and a Boolean indicating AM or PM) and a future time (in hours, minutes,n and a Boolean indicating AM or PM) and calculate the difference in minutes between the start and future time.
...

Apparently my program is returning wrong values. Can anyone isolate where the problem is occurring?

#include <iostream>

using namespace std;

int computeDifference(int hours1, int mins1, int hours2, int mins2, bool isPM1, bool isPM2); //calculates the time difference between the starting and ending time in minutes

int main()
{
    int hours1, mins1, hours2, mins2;
    bool isPM1 = 0, isPM2 = 0;
    char time1, time2;
    
    cout << "Please enter the starting hour: \n";
    cin >> hours1;
    cout << "Please enter the starting minute: \n"; 
    cin >> mins1;
    cout << "Please indicate whether it is AM or PM (enter A for AM; enter P for PM) \n";
    cin >> time1;
    if ((time1 == 'p') || (time1 == 'P'))
       {
       isPM1 = 1;
       }
   
    cout << "Please enter the hour you would like to travel to: \n";
    cin >> hours2;
    cout << "Please enter the minute of the hour you would like to travel to: \n";
    cin >> mins2;
    cout << "Please indicate whether it is AM or PM (enter A for AM; enter P for PM) \n";
    cin >> time2;
    if ((time2 == 'p') || (time2 == 'P'))
       {
       isPM2 = 1;
       }
    
    
    
    cout << "The time difference is: ";
    cout << computeDifference(hours1, mins1, hours2, mins2, isPM1, isPM2);
    if (computeDifference(hours1, mins1, hours2, mins2, isPM1, isPM2) == 1)
       cout << " minute.\n";
    else
       cout << " minutes.\n";


    
    system("PAUSE");
    return 0; 
}
int computeDifference(int hours1, int mins1, int hours2, int mins2, bool isPM1, bool isPM2)
{
 int difference, total_mins1, total_mins2;

 if (isPM1)
 {
    if ((hours1 >= 1) && (hours1 < 12))
    {
       hours1 += 12;
    }
 }
 if (isPM2)
 {
    if (hours2 >= 1 && hours2 < 12)
    {
    hours2 += 12;
    }
 }
 total_mins1 = (hours1 * 60) + mins1;
 total_mins2 = (hours2 * 60) + mins2;

 if ((hours1 >= hours2) || ((hours1 == hours2) && (mins1 > mins2)))
 {
 total_mins2 += 1440;          
 }
 
 
 difference = total_mins2 - total_mins1;
 
 if (difference > 1440)
 {
    difference -= 1440;
 }
 return difference;
}



Is This A Good Question/Topic? 0
  • +

Replies To: C++ time machine problem

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2015
  • View blog
  • Posts: 5,411
  • Joined: 27-December 05

Re: C++ time machine problem

Posted 08 February 2011 - 12:14 AM

View Postftwjake, on 07 February 2011 - 11:07 PM, said:

Apparently my program is returning wrong values. Can anyone isolate where the problem is occurring?

What do you mean, "apparently"? Have you at least isolated under which circumstances the problem is occurring?
Was This Post Helpful? 0
  • +
  • -

#3 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2130
  • View blog
  • Posts: 4,196
  • Joined: 30-May 10

Re: C++ time machine problem

Posted 08 February 2011 - 02:11 AM

> Apparently my program is returning wrong values. Can anyone isolate where the problem is occurring?
First you need to say which test case(s) pass or fail. For example 1010a to 1111a works, but 1010a to 1010p doesn't.

Also, now is a REALLY good time to get acquainted with your debugger.

Here is an example (blue text is annotation)

Compile for debug
$ g++ -g foo.cpp
Run the code in the debugger
$ gdb ./a.out
GNU gdb (GDB) 7.1-ubuntu
Copyright © 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/sc/work/a.out...done.
Pick a spot where you would like to stop (where the trouble seems to start)
(gdb) break computeDifference
Breakpoint 1 at 0x804894d: file foo.cpp, line 50.
(gdb) run
Starting program: /home/sc/work/a.out
Please enter the starting hour:
10
Please enter the starting minute:
10
Please indicate whether it is AM or PM (enter A for AM; enter P for PM)
a
Please enter the hour you would like to travel to:
11
Please enter the minute of the hour you would like to travel to:
11
Please indicate whether it is AM or PM (enter A for AM; enter P for PM)
a

Breakpoint 1, computeDifference (hours1=10, mins1=10, hours2=11, mins2=11, isPM1=false, isPM2=false) at foo.cpp:50
50 if (isPM1)
Step the code one statement at a time
(gdb) s
57 if (isPM2)
(gdb) s
64 total_mins1 = (hours1 * 60) + mins1;
(gdb) s
65 total_mins2 = (hours2 * 60) + mins2;
Mmm, this is interesting... (or is it?)
(gdb) print total_mins1
$1 = 610


If you're using some kind of IDE, then you can do all this by pointing and clicking on your source code.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1