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":"
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.
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 .
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.
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.
#include <iostream>
#include <string>
#include <array> //include std::array header file
constint 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;
constexprchar 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.
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.