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
Please fill these out. I really need this done.
tatyana61 [14]

Answer:

i can't see this

Explanation:

5 0
3 years ago
Which person would be the best fit for a career in the Information Technology field?
zaharov [31]
The best possible fit would be a person with a creative mind and flexible mind. 
The technical stuff can be taught and learnt. But to evolve and keep up with the pace of how technology improves day to day, you'd need some with flexibility to cope up with this and creativeness to make innovations.
6 0
3 years ago
Read 2 more answers
Which one of the following terms is defined as the material and surfaces upon which an artist works?
katrin [286]

Media is the surface or material that an artist works on


5 0
4 years ago
7. Explain the steps for formatting a shape ?
alisha [4.7K]

Answer:

To begin, select the shapes you want to format. To select more than one, press and hold the Shift key. When you select one or more shapes, a new Drawing Tools tab appears. Here, you can select Shape Fill to fill the selected shapes with a solid color, gradient, texture, or picture.

Explanation:

3 0
3 years ago
Your ipad has an application that will not stop running. what feature/tool can you use to stop it? kill treason task manager for
saveliy_v [14]

you could force quit

6 0
3 years ago
Read 2 more answers
Other questions:
  • What is cyberbullying?
    14·2 answers
  • When u look at a green object through red glass the object will appear
    11·2 answers
  • Which date formats are allowed in Excel for February 14, 2018? Check all that apply.
    15·2 answers
  • Which of the following dimensions of e-commerce technology involves the integration of video, audio, and text marketing messages
    11·1 answer
  • In a ______topology, every device has exactly two neighbors for communication purposes. A failure in any cable or device can tak
    15·2 answers
  • Short notes on Supply chain Management System (SCMS)
    12·1 answer
  • Difrent between computer and computer system​
    9·1 answer
  • In design and implementation of any _____ reasoning application, there are 4 Rs involved: retrieve, reuse, revise, and retain.
    7·1 answer
  • Caps lock key is used to type alphabets. _________​
    6·2 answers
  • What would be printed to the screen when the following program is run?
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!