Duration Calculator

I have been trying to write a duration calculator and I cant get the one's place value or the correct tens place value, please help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//Structures
//James Wester
#include <iostream>
#include <string>

using namespace std;

struct timeDate
{
    int hours, minutes, duration, solution, result; 
    
};    

int main()
{
    startprogram:
    
    timeDate value;
    
    
    cout << "       ,--.-----.--. " << endl;
    cout << "       |--|-----|--| " << endl;
    cout << "       |--|     |--| " << endl;
    cout << "       |  |-----|  | " << endl;
    cout << "     __|--|     |--|__ " << endl;
    cout << "    /  |  |-----|  |  \\ " << endl;
    cout << "   /   \\__|-----|__/   \\ " << endl;
    cout << "  /   ______---______   \\/\\ " << endl;
    cout << " /   /               \\   \\/ " << endl;
    cout << "{   /    _     _   _  \\   } " << endl;
    cout << "|  {    | | . | | | |  }  |-, " << endl;
    cout << "|  |    |_| . |_| |_|  |  | | " << endl;
    cout << "|  {                   }  |-' " << endl;
    cout << "{   \\                 /   } " << endl;
    cout << " \\   `------___------'   /\\ " << endl;
    cout << "  \\     __|-----|__     /\\/ " << endl;
    cout << "   \\   /  |-----|  \\   / " << endl;
    cout << "    \\  |--|     |--|  / " << endl;
    cout << "     --|  |-----|  |-- " << endl;
    cout << "       |--|     |--| " << endl;
    cout << "       |--|-----|--| " << endl;
    cout << "       `--'-----`--' " << endl;
    
    cout << " " << endl;
    cout << "Welcome to the Time Calculator! Please enter" << endl;
    cout << "your hours, minutes, and duration (in minutes)." << endl;
    cout << " " << endl;
    
    cout << "Enter your hours: ";
    cin >> value.hours;
    cout << " " << endl;
    
    if (value.hours >= 25)
    {
        cout << "This is a 24 hour clock, please enter a value from 1-24 for hours." << endl;
        cout << " " << endl;
        goto startprogram;
    }
    
    cout << "Enter your minutes: ";
    cin >> value.minutes;
    cout << " " << endl;
    
    if (value.minutes >= 61)
    {
        cout << "This is a 24 hour clock, please enter a value from 0-60 for minutes." << endl;
        cout << " " << endl;
        goto startprogram;
    }    
    
    cout << "Enter your duration: ";
    cin >> value.duration;
    cout << " " << endl;
    
    if (value.duration < 0)
    {
        cout << "This is a 24 hour clock, please enter a value from zero or greater for duration (in minutes)." << endl;
        cout << " " << endl;
        goto startprogram;
    }    
     
    value.solution = value.duration/60; 
    
    value.minutes = (value.minutes + value.hours % 60);
    
    if (value.minutes >= 61)
    {
        value.minutes = (value.minutes - 60) && (value.hours = value.hours + 1);
    }    
    
    if (value.solution < 0) 
    {
        cout << "Whoah there! Something went wrong, lets restart the program." << endl;
        cout << " " << endl;
        goto startprogram;
    }
    
    if (value.solution > 24)
    {
        value.solution = value.solution - 24;
    }
    
    value.result = value.solution + value.hours;
    
    if (value.result >= 25)
    {
        value.result = value.result - 24;
    }
    
    cout << value.result << ":" << value.minutes << endl;
    
    
    return 0;
}
Hello RegalCppNewb,

I believe that your problem is with line 84. When I commented this line the program worked the way it should. Although I did not try to break it yet.

Line 84 gives a new value to "minutes" that is not needed.

And I think that the if statements the have ">=" are wrong and may not work the way that you might expect.

Hope that helps,

Andy
May I ask how line 84 isnt necessary? I mean its taking the minutes added to the hour and placing them into the minutes value, for example 117/60 is going to be 1.95 hours, I want to get that decimal value into the minutes category
Hello RegalCppNewb,

Maybe I am misunderstanding how the program works. My initial test was 5 hours and 30 minutes with a duration 120 minuted. Now with line 84 the result was 07:35 and I expected 07:30. Without line 84 I achieved the 07:30 I was expecting. If I have this wrong let me know.

An explanation of what you want the program to do would help. Also some test data would help to understand what the program is doing. A known input with a known output helps track down problems.

Maybe I am wrong, but I thought the program worked better with what I was using at the time.

I also wonder if you understand what is happening with line 84. In the part value.minutes + value.hours % 60 the order of precedence does value.hours % 60 first then adds the result to value.minutes before it sets the final result equal to "value.minutes". Also did you want the "%" (modulo) or the "/" (division)?

All your numeric variables are defined as "int"s. Be careful when doing division because integer division will drop any part to the right of the decimal point which may not give you the answer that you are expecting.

Hope tht helps,

Andy
The program is supposed to give the time after a given event, duration is also in the form of minutes not hours.

Example input
23 (Hours)
55 (Minutes)
1880 (Duration in terms of minutes)

Example output
7:15

When I test those values I get:

7:1

Instead of the 7:15

Since the variables are ints I thought I could use the remainder of the hours value and add that to the minutes so it would display like a clock format instead of 7.96.

Welcome to the Time Calculator! Please enter
your hours, minutes, and duration (in minutes).

Enter your hours: 23

Enter your minutes: 55

Enter your duration: 1880

7:1
Last edited on
Hello RegalCppNewb,

Just so you know 7:1 is not missing the 5 it is missing a zero because it should be showing 7:01, but an int does not store the leading zero.

I have not tested it yet, but line 88 should be two lines of code. The "&&" works great in a conditional statement, but not this way.

Sorry it is late for me, so I will have to work on this tomorrow.

Hope that helps,

Andy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <cmath>
using namespace std;

//=========== timeDate and related functions ===========================

struct timeDate { int hours, minutes; };


timeDate getTime()
{
   int hours, minutes;
   string line;

   bool ok = false;
   while ( !ok )
   {
      cout << "Enter hours (0-23) and minutes (0-59) as hh:mm ... ";
      getline( cin, line );
      int pos = line.find( ':' );
      hours   = stoi( line.substr( 0, pos  ) );
      minutes = stoi( line.substr( pos + 1 ) );
      ok = hours >= 0 && hours <= 23 && minutes >=0 && minutes <= 59;
      if ( !ok ) cout << "Invalid time; try again." << endl;
   }

   return { hours, minutes };
}


timeDate operator+( timeDate a, timeDate b )     
{
   int minutes = 60 * ( a.hours + b.hours ) + a.minutes + b.minutes;
   return { ( minutes / 60 ) % 24, minutes % 60 };
}


ostream &operator << ( ostream &strm, timeDate t )
{
   return strm << setfill( '0' ) << setw( 2 ) << t.hours << ':' << setw( 2 ) << t.minutes;
}


//======================================


struct Pixel { int i, j; };

Pixel getPixel( int i0, int j0, double r, double theta )
{
   return { i0 - r * cos( theta ) + 0.5, j0 + r * sin( theta ) + 0.5 };
}


void clockFace( timeDate t )
{
   const double PI = 3.141592653589793;
   const int SIZE = 37, HOUR_RADIUS = 10, MINUTE_RADIUS = 15, DIGIT_RADIUS = 17, NUM_SPOTS = MINUTE_RADIUS;
   const char SPOT = '*';
   int i0 = ( SIZE - 1 ) / 2, j0 = i0;
   vector<string> grid( SIZE, string( SIZE, ' ' ) );
   Pixel p;

   for ( int hour = 1; hour <= 12; hour++ )
   {
      double angle = 2.0 * PI * hour / 12;
      p = getPixel( i0, j0, DIGIT_RADIUS, angle );
      grid[p.i][p.j] = (char) ( '0' + hour % 10 );
      if ( hour >= 10 ) grid[p.i][p.j-1] = '1';
   }

   double angleHours   = 2.0 * PI * ( t.hours % 12 + t.minutes / 60.0 ) / 12.0;
   double angleMinutes = 2.0 * PI * t.minutes / 60;
   for ( int r = 1; r <= NUM_SPOTS; r++ )
   {
      double frac = ( r - 0.5 ) / NUM_SPOTS;
      p = getPixel( i0, j0, HOUR_RADIUS * frac, angleHours );
      grid[p.i][p.j] = SPOT;

      p = getPixel( i0, j0, MINUTE_RADIUS * frac, angleMinutes );
      grid[p.i][p.j] = SPOT;
   }

   for ( string s : grid ) cout << s << '\n';
}

//======================================

int main()
{
   cout << "\nSTART\n";
   timeDate start = getTime();

   cout << "\nDURATION\n";
   timeDate duration = getTime();

   timeDate result = start + duration;
   cout << "\nFINAL time is " << result << "\n\n";

   clockFace( result );
}



START
Enter hours (0-23) and minutes (0-59) as hh:mm ... 15:45

DURATION
Enter hours (0-23) and minutes (0-59) as hh:mm ... 9:16

FINAL time is 01:01

                                     
                 12                  
                                     
        11                1          
                    *                
                   *                 
                   *                 
                   *                 
                   *                 
  10               *             2   
                   *   *             
                   *  *              
                   *  *              
                   * *               
                  * **               
                  * *                
                  **                 
                  **                 
 9                *                3 
                                     
                                     
                                     
                                     
                                     
                                     
                                     
                                 4   
   8                                 
                                     
                                     
                                     
                                     
                                     
          7               5          
                                     
                  6                  
                                     
Last edited on
Welp, time to learn what lastchance's code means lol, thank you both for all the attention and assistance you all have given me, I appreciate it greatly.
Hello RegalCppNewb,

In keeping more with your code here is another way to deal with the zero:

1
2
3
4
5
6
7
8
9
if (value.result < 10)
	std::cout << '0' << value.result << ':';
else
	std::cout << value.result << ":";

if (value.minutes < 10)
	std::cout << '0' << value.minutes << std::endl;
else
	cout << value.minutes << endl;


I have also been meaning to mention the use of the "goto" is bad form and bad programing. If you put most of main in a while loop you can replace the "goto"s with "continue"s and achieve the same result. Then you can add this just before the end of the while loop:

1
2
3
4
5
6
std::cout << "\n Do another Y/N: ";
std::cin >> choice;
choice = std::toupper(choice);  // <--- "toupper" needs header file <cctype>.

if (choice == 'N')
	break;  // <--- Leaves the while loop. 


After that some of your logic has some problems. Like adding "hours" to "minutes". Although they are the same type of variable they are still different types of values that leads to false results. You have some good information to work with just going about in the wrong way. Still working on the logic part.

Hope that helps,

Andy
Topic archived. No new replies allowed.