deleting string from a vector element

Hello there.

I am doing a converter from 12 hour system to 24 hour system.

the user input should be like this:
07:05:45PM

output:
19:05:45

I have two conditions, whether if it is PM or AM.

if AM:
do not convert anything, just remove AM from the string and print it to screen.

if PM:
convert to the appropriate time (I haven't gotten here yet).

So if you look at line 46, how can I remove AM from the string and print the rest of the input?

For example,

If user inputs:
07:05:45PM
output should be:
07:05:45

How can I make it like this? I haven't found anything related to do this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <numeric> //accumulate
#include <algorithm> //remove()
using namespace std;

//splits string entered by delim
void split(const string &s, char delim, vector<string> &elements)
{
	stringstream ss;
	ss.str(s);
	string item;
	while (getline(ss, item, delim))
	{
		elements.push_back(item);
	}
}

//put the string into a vector
vector<string> splitToVector(const string &s, char delim)
{
	vector<string> elements;
	split(s, delim, elements);
	return elements;
}

//verifies either AM or AM exists in the string
bool AMorPM(vector<string> v)
{
	//convert vector to string so we can use find()
	bool isPM = false;
	string splittedString;
	splittedString = accumulate(begin(v), end(v), splittedString);
	string PM = "PM";
	if (splittedString.find(PM) != string::npos)
	{
		isPM = true;
	}
	return isPM;
}

void result(vector<string> vecOfString, bool isPM)
{
	if (isPM == false)
	{	
		string AM = "AM";
		//delete AM
		vecOfString.erase(std::remove(vecOfString.begin(), vecOfString.end(), "AM"), vecOfString.end());
		for (int i = 0; i < vecOfString.size(); i++)
		{
			cout << vecOfString[i] << ":";
		}
	}
}

int main()
{
	vector <string> splitString;
	string userTimeInput;
	bool isAMorPM = false;
	cin >> userTimeInput;
	splitString = splitToVector(userTimeInput, ':');
	isAMorPM = AMorPM(splitString);
	result(splitString, isAMorPM);
	system("PAUSE");
	return 0;
}
No need to make it complicated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <string>
#include <cctype>

using namespace std;

bool detectClockType(string input, bool &isAm)
{
	if(toupper(input[input.size() - 1]) == 'M' && toupper(input[input.size() - 2]) == 'A') 
	{
		isAm = true; return true;
	}
	else if(toupper(input[input.size() - 1]) == 'M' && toupper(input[input.size() - 2]) == 'P') 
	{
		isAm = false; return true;
	}
	return false;
}

string convertAMToTwentyFour(string input)
{
	input.erase(input.end() - 1);
	input.erase(input.end() - 1);
	return input;
}

string convertPMToTwentyFour(string input)
{
	int h_1 = (input[0] - '0');
	int h_2 = (input[1] - '0');

	h_1++;
	h_2 += 2;

	int h = (h_1 * 10) + h_2;

	h_1 = h / 10;
	h_2 = h % 10;

	input[0] = (h_1 + '0');
	input[1] = (h_2 + '0');

	input.erase(input.end() - 1);
	input.erase(input.end() - 1);
	return input;
}

int main()
{
	string input;
	cout << "Enter clock time (either AM or PM) : "; 
	getline(cin, input);

	bool isAm;
	if(detectClockType(input, isAm))
	{
		if(isAm) input = convertAMToTwentyFour(input); 
		else input = convertPMToTwentyFour(input);

		cout << "24 hour format is : " << input << endl;
	}

	cin.get();
	return 0;
}


Enter clock time (either AM or PM) : 11:11:11 PM

24 hour format is : 23:11:11

Enter clock time (either AM or PM) : 05:25:30 PM

24 hour format is : 17:25:30
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <cctype>
#include <iomanip>
#include <iostream>
#include <string>

int main( )
{
    char sep{};
    int hr{}, min{}, sec{};
    std::string meridiem{};
    while( true ) {
        std::cout << "12 hour time: ";

        bool is_valid{
            std::cin >> hr >> sep >> min >> sep >> sec >> meridiem &&
            sep == ':' &&
            (hr <= 12 && min <= 59 && sec <= 59)
        };
        for( char& c: meridiem ) c = toupper( c );
        is_valid = is_valid && (meridiem == "AM" || meridiem == "PM");
        if( !is_valid ) {
            std::cout << "invalid input!\n";
            break;
        }

        std::cout << "24 hour time = ";
        if( meridiem == "AM" ) {
            std::cout << std::setw( 2 ) << std::setfill( '0' ) << hr  << ":";
            std::cout << std::setw( 2 ) << std::setfill( '0' ) << min << ":";
            std::cout << std::setw( 2 ) << std::setfill( '0' ) << sec << "\n";
        }
        else if( meridiem == "PM" ) {
            std::cout << std::setw( 2 ) << std::setfill( '0' ) << (hr + 12) % 24 << ":";
            std::cout << std::setw( 2 ) << std::setfill( '0' ) << min << ":";
            std::cout << std::setw( 2 ) << std::setfill( '0' ) << sec << "\n";
        }
        
        std::cout << "\n";
   }
}


12 hour time: 12:34:56pm
24 hour time = 00:34:56

12 hour time: 12:34:56am
24 hour time = 12:34:56

12 hour time: 13:45:54pm
invalid input!
Topic archived. No new replies allowed.