State Program

Hello all. I have to write a program that prompts the user to enter the name of a state and outputs the state name followed by its abbreviation. There are two kinds of abbreviations:
1. If the state name is one word, the abbreviation is the first two letters of the state in uppercase
2. Otherwise, the abbreviation is the first letter of each word in uppercase

When I run the program, I get a debug assertion failed - string subscript out of range. I have been looking at my code but can't find any error. Anyway, here is the 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <string>
#include <cctype>

using namespace std;

void GetState(string&);
void GetAbbreviation(const string, string&);
void Display(const string, string&);
void PromptUser(char&);

int main()
{
	//Declare variables
	string stateName = "";
	string abbreviation = "";
	char proceed = ' ';
 

	do
	{
		//Get the state name
		GetState(stateName);

		//Get the abbreviation of the state name
		GetAbbreviation(stateName, abbreviation);

		//Output the state and the abbreviation of the state
		Display(stateName, abbreviation);
		
		//Ask the user to continue
		PromptUser(proceed);
	}
	while(proceed == 'y' || proceed == 'Y');
	
	//Terminate program
	return 0;
}

//--------------------------------------
//name: GetState
//Prompt the user to enter a state name.
//--------------------------------------
void GetState(string& state)
{
	//Prompt user for the state name
	cout << "Enter a state name: ";
	getline(cin, state);
}

//---------------------------------------------
//name: GetAbbreviation
//Calculate the abbreviation of the state name.
//---------------------------------------------
void GetAbbreviation(const string state, string& abbr)
{
	//Store the index of the first occurence of a blank space
	int pos = state.find(" ");

	//If the state name has no space, its abbreviation is the first two
	//letters of the state in uppercase
	if(pos == string::npos)
	{
		abbr += static_cast<char>(toupper(state[0]));
		abbr += static_cast<char>(toupper(state[1]));
	}
	else //Otherwise the abbreviation is the first letter of each word in uppercase
	{
		//Store the first letter
		abbr += static_cast<char>(toupper(state[0]));

		//Store the second letter
		abbr += static_cast<char>(toupper(state[pos + 1]));
	}
}

//-------------------------------------------
//name: Display
//Output the state name and its abbreviation.
//-------------------------------------------
void Display(const string state, string& abbr)
{
	//Output the state followed by its abbreviation
	cout << "  " << state << " or " << abbr << endl;

	//Reset the abbreviation to empty before continuing next iteration
	abbr.erase();
}

//--------------------------------------------------
//name: PromptUser
//Ask the user if they want to continue the program.
//--------------------------------------------------
void PromptUser(char& ch)
{
	//Prompt user to continue
	cout << "  Continue (y/n)? ";
	cin >> ch;
}
You never check if the string was read correctly. If stateName is empty or ends with a space you will go out of range.

Another problem is that you use an int to store the result of string::find. Use size_t instead.
size_t pos = state.find(" ");
Ok cool. I found out what you were talking about. The error occured in the PromptUser function. Once the user hits enter the newline character is technically stored in the stateName.
So I added a cin.get(). Thanks!

1
2
3
4
5
6
7
void PromptUser(char& ch)
{
	//Prompt user to continue
	cout << "  Continue (y/n)? ";
	cin >> ch;
         cin.get();
}
Last edited on
Topic archived. No new replies allowed.