0

Input is in the format: hh:mm:ssAM or hh:mm:ssPM

Example:

INPUT: 07:05:45PM

OUTPUT: 19:05:45

#include <bits/stdc++.h>
#include<iostream>
#include<string>


using namespace std;

int main()
{
    int hh,mm,ss ;
    char tz[2];

    cin>>hh>>mm>>ss>>tz;


    if(strcmp(tz,"AM")==0 && hh==12)
    {
        hh=0;
    }

    else if(strcmp(tz,"PM")==0 && hh!=12)
    {
        hh += 12;

    }
    cout<<hh<<":"<<mm<<":"<<ss;
    return 0;
}

Unlike the expected output i.e 19:05:45 I am getting 7:0:0

  • 2
    You should never `#include `. It is not proper C++. It ruins portability and fosters terrible habits. See [Why should I not `#include `](https://stackoverflow.com/q/31816095). – L. F. Jul 26 '19 at 07:49
  • 2
    Also, please avoid `using namespace std;`. It is considered bad practice. See [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721) – L. F. Jul 26 '19 at 07:49
  • 2
    When `>>` operator encounters ':' in input line it can't input it in either `mm` or `ss`, so these variables set to `0`. You need to input ':' in `char` variable. Also, you `tz` array is only 2 characters long, but C style strings (why did you use it instead of `std::string` at all???) should have `'\0'` terminator character at end, i.e. you need to reserve 3 elements. – sklott Jul 26 '19 at 07:49

3 Answers3

1

Your statement cin>>hh>>mm>>ss>>tz; takes no account of the colons in your input. It's an example of how code that's roughly right is actually no good at all. You have to get code exactly right. You can't just write something that is roughly right and hope that the computer will understand what you mean.

You also have an error in the way you declared tz. Remember that for a C style string you have to have room for the nul terminator. So you need an array one bigger that the maximum length of your string, i.e. char tz[3];.

Another problem is the headers. The correct header for strcmp is #include <cstring> (the <string> header is for C++ strings which you aren't using) and <bits/stdc++.h> is not a standard C++ header, you should get out of the habit of using that.

Here's how I might do this

int hh, mm, ss;
char dummy1, dummy2, tz[3];

cin >> hh >> dummy1 >> mm >> dummy2 >> ss >> tz;

The purpose of the dummy variables is to read the colons. You aren't interested in the colons but you can't just ignore them.

This code has no checking for errors in the input, which in a real-world program would be a serious problem. But maybe for a tutorial program that doesn't matter.

john
  • 71,156
  • 4
  • 49
  • 68
0

Here look your input carefully .
INPUT 07:00:05PM,
Here after hour,minute ,there is an colon but you are not considering it during input. So please take an colon as input after hour and minute.

For example:

char tz[3],colon;
cin>>hh>>colon>>mm>>colon>>ss>>tz;

Full code will be:

#include <bits/stdc++.h>
#include<iostream>
#include<string>


using namespace std;

int main()
{
    int hh,mm,ss ;
    char tz[3],colon;
    cin>>hh>>colon>>mm>>colon>>ss>>tz;
    if(strcmp(tz,"AM")==0 && hh==12)
    {
        hh=0;
    }

    else if(strcmp(tz,"PM")==0 && hh!=12)
    {
        hh += 12;

    }
    cout<<hh<<":"<<mm<<":"<<ss;
    return 0;
}
mahbubcseju
  • 1,878
  • 1
  • 11
  • 15
0

If your format is fixed, what about modifying the string in-place ?

if (time[8] == 'P') {
  if (time[1] < '8') {
    time[1]+= 2; time[0]+= 1; // No carry
  }
  else {
    time[1]-= 8; time[0]+= 2; // Carry
  }
}

(This assumes the ANSI collating sequence. It is million times more efficient.)

Yves Daoust
  • 48,767
  • 8
  • 39
  • 84
  • @sklott: why not ? – Yves Daoust Jul 26 '19 at 09:16
  • This is hackish code applicable only in contests and such. Such code should be avoided in production (unless there are very pressing need to do so, such as performance optimization) and beginners should learn "proper" way to code before "hackish" way. Doing it this way just because its "million times more efficient" without real need to be such efficient is "premature optimization". – sklott Jul 26 '19 at 09:50
  • You'll have to handle the case of 12 AM being converted to 00. Not that I would recommend this solution to the problem. It's clever and solves the problem, and it is more efficient. But clear and provably correct code is more important, in my experience. And in the context of what this code is doing, efficiency is irrelevant: the cost of I/O will dwarf even the cost of the OP's less-efficient code. – Jim Mischel Jul 26 '19 at 17:43
  • @JimMischel: you are right, I am not used to the intricacies of that notation. – Yves Daoust Jul 26 '19 at 19:40
  • You get 24 for 12PM instead of 12AM. If the hour is 12, you can flip the AM/PM, then the answer is correct. – Ayxan Haqverdili Jan 13 '21 at 08:15