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.
#include <iostream>
#include <string>
#include <cctype>
usingnamespace 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;
}
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();
}