logo
down
shadow

Outputting time in minutes and seconds C++


Outputting time in minutes and seconds C++

By : Victoria Torres
Date : November 21 2020, 11:01 PM
fixed the issue. Will look into that further Accept the total time as an integer instead of a string, and then you can divide it by 60 to get whole minutes and modulus of 60 to get the remaining seconds:
code :
#include <stdio.h>
#include <string>

int main() {
    char snail_name[15] = "";
    int time_taken = 0;

    printf("\nEnter name of snail: ");
    scanf("%15s", snail_name);
    printf("Time to finish course:");
    scanf("%d", &time_taken);
    printf("%s ran the course in %d:%d\n", &snail_name, time_taken / 60, time_taken % 60);

    return 0;
}


Share : facebook icon twitter icon
Python: Read text file with time in hour, minutes and seconds; and angle in degree, arc minutes and arc seconds

Python: Read text file with time in hour, minutes and seconds; and angle in degree, arc minutes and arc seconds


By : Mr. Robot
Date : March 29 2020, 07:55 AM
To fix the issue you can do Using matplotlib.dates.datestr2num you could easily convert your first column to plottable numbers, but I did not find a function for your second column. You can build a function to process that, though:
code :
import numpy as np

def calc_hour( str ):
    hour, min, sec = [float(i) for i in str.split(':')]
    min += sec/60.
    hour += min/60.
    return hour

calc_hour = np.vectorize( calc_hour )

def calc_deg( str ):
    deg, min, sec = [float(i) for i in str.split(':')]
    min += sec/60.
    deg += min/60.
    return deg

calc_deg = np.vectorize( calc_deg )
values = np.loadtxt('tmp.txt', dtype=str)
hours= calc_hour( values[:,0] )
degs =  calc_deg( values[:,1] )
hours = array([ 0.10133333,  0.11713889,  0.23825   ,  0.38333333,  0.39730556,
                0.39844444,  0.39983333,  0.40102778,  0.40105556,  0.40108333,
                0.50836111])    

degs = array([-70., -66., -59., -52., -49., -29., -28., -26., -14.,  25.,  30.])
import matplotlib.pyplot as plt
plt.plot(hours,degs)
Converting time, after 60 seconds it is only displaying the minutes. I am trying to display minutes followed by seconds.

Converting time, after 60 seconds it is only displaying the minutes. I am trying to display minutes followed by seconds.


By : user2315317
Date : March 29 2020, 07:55 AM
it helps some times How do I display the seconds followed by the minutes like 1:20. Before the 60 seconds it displays the seconds but after the 60 seconds, it displays 1 mins. Following a tutorial but it is not what I am looking for. If you need more information let me know. , You could do like this
code :
private String parseDuration(int durationInSeconds) {
    return String.format("%02dm :: ", durationInSeconds/60) + String.format("%02ds", durationInSeconds%60);
}
How to format elapsed time from seconds to hours, minutes, seconds and milliseconds in Python?

How to format elapsed time from seconds to hours, minutes, seconds and milliseconds in Python?


By : user2975840
Date : March 29 2020, 07:55 AM
it should still fix some issue How can I format the time elapsed from seconds to hours, mins, seconds? , If you want to include times like 0.232999801636 as in your input:
code :
import time
start = time.time()
end = time.time()
hours, rem = divmod(end-start, 3600)
minutes, seconds = divmod(rem, 60)
print("{:0>2}:{:0>2}:{:05.2f}".format(int(hours),int(minutes),seconds))
In [12]: def timer(start,end):
   ....:         hours, rem = divmod(end-start, 3600)
   ....:         minutes, seconds = divmod(rem, 60)
   ....:         print("{:0>2}:{:0>2}:{:05.2f}".format(int(hours),int(minutes),seconds))
   ....:     

In [13]: timer(12345.242,12356.434)
00:00:11.19
In [14]: timer(12300.242,12600.5452)
00:05:00.30
In [19]: timer(0.343,86500.8743)
24:01:40.53
In [16]: timer(0.343,865000.8743)
 240:16:40.53    
In [17]: timer(0,0.232999801636)
00:00:00.23
Parsing t=1h2m3s(h - hour, m-minutes, s-seconds ) into seconds if order of hours, minutes and seconds are valid

Parsing t=1h2m3s(h - hour, m-minutes, s-seconds ) into seconds if order of hours, minutes and seconds are valid


