Convesion of char to int !!

Can I ask anyone how will I convert TIME in char to TIME in int form ???


this is my example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  Time in: 08:00
  Time out:17:30

  //Time in and Time out are in char format ... how can I convert it into int without converting the colon":" into integer????

//and i have to compute the total wor hour a day .... so it will become like this ..


 Time out: 17.30
   (minus)-
 Time in:  08.00
 (equals)= 09.30

//so its means that i work for 9 hours and 30 minutes.... pls help me how to convert char to int but without converting the colon":" 
Last edited on
closed account (o3hC5Di1)
Hi there,

I would probably do something like this:

1
2
3
4
5
6
7
8
9
10
11
char time_out[] = "17:30";

int hours, minutes;

//cast first character to an int, then multiply by ten (i.e. 1 means >10:00, 2 means > 20:00)
hours = static_cast<int>(time_out[0])*10;
//then cast second character to int and add it to the hours
hours += static_cast<int>(time_out[1]);

//same for minutes, on one line
minutes = (static_cast<int>(time_out[3])*10) + static_cast<int>(time_out[4]);


Note that you will need to keep in mind that an hour is sixty minutes when you detract time out and time in. For instance:

17:15 - 15:30 -> 17-15 =2, 15-30= -15 => you need to detract another hour: 2-1=1, and you need to detract the remaining minutes: 60-15=45. Result: 1:45.

All the best,
NwN
1
2
3
4
5
6
7
8
9
constexpr char zero = '0' ;

// hours = static_cast<int>(time_out[0])*10;
int hours = ( time_out[0] - zero ) * 10 ; 
//hours += static_cast<int>(time_out[1]);
hours += ( time_out[1] - zero ) * 10 ;

//minutes = (static_cast<int>(time_out[3])*10) + static_cast<int>(time_out[4]);
int minutes = ( time_out[3] - zero ) * 10 + ( time_out[4] - zero ) ;


Note: The values of characters for the decimal digits '0' '1' '2' .... '9' are contiguous and ascending.
So, int( '7' - '0' ) == 7
thanks @NwN ... can i use this one from MONDAY to FRIDAY??? to compute the total hours of work in a week?

Hi there,

I would probably do something like this:

char time_out[] = "17:30";

int hours, minutes;

//cast first character to an int, then multiply by ten (i.e. 1 means >10:00, 2 means > 20:00)
hours = static_cast<int>(time_out[0])*10;
//then cast second character to int and add it to the hours
hours += static_cast<int>(time_out[1]);

//same for minutes, on one line
minutes = (static_cast<int>(time_out[3])*10) + static_cast<int>(time_out[4]);


Note that you will need to keep in mind that an hour is sixty minutes when you detract time out and time in. For instance:

17:15 - 15:30 -> 17-15 =2, 15-30= -15 => you need to detract another hour: 2-1=1, and you need to detract the remaining minutes: 60-15=45. Result: 1:45.

All the best,
NwN




Thanks @JLBorges
constexpr char zero = '0' ;

// hours = static_cast<int>(time_out[0])*10;
int hours = ( time_out[0] - zero ) * 10 ;
//hours += static_cast<int>(time_out[1]);
hours += ( time_out[1] - zero ) * 10 ;

//minutes = (static_cast<int>(time_out[3])*10) + static_cast<int>(time_out[4]);
int minutes = ( time_out[3] - zero ) * 10 + ( time_out[4] - zero ) ;


Note: The values of characters for the decimal digits '0' '1' '2' .... '9' are contiguous and ascending.
So, int( '7' - '0' ) == 7




One thing the TIME IN and TIME OUT above are just an example ...
what i need is to change char to int when i input the TIME IN and TIME OUT from MONDAY TO FRIDAY .... and then compute the total work in a week .... thank you again i hope u will answer this ... thanks .
Last edited on
closed account (o3hC5Di1)
Hi there,

Use JLBorges code - it's much tidier. (Thanks by the way JLBorges, forgot about that neat little trick).

