In: Computer Science
use c++ language. Create a structure called time. Its three members, all type int, should be called hours, minutes, and seconds. Write a program that prompts the user to enter a time value in hours, minutes, and seconds. This should be in 12:59:59 format. This entire input should be assigned first to a string variable. Then the string should be tokenized thereby assigning the 1st token to hours, 2nd token to minutes, and 3rd token to seconds member variables of the structure called time. Finally inside a void Print_in_Seconds(void) member function of time structure, it should print out the total number of seconds represented by this time value: long totalsecs = t1.hours*3600 + t1.minutes*60 + t1.seconds
comment your code properly. do not use vectors
before coding explain it in simple words and use your own logic
Code:
// C++ program to convert 12 hour to 24 hour
// format
#include<iostream>
#include<string>
using namespace std;
struct time{
int hours;
int minutes;
int seconds;
void print(){
cout<<"Total Second:
"<<hours*3600+minutes*60+seconds;
}
};
time Tokenize(string str)
{
time t;
// Get hours
int h1 = (int)str[1] - '0';
int h2 = (int)str[0] - '0';
int hh = (h2 * 10 + h1 % 10);
int count=0;
int min=0,sec=0;
for (int i=3; i <= 7; i++){
if(i==3 || i==4){
min=min*10+((int)(str[i]-'0'));
}
else if(i==6 || i==7){
sec=sec*10+((int)(str[i]-'0'));
}
}
t.hours=hh;
t.minutes=min;
t.seconds=sec;
return t;
}
// Driver code
int main()
{
cout<<"Enter Time- 00:00:00 Formate: ";
string str;
cin>>str;
time t=Tokenize(str);
t.print();
return 0;
}
Output: