This question already has answers here:
C++ Converting a time string to seconds from the epoch
(10 answers)
Closed 9 years ago.
I have a string containing a value of time in HH:MM:SS(':' are a part of the string.) format.I have to convert the time into seconds and give that value to an integer variable.I am not aware of any function that can help me do it.
Use sscanf_s.
For example:
void ScanTime(char* strTime)
{
int hours;
int minutes;
int seconds;
sscanf_s(strTime, "%d:%d:%d", &hours, &minutes, &seconds);
printf("hours: %d\nminutes:%d\nseconds:%d\n", hours, minutes, seconds);
}
int _tmain(int argc, _TCHAR* argv[])
{
ScanTime("13:57:44");
return 0;
}
Since this is not a standard C++ function it greatly depends on what framework or library you are using.
This how it's done in Borland C++ Builder:
String Time;
Time = "12:34:56";
TDateTime DT;
try{
DT = StrToTime(Time);
}catch(...){String M = "Error converting time string: "+Time; Application->MessageBox(M.c_str(),"ERROR",MB_OK );}
Use strtol.
for example::
int a=(int)strtol(numeric_string.c_str(),(char **)NULL,10);
if your intension isto simply convert HH:MM:SS, you need to write a function
algorithm would be like(assuming 24Hr format):
split the string using ":"
Convert each part using strtol to integer.
last part+(60* middle part)+(60*60*firstpart) will give you the
actual value
Related
This question already has answers here:
Convert char to int in C and C++
(14 answers)
Closed 2 years ago.
I am having trouble trying to converting time from 12 hour format to 24 hour format.
Note:- This is from an online test site and is filled with boilerplate code to work with the site, so I'm only going to post the part of the program where I am allowed to type.
string timeConversion(string s) {
/*
* Write your code here.
*/
int hours = ((int) s[0])*10+((int) s[1]);
char r[7];
//cout<< sizeof(s)<<"\n";
if(s[8]=='P')
{
hours=hours+12;
r[0]=(char) (hours/10);
r[1]=(char) (hours%10);
for (int i=2;i<8;i++)
{
r[i]=s[i];
}
}else
{
for(int i=0;i<8;i++)
{
r[i]=s[i];
}
}
return r;
}
Here is the input and outputs of test
Input(stdin):-
07:05:45PM
My output(stdout):-
6:05:45
Expected output:-
19:05:45
Now I test line 5 (i.e the line where i convert the hours section into an integer) in another compiler by itself and for some reason instead of properly converting its showing hours=534
Can you guys tell me what went wrong and how to fix it?
I found out what was going wrong with my code earlier. The reason why it didnt work is because c++ doesn't convert the numerical character into the actual numerical value, rather it converts it into the corresponding ascii value for which starts at 48 for "0" and ends at 57 for "9"
Statement to use for converting hours in the string and vice versa would be to
string -> int
int hours = (((int) s[0])%48)*10+(((int) s[1])%48);
or
int hours = (((int) s[0])-48)*10+(((int) s[1])-48);
int -> string
r[0]=(char) ((hours/10)+48);
r[1]=(char) ((hours%10)+48);
I have a string containing the seconds from the epoch, how do i convert this string to a format
as such -
YEAR-MONTH-DAY HH:MM:SS
Here MONTH should display the month number and DAY gives the day number. Also the time needs to be in 24 hour format.
For example :
2019-06-26 11:14:25
I have tried using strptime but havent been successful in doing so, could someone help me in what I am doing wrong.
This is what i have tried so far
int main()
{
string timee = "12341234534";
const char *ptr = timee.c_str();
struct tm tim;
strptime(ptr, "%S", &tim);
time_t abcd = mktime(&tim);
cout<<abcd;
return 0;
}
Found this code snippet on another stackoverflow link
How to convert a string variable containing time to time_t type in c++?
Your question is a little confusing, but I think I figured out what you want...
If the time is in a time_t compatible format, then just read it directly into a time_t variable, and convert it through std::localtime so you can output it in the wanted format using std::put_time.
Something like
std::time_t time;
std::cin >> time;
std::cout << std::put_time(std::localtime(&time), "%F %T") << '\n';
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
When I used VS 2015 to debug the following code,
int main()
{
char thev[8] = "0.12345";
float fv = atof(thev);
printf("%f\n", fv);
return 0;
}
the value of fv in watch window is 0.12345004,
and printf is 0.123450, how to let fv=0.12345 in the run time?
There is similar post, here, but no answer there, can somebody help me?
And I pasted that code to my VS 2015,
int main()
{
const char *val = "73.31";
std::stringstream ss;
ss << val;
double doubleVal = 0.0f;
ss >> doubleVal;
}
the value doubleVal in watch window is 73.310000000000002
Replace the following code:
printf("%f\n", fv);
with:
printf("%.5f\n", fv);
Explaination:
we use a width (%.5f) to say that we want 5 digits (positions) reserved for the output.
The result is that 5 “space characters” are placed before printing the character. And the next character will be not printed.
Reference: https://www.codingunit.com/printf-format-specifiers-format-conversions-and-formatted-output
There is no exact representation in IEEE single precision for 0.12345. The closest two values on either side of it are 0.123450003564357757568359375 and 0.12344999611377716064453125. atof is picking the former, I think because it is closer to 0.12345 than the latter.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am passing a string variable (std::string) and iterating through the string character by character. Whenever I run into a decimal, I want to combine the previous position on the string (i.e 2) and the next position in the string (i.e 5) into a double. So how would I go about making the char 2, char . , char 5 into one whole value (2.5)?
std::double x;
std::string varibleName = "4 5 7 2.5";
for (int i = 0; i < variableName.length(); i++) // iterates through variableName
{
if (variableName[i] == '.'){ // if the current position of the iteration is a decimal, I want to grab the char before the decimal and the char after the decimal so I can combine all three positions of the string making it 2.5 and not 25.
}
}
Well, you are wildly overthinking it. The C++ library provides std::stof, std::stod, std::stold that does exactly what you want. Convert a string like "2.5" to a float, double or long double, e.g.
#include <iostream>
int main (void) {
std::string s = "2.5";
double d = std::stod(s);
std::cout << d << "\n";
}
Example Use/Output
$ ./bin/stodex
2.5
Look things over and let me know if you have further overthinking questions.
Note that giving an example, code snips, and error logs make troubleshooting a lot easier :)
It sound like you have some input like "2.1" and need to convert it to a double like 2.1?
If that is the case you can use the <cstdlib> atof function.
Example:
/* atof example: sine calculator */
#include <stdio.h> /* printf, fgets */
#include <stdlib.h> /* atof */
#include <math.h> /* sin */
int main ()
{
double n;
char buffer[256];
printf ("Enter degrees: ");
fgets (buffer,256,stdin);
n = atof (buffer);
printf ("You entered %s which is a double like %f\n" , buffer, n);
return 0;
}
This question already has answers here:
How to convert a single char into an int [duplicate]
(11 answers)
Closed 5 years ago.
i have been trying to make a program that checks if a national identification number is valid how ever i have run into a issue and i can't find an answer anywhere.
I am using a string to store the users input of the identification code and i need to somehow covert the string to int keeping the symbol instead of getting an ascii value.
Example:
(lets say the user inputs the string persKods as 111199-11111
the 5th symbol is 9 so the year value should output as 9 instead of 54 which is its ascii value)
int day,month,year;
year=this->persKods.at(4);
cout << year; // it outputs 54 instead of 9 :/
Can you try to ascci value of '0'.
#include <iostream>
using namespace std;
int main() {
// your code goes here
string str="111199";
int test = str.at(4) - '0';
cout<<test;
return 0;
}
For more information link