1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
vaieri [72.5K]
3 years ago
9

Time Conversion C++: Given a time in -hour AM/PM format, convert it to military (24-hour) time.

Computers and Technology
1 answer:
mixer [17]3 years ago
8 0

Answer:

Code in C++::

#include<bits/stdc++.h>

#include<iostream>

#include<string>

#include<sstream>

using namespace std;

string timeConversion(string s){

/**

* Following two strings are declared named d and c respectively.

* String c will be storing the converted format time and we will return

* c as the answer.

*/

string d,c="";

/**

* String d stores "PM" or "AM"

*/

d=s.substr(8,2);

/**

* An integer named hr is declared below and it stores

* the hh part of string hh:mm:ssPM in integer format.

*/

int hr=atoi(s.substr(0,2).c_str());

if(hr==12 && d=="AM"){

/**

* Now suppose hr is 12 and its AM then we know that it's

* midnight and so hr must be 0.

*/

hr=0;

}

if(d=="PM" && hr!=12){

/**

* Suppose d is "PM" and hr is not 12 then we add 12 into hr.

*/

hr+=12;

}

if(hr<10){

/**

* Now suppose hr is less then 10 then we know that in final answer

* if hr is 7 then we need "07".

* So if hr < 10 then we add extra 0 at start of c string.

*/

c+="0";

}

/**

* Now we convert hr back to string using stringstream.

* A variable named hour is declared and we convert hr to string as follows.

*/

stringstream hour;

hour<<hr;

/**

* Finally we update the c string as required and return it at the end.

*/

c=c+hour.str()+s.substr(2,6);

return c;

}

int main(){

/**

* A string named s is declared and using cin we scan the string.

*/

string s;

cin>>s;

/**

* Below we call the function and pass string s as parameter.

* Whatever function returns, it is printed.

*/

cout<<timeConversion(s)<<endl;

return 0;

}

Explanation:

see thees attached

You might be interested in
What is the best stratiget to avoid paying intrest in your credit cared
pochemuha
Paying it in full month over month.
4 0
3 years ago
I need this ASAP!
Elanso [62]

Answer:

b

Explanation:

4 0
3 years ago
Read 2 more answers
When parallel parking, you should select a space that is __________ as long as your car.
Shtirlitz [24]

that is Equally as long as your car


7 0
4 years ago
Configuring a firewall to ignore all incoming packets that request access to a specific port is known as ____.
Sati [7]

Answer: Logical Point Blocking

Explanation:

4 0
1 year ago
In which type of network can a computer upload and download data at the same time?
REY [17]

answer is peer-to-peer


hope this helps

8 0
3 years ago
Other questions:
  • Worms often try to disguise from where they are sending data by using a bogus ip addresses instead of using an authorized ip add
    11·1 answer
  • Universal Containers has a bundle with many features. In one of these features, Feature A, all product options share the same Di
    5·1 answer
  • Some hardware can be added to the computer without having to restart or power down the computer. After a short period of time th
    5·1 answer
  • Which term is used for a formal, documented process that describes when and how official project documents may be modified, the
    14·2 answers
  • create a function named dispAry. This function has 2 arguments, the first one is an integer array and second one is a integer to
    15·1 answer
  • 40 points to answer ALL
    9·2 answers
  • Which of these pieces of hardware is used for telecommunication
    12·2 answers
  • What are some of these new iPad extras? One is the iMarker. Crayola teamed up with another company to make it. It costs $29.99.
    8·1 answer
  • list out the application of the computer and explain it?(and as the answer for this question as image) ​
    7·1 answer
  • Which type of protocol provides a way to transfer files from one computer to another over any tcp/ip network, such as a lan or t
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!