I'm guessing you're having time_in and time_out for 5 days of the week. You should probably store them in a container (like std::array or std::vector), after which you can iterate through the container (i.e. access every element in an automatic loop) and convert them to ints and do your calculations.

Please copypaste us on your full code if you require more help specific to your project.

All the best,
NwN
One thing that I've noticed from the code that I personally don't like is the use of two variables for minutes and hours.

You can get the hours the same way, then multiply that times 60, then add the minutes to the hours. That will make any calculations with multiple times easy to do because you don't have to worry about wrapping around the hour. To retrieve the information, you could just take the time in minutes and divide by 60 for the hours and modulo by 60 for the minutes.

If you don't like this, that's fine. I just feel that the fewer variables you have to use, the better. It is also a lot easier to add the minutes than the minutes and hours.
@NwN can i ask how can i store the TIME IN and TIME OUT in a container ?
Last edited on
closed account (o3hC5Di1)
Hi there,

The cleanest way would be to either use GRex2595's suggestion , or creating a struct which represents the time:

1
2
3
4
5
6
7
8
9
struct time
{
    int minutes, hours;
}

struct workday
{
    time time_in, time_out;
};


This will allow you to create a container of workday's:

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
#include <iostream>
#include <string>
#include <array>  //include std::array header file

const int WORKWEEK_SIZE = 5; //define the length of a workweek
std::array workweek<WORKWEEK_SIZE, workday>;  //Create an array of 5 workdays


//Now you can do things such as
std::string tmp;
constexpr char zero = '0' ;


for (int i=0; i< WORKWEEK_SIZE; ++i) //for every day of the week
{
    workday w; //create a workday

    std::cout << "Enter time in for day " << i+1 << ": "  //ask user for time-in
    std::cin >> tmp;

    w.time_in.hours = ( tmp[0] - zero ) * 10 ; 
    w.time_in.hours+= ( tmp[1] - zero ) * 10 ;
    w.time_in.minutes = ( tmp[3] - zero ) * 10 + ( tmp[4] - zero ) ;


    std::cout << "Enter time out for day " << i+1 << ": " //ask user for time-out
    std::cin >> tmp;

    w.time_out.hours = ( tmp[0] - zero ) * 10 ; 
    w.time_out.hours+= ( tmp[1] - zero ) * 10 ;
    w.time_in.minutes = ( tmp[3] - zero ) * 10 + ( tmp[4] - zero ) ;

    workweek[i] = std::move(w);  //add workday to workweek
}

//You could iterate the array now to calculate and display:

for (int i=0; i< workweek[WORKWEEK_SIZE]; ++i) //for every workday in the workweek container
{
    //print time_in for every day of the workweek (as an example)
    std::cout << "Day " << i+1 << " time in: " << workweek[i].time_in.hours << ":" << workweek[i].time_in.minutes;
}


Note that this code could be cleaned up further using a separate function to convert the string to a "workday" object, as well as overloading operators on the time struct to calculate with it easily. For the sake of clarity to beginners, I opted not to do those here.

All the best,
NwN
my program should be like this ....


Monday Time In: 08:00 time in char format
Monday Time Out : 17:30 time in char format

then convert to int and compute total work hours for Monday

Tuesday Time In: 08:00 time in char format
Tuesday Time Out: 17:00 time in char format

then convert to int and compute total work hours for Tuesday

Wednesday Time In: 07:50 time in char format
Wednesday Time Out: 17:10 time in char format

then convert to int and compute total work hours for Wednesday

Thursday Time In: 07:30 time in char format
Thursday Time Out: 17:00 time in char format

then convert to int and compute total work hours for Thursday

Friday Time In: 09:00 time in char format
Friday Time Out: 15:00 time in char format

then convert to int and compute total work hours for Friday

and then compute the total work hours in a week

hope u can help me .... Thanks in advance ...
Last edited on
closed account (o3hC5Di1)
Hi there,

The code I gave you does pretty much what you need.
Please give it a fair attempt yourself and come back to us with any specific issues you might have.

All the best,
NwN
Topic archived. No new replies allowed.