Changing State Names to Abbreviations

The purpose of the code is to read in state names, say california and print CA. However, if it was a state with two words, it would read in only the first letter of each. I.E New Hampshire would be NH.

here's my code:
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
#include <iostream>
#include <iomanip>
#include <cctype>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;

int main()
{
	//declare variable
	string state;
	char cont;
	size_t pos = state.find(" ");

	do
	{
		cout << "Enter a State name:";
		getline(cin, state);

		//If there is two words only read the first letter of each
		if (pos==string::npos)
		{
			string initial = state.substr(0, 2); //declare intial for first two letters
			std::transform(initial.begin(), initial.end(), initial.begin(), std::ptr_fun<int, int>(std::toupper));
			cout << "\t" << initial << endl;
			cout << "\t" << "Continue(y/n)";
			cin >> cont;
			cont = tolower(cont);
		}

		//Read the first two letters of a single word state

		else		
		{
			string initial = state.substr(0, 1); 		//declare intial first letter

			string initial2 = state.substr(pos + 1, 1);  //declare initial for 2 letter
			std::transform(initial.begin(), initial.end(), initial.begin(), std::ptr_fun<int, int>(std::toupper));
			std::transform(initial2.begin(), initial2.end(), initial2.begin(), std::ptr_fun<int, int>(std::toupper));
			cout << "\t" << initial;
			cout << initial2 << endl;
			cout << "\t" << "Continue(y/n)";
			cin >> cont;

			
		}
		getline(cin, state);
	} while (cont == 'y');
	system("pause");

	return 0;
}







I am able to get the one word states and tried getting the two word states but it doesnt work.
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
#include <iostream>
#include <iomanip>
#include <cctype>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;

int main()
{
	//declare variable
	string state;
	char cont;
	size_t pos = state.find(" ");

	do
	{
		cout << "Enter a State name:";
		getline(cin, state);
		
		size_t pos = state.find(' ');

		//If there is two words only read the first letter of each
		if (pos==string::npos)
		{
			string initial = state.substr(0, 2); //declare intial for first two letters
			std::transform(initial.begin(), initial.end(), initial.begin(), std::ptr_fun<int, int>(std::toupper));
			cout << "\t" << initial << endl;
			cout << "\t" << "Continue(y/n)";
			cin >> cont;
			cont = tolower(cont);
		}

		//Read the first two letters of a single word state

		else		
		{
			string initial = state.substr(0, 1); 		//declare intial first letter

			string initial2 = state.substr(pos + 1, 1);  //declare initial for 2 letter
			std::transform(initial.begin(), initial.end(), initial.begin(), std::ptr_fun<int, int>(std::toupper));
			std::transform(initial2.begin(), initial2.end(), initial2.begin(), std::ptr_fun<int, int>(std::toupper));
			cout << "\t" << initial;
			cout << initial2 << endl;
			cout << "\t" << "Continue(y/n)";
			cin >> cont;

			
		}
		getline(cin, state);
	} while (cont == 'y');
	system("pause");

	return 0;
}
Last edited on
Topic archived. No new replies allowed.