By : 鲁二峰
Date : March 29 2020, 07:55 AM
may help you . I would first check that the order matches then, if it does calculate the time in seconds.
code :
function calculateInSeconds(timeStamp) {
  var timeInSeconds=0;
  console.log("calculateInSeconds timeStamp:"+timeStamp);
  if(timeStamp.match(/t=[0-9]*h?[0-9]*m?[0-9]*s?/g).toString()==timeStamp){
    timeStamp.replace(/([0-9]+)[h|m|s]/g, function(match, value) {
       if (match.indexOf("h") > -1) {
          timeInSeconds += value * 60 * 60;
       } else if (match.indexOf("m") > -1) {
                timeInSeconds += value * 60;
       } else if (match.indexOf("s") > -1) {
                timeInSeconds += value * 1;
       }
     });
   }
   console.log("timeInSeconds"+timeInSeconds);
}
calculateInSeconds("t=1h2m");//3720 valid
calculateInSeconds("t=2m1h");//0 invalid
minutes and seconds function outputting unwanted numbers to end if time contains milliseconds

minutes and seconds function outputting unwanted numbers to end if time contains milliseconds


By : cainho
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Apply with Math.round() function it will rounded the values.
Refer the link
code :
var time = 122.33, // example time (2 minutes, 2 seconds)
		mins = ~~(time / 60),
		secs = time % 60;

	var hrs = ~~(time / 3600),
		mins = ~~((time % 3600) / 60),
		secs = time % 60;

	var ft = "";

	if (hrs > 0) {
	    ft += "" + hrs + ":" + (mins < 10 ? "0" : "");
	}
	ft += "" + mins + ":" + (secs < 10 ? "0" : "");
	ft += "" +Math.round( secs);
console.log(ft);
Related Posts Related Posts :
  • How could I link the Boost (not in usr/include) in CMakeList.txt using find_package
  • C++, Linker Issue returning undefined reference
  • Can you use std::string in a 3rd party dll?
  • Why does this use of std::sort with a custom comparator not compile?
  • Fixed base address in MSVC2015
  • Armadillo C++ Matrix Library - simplest way to initialize all values in a matrix to 999?
  • How do you end while loop
  • C++ Basic While Loop: Unknown Inputs and Value Increment
  • Cmake build failed with CMAKE_AR-NOTFOUND (cr exe not found)
  • c++ typeid on class member operator() overloads
  • Different size of string showing when going by two different methods
  • what the meaning "source of entropy of sth." on stack
  • wxWidgets - wxStyledTextCtrl - Text alignment RTL
  • get the entry point to a 64bit process memory from a 32bit process
  • Stroustrup: For C++, how do I install FLTK library?
  • Getting a word after a certain character?
  • How can I convert a char vector to a char array?
  • Comparing vectors of different length c++
  • How do I add four seperate times together?
  • How to replace data in a certain node in a linked list?
  • Prepared statements in C++ libneo4j_client?
  • FindFirstVolume does not return EFI system partition
  • User Inputs Value, but Arbitrary Value Printed to Screen. Printed Value doesn't Match Entered Value
  • How to work with std::make_unique function and interface classes?
  • Refresh image in command line (C++)
  • How to flip a Char array with pointers in C++
  • Creating a for loop to continue to count
  • how to get to field of 2D object dynamic array?
  • Multiple class object share common variable
  • Why is my multi-threaded program not terminating properly?
  • How do I open a file in C++ using just the filename?
  • QT DOMXml - Change the name of a node
  • Using undeclared list as argument in c++
  • While loop won't continue
  • If/else if always goes to else statement
  • Why shared_timed_mutex is defined in c++14, but shared_mutex in c++17?
  • Confused by pipes. 'cat -A' seems to filter out part of output
  • Cannot open source file gtest/gtest.h
  • How to get a certain word in string of getline?
  • glGetShaderInfoLog to std::string?
  • Read in a certain line only?
  • C++ Vector read access violation Mylast returned 0x8
  • In C++ why is ifstream getline returning every other number in my .txt file, rather than all of them?
  • Can't convert string to int in return. No string variable declared whatsoever in int returning function getsocial()
  • Segmentation fault due to size() function of self-implemented data structure in C++
  • i need help making this little c++ program work
  • Template function wrapper
  • static variable in #define directive c++
  • In Qt MainWindow, why does an instance have a pointer to an instance?
  • OpenGL glDrawElements
  • Trying to disable a function if any type of list is passed in
  • make a variable inaccessible in a certain scope, possible?
  • Implement Node Addon in Xcode
  • Controlling compiler directive in file from other file C++
  • QSqlQuery INSERT statement with sqlite3
  • Why doesn't clang/llvm optimize this?
  • STL map declaration in c++
  • No warning, when passing a member variable by (const) reference to a non-const member function
  • I got the first size working but can't set the size for my other constructors
  • Vulkan error: Cannot submit cmd buffer using deleted buffer 0x0
  • shadow
    Privacy Policy - Terms - Contact Us © soohba.com