Created
October 5, 2018 18:20
-
-
Save Abdulazizsayed/be361f2c2e3be08b02edb02cf3082d03 to your computer and use it in GitHub Desktop.
A program that convert time from 12-hour system to 24-hour system.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
using namespace std; | |
void time_convert(string time); | |
int main() | |
{ | |
string str, time=""; | |
cout << "Please, Enter the time you want to convert: "; | |
getline(cin ,str); | |
for(int i=0 ; i<str.length() ; i++){ | |
if(str[i] == ':' || str[i] == ' '){ | |
continue ; | |
} | |
time += str[i]; | |
} | |
cout << "The converted time is: "; | |
time_convert(time); | |
cout << " Hours.\n"; | |
return 0; | |
} | |
void time_convert(string time) | |
{ | |
if(time.length() == 6){ | |
if(time.substr(0 ,2) == "12"){ | |
if(time.substr(4) == "AM" || time.substr(4) == "am"){ | |
cout << "00" + time.substr(2 ,2); | |
} | |
else{ | |
cout << time.substr(0 ,4); | |
} | |
} | |
else{ | |
if(time.substr(4) == "pm" || time.substr(4) == "PM"){ | |
cout << char(time[0] + 1) << char(time[1] + 2) << time.substr(2 ,2); | |
} | |
else{ | |
cout << time.substr(0 ,4); | |
} | |
} | |
} | |
else{ | |
if(time.substr(3) == "pm" || time.substr(3) == "PM"){ | |
cout << "1" << char(time[0] + 2) << time.substr(1 ,2); | |
} | |
else{ | |
cout << "0" << time.substr(0 ,3); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment